MCPcopy
hub / github.com/dropbox/godropbox / WLock

Method WLock

sync2/boundedrwlock.go:83–118  ·  view source on GitHub ↗

Lock for writing, waiting up to 'timeout' for successful exclusive acquisition of the lock.

(timeout time.Duration)

Source from the content-addressed store, hash-verified

81// Lock for writing, waiting up to 'timeout' for successful exclusive
82// acquisition of the lock.
83func (rw *BoundedRWLock) WLock(timeout time.Duration) (err error) {
84 deadline := time.After(timeout)
85 rw.control.Lock()
86 if rw.readers != 0 || rw.nextWriter != nil {
87 me := newWait(true)
88 if rw.nextWriter == nil {
89 rw.nextWriter = me
90 } else {
91 select {
92 case rw.waiters <- me:
93 default:
94 err = errors.New("Waiter capacity reached in WLock")
95 }
96 }
97 rw.control.Unlock()
98 if err != nil {
99 return
100 }
101
102 woken := me.WaitAtomic(deadline)
103 if !woken {
104 return errors.New("Waiter timeout")
105 }
106 rw.control.Lock()
107 if rw.readers != 0 {
108 panic("readers??")
109 }
110 if rw.nextWriter != me {
111 panic("not me??")
112 }
113 } else {
114 rw.nextWriter = newWait(true)
115 }
116 rw.control.Unlock()
117 return
118}
119
120// Unlock the write lock.
121//

Callers 3

TestWriterTimeoutMethod · 0.95
TestReaderTimeoutMethod · 0.95
stressRWMutexFunction · 0.95

Calls 6

NewFunction · 0.92
newWaitFunction · 0.85
WaitAtomicMethod · 0.80
AfterMethod · 0.65
LockMethod · 0.65
UnlockMethod · 0.65

Tested by 3

TestWriterTimeoutMethod · 0.76
TestReaderTimeoutMethod · 0.76
stressRWMutexFunction · 0.76