// goroutines respectively. The semaphore can be in the following states:
//
// 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;
// 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 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;
-// 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.
-// nil - none of the above.
+// pdNil - none of the above.
const (
+ pdNil uintptr = 0
pdReady uintptr = 1
pdWait uintptr = 2
)
// rg, wg are accessed atomically and hold g pointers.
// (Using atomic.Uintptr here is similar to using guintptr elsewhere.)
- rg atomic.Uintptr // pdReady, pdWait, G waiting for read or nil
- wg atomic.Uintptr // pdReady, pdWait, G waiting for write or nil
+ rg atomic.Uintptr // pdReady, pdWait, G waiting for read or pdNil
+ wg atomic.Uintptr // pdReady, pdWait, G waiting for write or pdNil
lock mutex // protects the following fields
closing bool
pd := pollcache.alloc()
lock(&pd.lock)
wg := pd.wg.Load()
- if wg != 0 && wg != pdReady {
+ if wg != pdNil && wg != pdReady {
throw("runtime: blocked write on free polldesc")
}
rg := pd.rg.Load()
- if rg != 0 && rg != pdReady {
+ if rg != pdNil && rg != pdReady {
throw("runtime: blocked read on free polldesc")
}
pd.fd = fd
pd.closing = false
pd.setEventErr(false)
pd.rseq++
- pd.rg.Store(0)
+ pd.rg.Store(pdNil)
pd.rd = 0
pd.wseq++
- pd.wg.Store(0)
+ pd.wg.Store(pdNil)
pd.wd = 0
pd.self = pd
pd.publishInfo()
throw("runtime: close polldesc w/o unblock")
}
wg := pd.wg.Load()
- if wg != 0 && wg != pdReady {
+ if wg != pdNil && wg != pdReady {
throw("runtime: blocked write on closing polldesc")
}
rg := pd.rg.Load()
- if rg != 0 && rg != pdReady {
+ if rg != pdNil && rg != pdReady {
throw("runtime: blocked read on closing polldesc")
}
netpollclose(pd.fd)
return errcode
}
if mode == 'r' {
- pd.rg.Store(0)
+ pd.rg.Store(pdNil)
} else if mode == 'w' {
- pd.wg.Store(0)
+ pd.wg.Store(pdNil)
}
return pollNoError
}
// set the gpp semaphore to pdWait
for {
// Consume notification if already ready.
- if gpp.CompareAndSwap(pdReady, 0) {
+ if gpp.CompareAndSwap(pdReady, pdNil) {
return true
}
- if gpp.CompareAndSwap(0, pdWait) {
+ if gpp.CompareAndSwap(pdNil, pdWait) {
break
}
// Double check that this isn't corrupt; otherwise we'd loop
// forever.
- if v := gpp.Load(); v != pdReady && v != 0 {
+ if v := gpp.Load(); v != pdReady && v != pdNil {
throw("runtime: double wait")
}
}
gopark(netpollblockcommit, unsafe.Pointer(gpp), waitReasonIOWait, traceEvGoBlockNet, 5)
}
// be careful to not lose concurrent pdReady notification
- old := gpp.Swap(0)
+ old := gpp.Swap(pdNil)
if old > pdWait {
throw("runtime: corrupted polldesc")
}
if old == pdReady {
return nil
}
- if old == 0 && !ioready {
+ if old == pdNil && !ioready {
// Only set pdReady for ioready. runtime_pollWait
// will check for timeout/cancel before waiting.
return nil
}
if gpp.CompareAndSwap(old, new) {
if old == pdWait {
- old = 0
+ old = pdNil
}
return (*g)(unsafe.Pointer(old))
}