Created
July 28, 2010 10:08
-
-
Save nahi/493930 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: lib/thread.rb | |
=================================================================== | |
--- lib/thread.rb (リビジョン 28689) | |
+++ lib/thread.rb (作業コピー) | |
@@ -148,6 +148,7 @@ | |
@waiting.taint | |
self.taint | |
@mutex = Mutex.new | |
+ @relocked = ConditionVariable.new | |
end | |
# | |
@@ -158,7 +159,10 @@ | |
@que.push obj | |
begin | |
t = @waiting.shift | |
- t.wakeup if t | |
+ if t | |
+ t.wakeup | |
+ @relocked.wait(@mutex) | |
+ end | |
rescue ThreadError | |
retry | |
end | |
@@ -187,6 +191,7 @@ | |
raise ThreadError, "queue empty" if non_block | |
@waiting.push Thread.current | |
@mutex.sleep | |
+ @relocked.signal | |
else | |
return @que.shift | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Test for Queue fairness | |
# just testing - the fairness is not a spec at present. | |
create_waiter = lambda { |name, command| | |
t = Thread.new { | |
p [command.pop, name] | |
} | |
Thread.pass until t.status == 'sleep' | |
t | |
} | |
50.times do | |
command = Queue.new | |
# create 3 waiters, and... | |
create_waiter.call(:first, command) | |
create_waiter.call(:second, command) | |
create_waiter.call(:third, command) | |
# and pushes a command. | |
command.push(:go) | |
# then try to consume the command by a subsequent thread | |
Thread.new { | |
p [command.pop, :LATER] | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment