1
0
mirror of https://github.com/golang/go synced 2024-11-17 17:34:46 -07:00

runtime: name nil semaphore of pollDesc

Use explicit name pdNil for nil semaphore of a pollDesc to make it self-explanatory like pdReady and pdWait.

Change-Id: Ibfb246e14419d366edadbccac4d3717f0c135cb0
Reviewed-on: https://go-review.googlesource.com/c/go/+/424923
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Andy Pan 2022-08-19 18:45:51 +08:00 committed by Gopher Robot
parent 723a27994d
commit 556c978400

View File

@ -50,16 +50,17 @@ const (
// goroutines respectively. The semaphore can be in the following states: // goroutines respectively. The semaphore can be in the following states:
// //
// pdReady - io readiness notification is pending; // pdReady - io readiness notification is pending;
// a goroutine consumes the notification by changing the state to nil. // a goroutine consumes the notification by changing the state to pdNil.
// pdWait - a goroutine prepares to park on the semaphore, but not yet parked; // pdWait - a goroutine prepares to park on the semaphore, but not yet parked;
// the goroutine commits to park by changing the state to G pointer, // the goroutine commits to park by changing the state to G pointer,
// or, alternatively, concurrent io notification changes the state to pdReady, // or, alternatively, concurrent io notification changes the state to pdReady,
// or, alternatively, concurrent timeout/close changes the state to nil. // or, alternatively, concurrent timeout/close changes the state to pdNil.
// G pointer - the goroutine is blocked on the semaphore; // G pointer - the goroutine is blocked on the semaphore;
// io notification or timeout/close changes the state to pdReady or nil respectively // io notification or timeout/close changes the state to pdReady or pdNil respectively
// and unparks the goroutine. // and unparks the goroutine.
// nil - none of the above. // pdNil - none of the above.
const ( const (
pdNil uintptr = 0
pdReady uintptr = 1 pdReady uintptr = 1
pdWait uintptr = 2 pdWait uintptr = 2
) )
@ -93,8 +94,8 @@ type pollDesc struct {
// rg, wg are accessed atomically and hold g pointers. // rg, wg are accessed atomically and hold g pointers.
// (Using atomic.Uintptr here is similar to using guintptr elsewhere.) // (Using atomic.Uintptr here is similar to using guintptr elsewhere.)
rg atomic.Uintptr // pdReady, pdWait, G waiting for read or nil rg atomic.Uintptr // pdReady, pdWait, G waiting for read or pdNil
wg atomic.Uintptr // pdReady, pdWait, G waiting for write or nil wg atomic.Uintptr // pdReady, pdWait, G waiting for write or pdNil
lock mutex // protects the following fields lock mutex // protects the following fields
closing bool closing bool
@ -217,21 +218,21 @@ func poll_runtime_pollOpen(fd uintptr) (*pollDesc, int) {
pd := pollcache.alloc() pd := pollcache.alloc()
lock(&pd.lock) lock(&pd.lock)
wg := pd.wg.Load() wg := pd.wg.Load()
if wg != 0 && wg != pdReady { if wg != pdNil && wg != pdReady {
throw("runtime: blocked write on free polldesc") throw("runtime: blocked write on free polldesc")
} }
rg := pd.rg.Load() rg := pd.rg.Load()
if rg != 0 && rg != pdReady { if rg != pdNil && rg != pdReady {
throw("runtime: blocked read on free polldesc") throw("runtime: blocked read on free polldesc")
} }
pd.fd = fd pd.fd = fd
pd.closing = false pd.closing = false
pd.setEventErr(false) pd.setEventErr(false)
pd.rseq++ pd.rseq++
pd.rg.Store(0) pd.rg.Store(pdNil)
pd.rd = 0 pd.rd = 0
pd.wseq++ pd.wseq++
pd.wg.Store(0) pd.wg.Store(pdNil)
pd.wd = 0 pd.wd = 0
pd.self = pd pd.self = pd
pd.publishInfo() pd.publishInfo()
@ -251,11 +252,11 @@ func poll_runtime_pollClose(pd *pollDesc) {
throw("runtime: close polldesc w/o unblock") throw("runtime: close polldesc w/o unblock")
} }
wg := pd.wg.Load() wg := pd.wg.Load()
if wg != 0 && wg != pdReady { if wg != pdNil && wg != pdReady {
throw("runtime: blocked write on closing polldesc") throw("runtime: blocked write on closing polldesc")
} }
rg := pd.rg.Load() rg := pd.rg.Load()
if rg != 0 && rg != pdReady { if rg != pdNil && rg != pdReady {
throw("runtime: blocked read on closing polldesc") throw("runtime: blocked read on closing polldesc")
} }
netpollclose(pd.fd) netpollclose(pd.fd)
@ -280,9 +281,9 @@ func poll_runtime_pollReset(pd *pollDesc, mode int) int {
return errcode return errcode
} }
if mode == 'r' { if mode == 'r' {
pd.rg.Store(0) pd.rg.Store(pdNil)
} else if mode == 'w' { } else if mode == 'w' {
pd.wg.Store(0) pd.wg.Store(pdNil)
} }
return pollNoError return pollNoError
} }
@ -505,16 +506,16 @@ func netpollblock(pd *pollDesc, mode int32, waitio bool) bool {
// set the gpp semaphore to pdWait // set the gpp semaphore to pdWait
for { for {
// Consume notification if already ready. // Consume notification if already ready.
if gpp.CompareAndSwap(pdReady, 0) { if gpp.CompareAndSwap(pdReady, pdNil) {
return true return true
} }
if gpp.CompareAndSwap(0, pdWait) { if gpp.CompareAndSwap(pdNil, pdWait) {
break break
} }
// Double check that this isn't corrupt; otherwise we'd loop // Double check that this isn't corrupt; otherwise we'd loop
// forever. // forever.
if v := gpp.Load(); v != pdReady && v != 0 { if v := gpp.Load(); v != pdReady && v != pdNil {
throw("runtime: double wait") throw("runtime: double wait")
} }
} }
@ -526,7 +527,7 @@ func netpollblock(pd *pollDesc, mode int32, waitio bool) bool {
gopark(netpollblockcommit, unsafe.Pointer(gpp), waitReasonIOWait, traceEvGoBlockNet, 5) gopark(netpollblockcommit, unsafe.Pointer(gpp), waitReasonIOWait, traceEvGoBlockNet, 5)
} }
// be careful to not lose concurrent pdReady notification // be careful to not lose concurrent pdReady notification
old := gpp.Swap(0) old := gpp.Swap(pdNil)
if old > pdWait { if old > pdWait {
throw("runtime: corrupted polldesc") throw("runtime: corrupted polldesc")
} }
@ -544,7 +545,7 @@ func netpollunblock(pd *pollDesc, mode int32, ioready bool) *g {
if old == pdReady { if old == pdReady {
return nil return nil
} }
if old == 0 && !ioready { if old == pdNil && !ioready {
// Only set pdReady for ioready. runtime_pollWait // Only set pdReady for ioready. runtime_pollWait
// will check for timeout/cancel before waiting. // will check for timeout/cancel before waiting.
return nil return nil
@ -555,7 +556,7 @@ func netpollunblock(pd *pollDesc, mode int32, ioready bool) *g {
} }
if gpp.CompareAndSwap(old, new) { if gpp.CompareAndSwap(old, new) {
if old == pdWait { if old == pdWait {
old = 0 old = pdNil
} }
return (*g)(unsafe.Pointer(old)) return (*g)(unsafe.Pointer(old))
} }