mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Initial revision
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
392296c12d
commit
3db12e8b23
225 changed files with 75935 additions and 0 deletions
110
lib/thread.rb
Normal file
110
lib/thread.rb
Normal file
|
@ -0,0 +1,110 @@
|
|||
#
|
||||
# thread.rb - thread support classes
|
||||
# $Date$
|
||||
# by Yukihiro Matsumoto <matz@caelum.co.jp>
|
||||
#
|
||||
|
||||
unless defined? Thread
|
||||
fail "Thread not available for this ruby interpreter"
|
||||
end
|
||||
|
||||
unless defined? ThreadError
|
||||
class ThreadError<Exception
|
||||
end
|
||||
end
|
||||
|
||||
class Mutex
|
||||
def initialize
|
||||
@waiting = []
|
||||
@locked = FALSE;
|
||||
end
|
||||
|
||||
def locked?
|
||||
@locked
|
||||
end
|
||||
|
||||
def try_lock
|
||||
result = FALSE
|
||||
Thread.critical = TRUE
|
||||
unless @locked
|
||||
@locked = TRUE
|
||||
result = TRUE
|
||||
end
|
||||
Thread.critical = FALSE
|
||||
result
|
||||
end
|
||||
|
||||
def lock
|
||||
while (Thread.critical = TRUE; @locked)
|
||||
@waiting.push Thread.current
|
||||
Thread.stop
|
||||
end
|
||||
@locked = TRUE
|
||||
Thread.critical = FALSE
|
||||
self
|
||||
end
|
||||
|
||||
def unlock
|
||||
return unless @locked
|
||||
Thread.critical = TRUE
|
||||
wait = @waiting
|
||||
@waiting = []
|
||||
@locked = FALSE
|
||||
Thread.critical = FALSE
|
||||
for w in wait
|
||||
w.run
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def synchronize
|
||||
begin
|
||||
lock
|
||||
yield
|
||||
ensure
|
||||
unlock
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Queue
|
||||
def initialize
|
||||
@que = []
|
||||
@waiting = []
|
||||
end
|
||||
|
||||
def push(obj)
|
||||
Thread.critical = TRUE
|
||||
@que.push obj
|
||||
t = @waiting.shift
|
||||
Thread.critical = FALSE
|
||||
t.run if t
|
||||
end
|
||||
|
||||
def pop non_block=FALSE
|
||||
item = nil
|
||||
until item
|
||||
Thread.critical = TRUE
|
||||
if @que.length == 0
|
||||
if non_block
|
||||
Thread.critical = FALSE
|
||||
raise ThreadError, "queue empty"
|
||||
end
|
||||
@waiting.push Thread.current
|
||||
Thread.stop
|
||||
else
|
||||
item = @que.shift
|
||||
end
|
||||
end
|
||||
Thread.critical = FALSE
|
||||
item
|
||||
end
|
||||
|
||||
def empty?
|
||||
@que.length == 0
|
||||
end
|
||||
|
||||
def length
|
||||
@que.length
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue