]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: refine code reducing indents in netpollBreak()
authorAndy Pan <panjf2000@gmail.com>
Mon, 25 Jul 2022 04:32:24 +0000 (12:32 +0800)
committerMichael Knyszek <mknyszek@google.com>
Tue, 9 Aug 2022 13:23:50 +0000 (13:23 +0000)
Change-Id: I2d1528910cb3660344c7a664d6f32306defe75d3
Reviewed-on: https://go-review.googlesource.com/c/go/+/419321
Reviewed-by: Michael Pratt <mpratt@google.com>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Michael Pratt <mpratt@google.com>

src/runtime/netpoll_aix.go
src/runtime/netpoll_epoll.go
src/runtime/netpoll_kqueue.go
src/runtime/netpoll_solaris.go
src/runtime/netpoll_windows.go

index 22cc51388156141957c5d10ac934ba3aa1e45527..5247e56373a6619a95d7fb6bb44858d7304beac7 100644 (file)
@@ -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.
index b7d61999659a1e38004a4e473c391e6b1b9ae6ee..7ad2c8ab35e484b09df5547bd2ed784e1a3bcd6b 100644 (file)
@@ -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")
        }
 }
 
index 1694753b6fe0dbee7804398afef925340b31cfcf..78d1663ad9a0a4a309bf41faf539bbe49fdb1e08 100644 (file)
@@ -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")
        }
 }
 
index 6e545b3d31b283ed03efb222b9e56d4aa5dd40b2..ec51771b579acd0f8ac04418eb9195f074d687b9 100644 (file)
@@ -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")
                }
        }
 }
index 4c1cd2633a86ba1bbdb99b25d27446df2e800b33..7e4664909ccdac49b460c33c33896a1d3e8e839d 100644 (file)
@@ -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")
        }
 }