Skip to content

Commit 7081629

Browse files
committed
the front waiter should try to notify waiters
1 parent cd5d95a commit 7081629

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

semaphore/semaphore.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ func (s *Weighted) Acquire(ctx context.Context, n int64) error {
6767
// fix up the queue, just pretend we didn't notice the cancelation.
6868
err = nil
6969
default:
70+
isFront := s.waiters.Front() == elem
7071
s.waiters.Remove(elem)
72+
if isFront && s.size > s.cur {
73+
s.notifyWaiters()
74+
}
7175
}
7276
s.mu.Unlock()
7377
return err
@@ -97,6 +101,11 @@ func (s *Weighted) Release(n int64) {
97101
s.mu.Unlock()
98102
panic("semaphore: released more than held")
99103
}
104+
s.notifyWaiters()
105+
s.mu.Unlock()
106+
}
107+
108+
func (s *Weighted) notifyWaiters() {
100109
for {
101110
next := s.waiters.Front()
102111
if next == nil {
@@ -123,5 +132,4 @@ func (s *Weighted) Release(n int64) {
123132
s.waiters.Remove(next)
124133
close(w.ready)
125134
}
126-
s.mu.Unlock()
127135
}

0 commit comments

Comments
 (0)