Lock for writing, waiting up to 'timeout' for successful exclusive acquisition of the lock.
(timeout time.Duration)
| 81 | // Lock for writing, waiting up to 'timeout' for successful exclusive |
| 82 | // acquisition of the lock. |
| 83 | func (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 | // |