| |
| |
| |
|
|
| package poll |
|
|
| import "sync/atomic" |
|
|
| |
| |
| |
| type fdMutex struct { |
| state uint64 |
| rsema uint32 |
| wsema uint32 |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| const ( |
| mutexClosed = 1 << 0 |
| mutexRLock = 1 << 1 |
| mutexWLock = 1 << 2 |
| mutexRef = 1 << 3 |
| mutexRefMask = (1<<20 - 1) << 3 |
| mutexRWait = 1 << 23 |
| mutexRMask = (1<<20 - 1) << 23 |
| mutexWWait = 1 << 43 |
| mutexWMask = (1<<20 - 1) << 43 |
| ) |
|
|
| const overflowMsg = "too many concurrent operations on a single file or socket (max 1048575)" |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| func (mu *fdMutex) incref() bool { |
| for { |
| old := atomic.LoadUint64(&mu.state) |
| if old&mutexClosed != 0 { |
| return false |
| } |
| new := old + mutexRef |
| if new&mutexRefMask == 0 { |
| panic(overflowMsg) |
| } |
| if atomic.CompareAndSwapUint64(&mu.state, old, new) { |
| return true |
| } |
| } |
| } |
|
|
| |
| |
| func (mu *fdMutex) increfAndClose() bool { |
| for { |
| old := atomic.LoadUint64(&mu.state) |
| if old&mutexClosed != 0 { |
| return false |
| } |
| |
| new := (old | mutexClosed) + mutexRef |
| if new&mutexRefMask == 0 { |
| panic(overflowMsg) |
| } |
| |
| new &^= mutexRMask | mutexWMask |
| if atomic.CompareAndSwapUint64(&mu.state, old, new) { |
| |
| |
| for old&mutexRMask != 0 { |
| old -= mutexRWait |
| runtime_Semrelease(&mu.rsema) |
| } |
| for old&mutexWMask != 0 { |
| old -= mutexWWait |
| runtime_Semrelease(&mu.wsema) |
| } |
| return true |
| } |
| } |
| } |
|
|
| |
| |
| func (mu *fdMutex) decref() bool { |
| for { |
| old := atomic.LoadUint64(&mu.state) |
| if old&mutexRefMask == 0 { |
| panic("inconsistent poll.fdMutex") |
| } |
| new := old - mutexRef |
| if atomic.CompareAndSwapUint64(&mu.state, old, new) { |
| return new&(mutexClosed|mutexRefMask) == mutexClosed |
| } |
| } |
| } |
|
|
| |
| |
| func (mu *fdMutex) rwlock(read bool) bool { |
| var mutexBit, mutexWait, mutexMask uint64 |
| var mutexSema *uint32 |
| if read { |
| mutexBit = mutexRLock |
| mutexWait = mutexRWait |
| mutexMask = mutexRMask |
| mutexSema = &mu.rsema |
| } else { |
| mutexBit = mutexWLock |
| mutexWait = mutexWWait |
| mutexMask = mutexWMask |
| mutexSema = &mu.wsema |
| } |
| for { |
| old := atomic.LoadUint64(&mu.state) |
| if old&mutexClosed != 0 { |
| return false |
| } |
| var new uint64 |
| if old&mutexBit == 0 { |
| |
| new = (old | mutexBit) + mutexRef |
| if new&mutexRefMask == 0 { |
| panic(overflowMsg) |
| } |
| } else { |
| |
| new = old + mutexWait |
| if new&mutexMask == 0 { |
| panic(overflowMsg) |
| } |
| } |
| if atomic.CompareAndSwapUint64(&mu.state, old, new) { |
| if old&mutexBit == 0 { |
| return true |
| } |
| runtime_Semacquire(mutexSema) |
| |
| } |
| } |
| } |
|
|
| |
| |
| func (mu *fdMutex) rwunlock(read bool) bool { |
| var mutexBit, mutexWait, mutexMask uint64 |
| var mutexSema *uint32 |
| if read { |
| mutexBit = mutexRLock |
| mutexWait = mutexRWait |
| mutexMask = mutexRMask |
| mutexSema = &mu.rsema |
| } else { |
| mutexBit = mutexWLock |
| mutexWait = mutexWWait |
| mutexMask = mutexWMask |
| mutexSema = &mu.wsema |
| } |
| for { |
| old := atomic.LoadUint64(&mu.state) |
| if old&mutexBit == 0 || old&mutexRefMask == 0 { |
| panic("inconsistent poll.fdMutex") |
| } |
| |
| new := (old &^ mutexBit) - mutexRef |
| if old&mutexMask != 0 { |
| new -= mutexWait |
| } |
| if atomic.CompareAndSwapUint64(&mu.state, old, new) { |
| if old&mutexMask != 0 { |
| runtime_Semrelease(mutexSema) |
| } |
| return new&(mutexClosed|mutexRefMask) == mutexClosed |
| } |
| } |
| } |
|
|
| |
| func runtime_Semacquire(sema *uint32) |
| func runtime_Semrelease(sema *uint32) |
|
|
| |
| |
| func (fd *FD) incref() error { |
| if !fd.fdmu.incref() { |
| return errClosing(fd.isFile) |
| } |
| return nil |
| } |
|
|
| |
| |
| |
| func (fd *FD) decref() error { |
| if fd.fdmu.decref() { |
| return fd.destroy() |
| } |
| return nil |
| } |
|
|
| |
| |
| func (fd *FD) readLock() error { |
| if !fd.fdmu.rwlock(true) { |
| return errClosing(fd.isFile) |
| } |
| return nil |
| } |
|
|
| |
| |
| |
| func (fd *FD) readUnlock() { |
| if fd.fdmu.rwunlock(true) { |
| fd.destroy() |
| } |
| } |
|
|
| |
| |
| func (fd *FD) writeLock() error { |
| if !fd.fdmu.rwlock(false) { |
| return errClosing(fd.isFile) |
| } |
| return nil |
| } |
|
|
| |
| |
| |
| func (fd *FD) writeUnlock() { |
| if fd.fdmu.rwunlock(false) { |
| fd.destroy() |
| } |
| } |
|
|
| |
| |
| func (fd *FD) readWriteLock() error { |
| if !fd.fdmu.rwlock(true) || !fd.fdmu.rwlock(false) { |
| return errClosing(fd.isFile) |
| } |
| return nil |
| } |
|
|
| |
| |
| |
| func (fd *FD) readWriteUnlock() { |
| fd.fdmu.rwunlock(true) |
| fd.fdmu.rwunlock(false) |
| } |
|
|
| |
| func (fd *FD) closing() bool { |
| return atomic.LoadUint64(&fd.fdmu.state)&mutexClosed != 0 |
| } |
|
|