From d464704f111d211c1f1ff9ef23ef1d755054be00 Mon Sep 17 00:00:00 2001 From: shyouhei Date: Wed, 15 Aug 2007 19:08:43 +0000 Subject: add tag v1_8_5_54 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_8_5_54@12952 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ruby_1_8_5/lib/thread.rb | 484 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 484 insertions(+) create mode 100644 ruby_1_8_5/lib/thread.rb (limited to 'ruby_1_8_5/lib/thread.rb') diff --git a/ruby_1_8_5/lib/thread.rb b/ruby_1_8_5/lib/thread.rb new file mode 100644 index 0000000000..640bea5111 --- /dev/null +++ b/ruby_1_8_5/lib/thread.rb @@ -0,0 +1,484 @@ +# +# thread.rb - thread support classes +# $Date: 2005/06/07 09:41:17 $ +# by Yukihiro Matsumoto +# +# Copyright (C) 2001 Yukihiro Matsumoto +# Copyright (C) 2000 Network Applied Communication Laboratory, Inc. +# Copyright (C) 2000 Information-technology Promotion Agency, Japan +# + +unless defined? Thread + fail "Thread not available for this ruby interpreter" +end + +unless defined? ThreadError + class ThreadError 0 + @max = max + @queue_wait = [] + @queue_wait.taint # enable tainted comunication + super() + end + + # + # Returns the maximum size of the queue. + # + def max + @max + end + + # + # Sets the maximum size of the queue. + # + def max=(max) + Thread.critical = true + if max <= @max + @max = max + Thread.critical = false + else + diff = max - @max + @max = max + Thread.critical = false + diff.times do + begin + t = @queue_wait.shift + t.run if t + rescue ThreadError + retry + end + end + end + max + end + + # + # Pushes +obj+ to the queue. If there is no space left in the queue, waits + # until space becomes available. + # + def push(obj) + Thread.critical = true + while @que.length >= @max + @queue_wait.push Thread.current + Thread.stop + Thread.critical = true + end + super + end + + # + # Alias of push + # + alias << push + + # + # Alias of push + # + alias enq push + + # + # Retrieves data from the queue and runs a waiting thread, if any. + # + def pop(*args) + retval = super + Thread.critical = true + if @que.length < @max + begin + t = @queue_wait.shift + t.wakeup if t + rescue ThreadError + retry + ensure + Thread.critical = false + end + begin + t.run if t + rescue ThreadError + end + end + retval + end + + # + # Alias of pop + # + alias shift pop + + # + # Alias of pop + # + alias deq pop + + # + # Returns the number of threads waiting on the queue. + # + def num_waiting + @waiting.size + @queue_wait.size + end +end + +# Documentation comments: +# - How do you make RDoc inherit documentation from superclass? -- cgit v1.2.3