diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-02-26 07:43:11 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-02-26 07:43:11 +0000 |
commit | eeb804f153eff4bde9fc528414bfd9c8c8e343a1 (patch) | |
tree | 07f08eec5e3161b54324f0bdbe38f9ac4ed42f1c /lib/thread.rb | |
parent | ba2d46829afada0ecc878df16b04078b13c6cfd9 (diff) |
*** empty log message ***
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@92 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/thread.rb')
-rw-r--r-- | lib/thread.rb | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/lib/thread.rb b/lib/thread.rb index 5262ec539d..c33333f5f3 100644 --- a/lib/thread.rb +++ b/lib/thread.rb @@ -69,9 +69,37 @@ class Mutex unlock end end +end - def num_waiting - @waiting.size +class ConditionVariable + def initialize + @waiters = [] + @waiters_mutex = Mutex.new + end + + def wait(mutex) + mutex.unlock + @waiters_mutex.synchronize { + @waiters.push(Thread.current) + } + Thread.stop + mutex.lock + end + + def signal + @waiters_mutex.synchronize { + t = @waiters.shift + t.run if t + } + end + + def broadcast + @waiters_mutex.synchronize { + for t in @waiters + t.run + end + @waiters.clear + } end end @@ -116,6 +144,11 @@ class Queue @que.length end alias size length + + + def num_waiting + @waiting.size + end end class SizedQueue<Queue @@ -130,19 +163,20 @@ class SizedQueue<Queue end def max=(max) + Thread.critical = TRUE if @max >= max @max = max + Thread.critical = FALSE else - Thread.critical = TRUE diff = max - @max @max = max + Thread.critical = FALSE diff.times do t = @queue_wait.shift t.run if t end - Thread.critical = FALSE - @max end + max end def push(obj) @@ -150,6 +184,7 @@ class SizedQueue<Queue while @que.length >= @max @queue_wait.push Thread.current Thread.stop + Thread.critical = true end super end @@ -160,8 +195,7 @@ class SizedQueue<Queue t = @queue_wait.shift t.run if t end - pop = super - pop + super end def num_waiting |