mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
ec35b84429
commit
f2caa2d1ef
2 changed files with 22 additions and 15 deletions
11
ChangeLog
11
ChangeLog
|
@ -1,3 +1,14 @@
|
|||
Wed Sep 11 16:24:22 2002 Akinori MUSHA <knu@iDaemons.org>
|
||||
|
||||
* 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().
|
||||
|
||||
Wed Sep 11 16:15:06 2002 Akinori MUSHA <knu@iDaemons.org>
|
||||
|
||||
* lib/tempfile.rb: Merge from 1.7: Add Tempfile#size.
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue