Skip to content

Commit

Permalink
[semaphore] Add TryAcquireAll function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Dubsky committed Oct 1, 2022
1 parent 8fcdb60 commit d16bec7
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions semaphore/semaphore.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ func (s *Weighted) TryAcquire(n int64) bool {
return success
}

// TryAcquireAll acquires all free weight from the semaphore without blocking.
// Returns the weight acquired. Returns 0 and leaves the semaphore unchanged if
// no weight was available.
func (s *Weighted) TryAcquireAll() int64 {
s.mu.Lock()
defer s.mu.Unlock()

// When somebody waits for a token there are obviously not enough tokens
// for waiters already waiting. Consequently, there are no free tokens.
if s.waiters.Len() > 0 {
return 0
}

free := s.size - s.cur
s.cur = s.size
return free
}

// Release releases the semaphore with a weight of n.
func (s *Weighted) Release(n int64) {
s.mu.Lock()
Expand Down

0 comments on commit d16bec7

Please sign in to comment.