mirror of
https://github.com/lloeki/normandy.git
synced 2025-12-06 10:04:39 +01:00
improve waitgroup
This commit is contained in:
parent
aa3bb21334
commit
0bbee7df48
2 changed files with 25 additions and 21 deletions
|
|
@ -5,16 +5,19 @@ class WaitGroup
|
|||
@waiting = []
|
||||
end
|
||||
|
||||
def add(count)
|
||||
sync! { @count += count }
|
||||
def add(delta)
|
||||
sync! do
|
||||
@count += delta
|
||||
fail 'negative WaitGroup counter' if @count < 0
|
||||
if @waiting.any? && delta > 0 && @count == delta
|
||||
fail 'misuse: add called concurrently with wait'
|
||||
end
|
||||
wake!
|
||||
end
|
||||
end
|
||||
|
||||
def done
|
||||
sync! do
|
||||
fail 'negative count' if done?
|
||||
@count -= 1
|
||||
wake!
|
||||
end
|
||||
add(-1)
|
||||
end
|
||||
|
||||
private def done?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue