diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-09-11 07:29:56 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-09-11 07:29:56 +0000 |
commit | f2caa2d1ef10a79f755a511be3bcfcfed992a21b (patch) | |
tree | 2dfa6eb6dbd8091d4d711e9ea8eb8e919d7ae451 /lib | |
parent | ec35b84429a9446fb11f2bd81a90d90d768a5de1 (diff) |
* lib/thread.rb: Merge from 1.7: Get rid of race condition in
Queue#pop().
* lib/thread.rb: Merge from 1.7: SizedQueue: new(max) should not
accept a value <= 0.
* lib/thread.rb: Merge from 1.7: SizedQueue: Properly override
enq(), shift() and deq().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_6@2846 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/thread.rb | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/lib/thread.rb b/lib/thread.rb index 34db9c3d46..1a08aa7761 100644 --- a/lib/thread.rb +++ b/lib/thread.rb @@ -173,22 +173,14 @@ class Queue alias enq push def pop(non_block=false) - Thread.critical = true - begin - loop do - if @que.empty? - if non_block - raise ThreadError, "queue empty" - end - @waiting.push Thread.current - Thread.stop - else - return @que.shift - end - end - ensure - Thread.critical = false + while (Thread.critical = true; @que.empty?) + raise ThreadError, "queue empty" if non_block + @waiting.push Thread.current + Thread.stop end + @que.shift + ensure + Thread.critical = false end alias shift pop alias deq pop @@ -215,6 +207,7 @@ end class SizedQueue<Queue def initialize(max) + raise ArgumentError, "queue size must be positive" unless max > 0 @max = max @queue_wait = [] @queue_wait.taint # enable tainted comunication @@ -256,6 +249,7 @@ class SizedQueue<Queue super end alias << push + alias enq push def pop(*args) retval = super @@ -276,6 +270,8 @@ class SizedQueue<Queue end retval end + alias shift pop + alias deq pop def num_waiting @waiting.size + @queue_wait.size |