Skip to content

Commit d510574

Browse files
committed
prevent spurious wakeups on JRuby
1 parent 3bdd372 commit d510574

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/resque/consumer.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
module Resque
22
class Consumer
33
class Latch # :nodoc:
4-
def initialize
5-
@mutex = Mutex.new
6-
@cond = ConditionVariable.new
4+
def initialize(count = 1)
5+
@count = count
6+
@lock = Monitor.new
7+
@cv = @lock.new_cond
78
end
89

910
def release
10-
@mutex.synchronize { @cond.broadcast }
11+
@lock.synchronize do
12+
@count -= 1 if @count > 0
13+
@cv.broadcast if @count.zero?
14+
end
1115
end
1216

1317
def await
14-
@mutex.synchronize { @cond.wait @mutex }
18+
@lock.synchronize do
19+
@cv.wait_while { @count > 0 }
20+
end
1521
end
1622
end
1723

0 commit comments

Comments
 (0)