From ffe5f085cd8b289a7853fcdb3c26adbebda09fd6 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 25 Jul 2022 12:32:24 +0800 Subject: [PATCH] runtime: refine code reducing indents in netpollBreak() Change-Id: I2d1528910cb3660344c7a664d6f32306defe75d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/419321 Reviewed-by: Michael Pratt Auto-Submit: Michael Knyszek Reviewed-by: Michael Knyszek TryBot-Result: Gopher Robot Run-TryBot: Michael Pratt --- src/runtime/netpoll_aix.go | 9 ++++++--- src/runtime/netpoll_epoll.go | 33 ++++++++++++++++++--------------- src/runtime/netpoll_kqueue.go | 27 +++++++++++++++------------ src/runtime/netpoll_solaris.go | 25 ++++++++++++++----------- src/runtime/netpoll_windows.go | 13 ++++++++----- 5 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/runtime/netpoll_aix.go b/src/runtime/netpoll_aix.go index 22cc513881..5247e56373 100644 --- a/src/runtime/netpoll_aix.go +++ b/src/runtime/netpoll_aix.go @@ -135,10 +135,13 @@ func netpollarm(pd *pollDesc, mode int) { // netpollBreak interrupts a poll. func netpollBreak() { - if atomic.Cas(&netpollWakeSig, 0, 1) { - b := [1]byte{0} - write(uintptr(wrwake), unsafe.Pointer(&b[0]), 1) + // Failing to cas indicates there is an in-flight wakeup, so we're done here. + if !atomic.Cas(&netpollWakeSig, 0, 1) { + return } + + b := [1]byte{0} + write(uintptr(wrwake), unsafe.Pointer(&b[0]), 1) } // netpoll checks for ready network connections. diff --git a/src/runtime/netpoll_epoll.go b/src/runtime/netpoll_epoll.go index b7d6199965..7ad2c8ab35 100644 --- a/src/runtime/netpoll_epoll.go +++ b/src/runtime/netpoll_epoll.go @@ -79,22 +79,25 @@ func netpollarm(pd *pollDesc, mode int) { // netpollBreak interrupts an epollwait. func netpollBreak() { - if atomic.Cas(&netpollWakeSig, 0, 1) { - for { - var b byte - n := write(netpollBreakWr, unsafe.Pointer(&b), 1) - if n == 1 { - break - } - if n == -_EINTR { - continue - } - if n == -_EAGAIN { - return - } - println("runtime: netpollBreak write failed with", -n) - throw("runtime: netpollBreak write failed") + // Failing to cas indicates there is an in-flight wakeup, so we're done here. + if !atomic.Cas(&netpollWakeSig, 0, 1) { + return + } + + for { + var b byte + n := write(netpollBreakWr, unsafe.Pointer(&b), 1) + if n == 1 { + break + } + if n == -_EINTR { + continue + } + if n == -_EAGAIN { + return } + println("runtime: netpollBreak write failed with", -n) + throw("runtime: netpollBreak write failed") } } diff --git a/src/runtime/netpoll_kqueue.go b/src/runtime/netpoll_kqueue.go index 1694753b6f..78d1663ad9 100644 --- a/src/runtime/netpoll_kqueue.go +++ b/src/runtime/netpoll_kqueue.go @@ -83,19 +83,22 @@ func netpollarm(pd *pollDesc, mode int) { // netpollBreak interrupts a kevent. func netpollBreak() { - if atomic.Cas(&netpollWakeSig, 0, 1) { - for { - var b byte - n := write(netpollBreakWr, unsafe.Pointer(&b), 1) - if n == 1 || n == -_EAGAIN { - break - } - if n == -_EINTR { - continue - } - println("runtime: netpollBreak write failed with", -n) - throw("runtime: netpollBreak write failed") + // Failing to cas indicates there is an in-flight wakeup, so we're done here. + if !atomic.Cas(&netpollWakeSig, 0, 1) { + return + } + + for { + var b byte + n := write(netpollBreakWr, unsafe.Pointer(&b), 1) + if n == 1 || n == -_EAGAIN { + break + } + if n == -_EINTR { + continue } + println("runtime: netpollBreak write failed with", -n) + throw("runtime: netpollBreak write failed") } } diff --git a/src/runtime/netpoll_solaris.go b/src/runtime/netpoll_solaris.go index 6e545b3d31..ec51771b57 100644 --- a/src/runtime/netpoll_solaris.go +++ b/src/runtime/netpoll_solaris.go @@ -191,17 +191,20 @@ func netpollarm(pd *pollDesc, mode int) { // netpollBreak interrupts a port_getn wait. func netpollBreak() { - if atomic.Cas(&netpollWakeSig, 0, 1) { - // Use port_alert to put portfd into alert mode. - // This will wake up all threads sleeping in port_getn on portfd, - // and cause their calls to port_getn to return immediately. - // Further, until portfd is taken out of alert mode, - // all calls to port_getn will return immediately. - if port_alert(portfd, _PORT_ALERT_UPDATE, _POLLHUP, uintptr(unsafe.Pointer(&portfd))) < 0 { - if e := errno(); e != _EBUSY { - println("runtime: port_alert failed with", e) - throw("runtime: netpoll: port_alert failed") - } + // Failing to cas indicates there is an in-flight wakeup, so we're done here. + if !atomic.Cas(&netpollWakeSig, 0, 1) { + return + } + + // Use port_alert to put portfd into alert mode. + // This will wake up all threads sleeping in port_getn on portfd, + // and cause their calls to port_getn to return immediately. + // Further, until portfd is taken out of alert mode, + // all calls to port_getn will return immediately. + if port_alert(portfd, _PORT_ALERT_UPDATE, _POLLHUP, uintptr(unsafe.Pointer(&portfd))) < 0 { + if e := errno(); e != _EBUSY { + println("runtime: port_alert failed with", e) + throw("runtime: netpoll: port_alert failed") } } } diff --git a/src/runtime/netpoll_windows.go b/src/runtime/netpoll_windows.go index 4c1cd2633a..7e4664909c 100644 --- a/src/runtime/netpoll_windows.go +++ b/src/runtime/netpoll_windows.go @@ -67,11 +67,14 @@ func netpollarm(pd *pollDesc, mode int) { } func netpollBreak() { - if atomic.Cas(&netpollWakeSig, 0, 1) { - if stdcall4(_PostQueuedCompletionStatus, iocphandle, 0, 0, 0) == 0 { - println("runtime: netpoll: PostQueuedCompletionStatus failed (errno=", getlasterror(), ")") - throw("runtime: netpoll: PostQueuedCompletionStatus failed") - } + // Failing to cas indicates there is an in-flight wakeup, so we're done here. + if !atomic.Cas(&netpollWakeSig, 0, 1) { + return + } + + if stdcall4(_PostQueuedCompletionStatus, iocphandle, 0, 0, 0) == 0 { + println("runtime: netpoll: PostQueuedCompletionStatus failed (errno=", getlasterror(), ")") + throw("runtime: netpoll: PostQueuedCompletionStatus failed") } } -- 2.50.0