improve waitgroup

This commit is contained in:
Loic Nageleisen 2015-12-17 08:54:10 +01:00
parent aa3bb21334
commit 0bbee7df48
2 changed files with 25 additions and 21 deletions

View file

@ -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?