1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
# thread.rb - thread support classes
|
|
|
|
# $Date$
|
2000-05-01 13:42:38 +04:00
|
|
|
# by Yukihiro Matsumoto <matz@netlab.co.jp>
|
|
|
|
#
|
2001-05-16 13:05:54 +04:00
|
|
|
# Copyright (C) 2001 Yukihiro Matsumoto
|
2000-05-01 13:42:38 +04:00
|
|
|
# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 08:53:16 +04:00
|
|
|
# Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
|
|
|
|
unless defined? Thread
|
|
|
|
fail "Thread not available for this ruby interpreter"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? ThreadError
|
1999-01-20 07:59:39 +03:00
|
|
|
class ThreadError<StandardError
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if $DEBUG
|
|
|
|
Thread.abort_on_exception = true
|
|
|
|
end
|
|
|
|
|
2003-11-16 04:53:12 +03:00
|
|
|
class Thread
|
|
|
|
#
|
|
|
|
# FIXME: not documented in Pickaxe or Nutshell.
|
|
|
|
#
|
|
|
|
def Thread.exclusive
|
|
|
|
_old = Thread.critical
|
|
|
|
begin
|
|
|
|
Thread.critical = true
|
|
|
|
return yield
|
|
|
|
ensure
|
|
|
|
Thread.critical = _old
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# +Mutex+ implements a simple semaphore that can be used to coordinate access to
|
|
|
|
# shared data from multiple concurrent threads.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# require 'thread'
|
|
|
|
# semaphore = Mutex.new
|
|
|
|
#
|
|
|
|
# a = Thread.new {
|
|
|
|
# semaphore.synchronize {
|
|
|
|
# # access shared resource
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# b = Thread.new {
|
|
|
|
# semaphore.synchronize {
|
|
|
|
# # access shared resource
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
class Mutex
|
|
|
|
def initialize
|
|
|
|
@waiting = []
|
1999-01-20 07:59:39 +03:00
|
|
|
@locked = false;
|
|
|
|
@waiting.taint # enable tainted comunication
|
|
|
|
self.taint
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Returns +true+ if this lock is currently held by some thread.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def locked?
|
2004-07-14 18:51:42 +04:00
|
|
|
@locked && true
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Attempts to obtain the lock and returns immediately. Returns +true+ if the
|
|
|
|
# lock was granted.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def try_lock
|
1999-01-20 07:59:39 +03:00
|
|
|
result = false
|
|
|
|
Thread.critical = true
|
1998-01-16 15:13:05 +03:00
|
|
|
unless @locked
|
2004-07-14 18:51:42 +04:00
|
|
|
@locked = Thread.current
|
1999-01-20 07:59:39 +03:00
|
|
|
result = true
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
Thread.critical = false
|
1998-01-16 15:13:05 +03:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Attempts to grab the lock and waits if it isn't available.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def lock
|
1999-01-20 07:59:39 +03:00
|
|
|
while (Thread.critical = true; @locked)
|
2004-07-14 18:51:42 +04:00
|
|
|
if @locked == Thread.current
|
|
|
|
raise ThreadError, "deadlock; recursive locking"
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
@waiting.push Thread.current
|
|
|
|
Thread.stop
|
|
|
|
end
|
2004-07-14 18:51:42 +04:00
|
|
|
@locked = Thread.current
|
1999-01-20 07:59:39 +03:00
|
|
|
Thread.critical = false
|
1998-01-16 15:13:05 +03:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Releases the lock. Returns +nil+ if ref wasn't locked.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def unlock
|
|
|
|
return unless @locked
|
1999-08-13 09:45:20 +04:00
|
|
|
Thread.critical = true
|
|
|
|
@locked = false
|
2000-03-17 11:58:21 +03:00
|
|
|
begin
|
|
|
|
t = @waiting.shift
|
|
|
|
t.wakeup if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
Thread.critical = false
|
2001-05-16 13:05:54 +04:00
|
|
|
begin
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Obtains a lock, runs the block, and releases the lock when the block
|
|
|
|
# completes. See the example under +Mutex+.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def synchronize
|
1999-01-20 07:59:39 +03:00
|
|
|
lock
|
1998-01-16 15:13:05 +03:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
unlock
|
|
|
|
end
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# FIXME: not documented in Pickaxe/Nutshell.
|
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
def exclusive_unlock
|
|
|
|
return unless @locked
|
|
|
|
Thread.exclusive do
|
|
|
|
@locked = false
|
2000-03-17 11:58:21 +03:00
|
|
|
begin
|
|
|
|
t = @waiting.shift
|
|
|
|
t.wakeup if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# +ConditionVariable+ objects augment class +Mutex+. Using condition variables,
|
|
|
|
# it is possible to suspend while in the middle of a critical section until a
|
|
|
|
# resource becomes available (see the discussion on page 117).
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# require 'thread'
|
|
|
|
#
|
|
|
|
# mutex = Mutex.new
|
|
|
|
# resource = ConditionVariable.new
|
|
|
|
#
|
|
|
|
# a = Thread.new {
|
|
|
|
# mutex.synchronize {
|
|
|
|
# # Thread 'a' now needs the resource
|
|
|
|
# resource.wait(mutex)
|
|
|
|
# # 'a' can now have the resource
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# b = Thread.new {
|
|
|
|
# mutex.synchronize {
|
|
|
|
# # Thread 'b' has finished using the resource
|
|
|
|
# resource.signal
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
class ConditionVariable
|
|
|
|
def initialize
|
|
|
|
@waiters = []
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Releases the lock held in +mutex+ and waits; reacquires the lock on wakeup.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def wait(mutex)
|
2004-05-27 11:43:38 +04:00
|
|
|
begin
|
|
|
|
mutex.exclusive_unlock do
|
|
|
|
@waiters.push(Thread.current)
|
|
|
|
Thread.stop
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
mutex.lock
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Wakes up the first thread in line waiting for this lock.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def signal
|
2000-03-17 11:58:21 +03:00
|
|
|
begin
|
|
|
|
t = @waiters.shift
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Wakes up all threads waiting for this lock.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def broadcast
|
1999-08-13 09:45:20 +04:00
|
|
|
waiters0 = nil
|
|
|
|
Thread.exclusive do
|
|
|
|
waiters0 = @waiters.dup
|
1999-01-20 07:59:39 +03:00
|
|
|
@waiters.clear
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
|
|
|
for t in waiters0
|
2000-03-17 11:58:21 +03:00
|
|
|
begin
|
|
|
|
t.run
|
|
|
|
rescue ThreadError
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# This class provides a way to communicate data between threads.
|
|
|
|
#
|
|
|
|
# TODO: an example (code or English) would really help here. How do you set up
|
|
|
|
# a queue between two threads?
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
class Queue
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Creates a new queue.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def initialize
|
|
|
|
@que = []
|
|
|
|
@waiting = []
|
1999-01-20 07:59:39 +03:00
|
|
|
@que.taint # enable tainted comunication
|
|
|
|
@waiting.taint
|
|
|
|
self.taint
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Pushes +obj+ to the queue.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def push(obj)
|
1999-01-20 07:59:39 +03:00
|
|
|
Thread.critical = true
|
1998-01-16 15:13:05 +03:00
|
|
|
@que.push obj
|
2000-03-17 11:58:21 +03:00
|
|
|
begin
|
|
|
|
t = @waiting.shift
|
|
|
|
t.wakeup if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
2000-05-12 13:07:57 +04:00
|
|
|
ensure
|
|
|
|
Thread.critical = false
|
2000-03-17 11:58:21 +03:00
|
|
|
end
|
2001-05-16 13:05:54 +04:00
|
|
|
begin
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
|
|
|
end
|
2000-05-30 08:24:17 +04:00
|
|
|
end
|
2001-05-16 13:05:54 +04:00
|
|
|
alias << push
|
|
|
|
alias enq push
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Retrieves data from the queue. If the queue is empty, the calling thread is
|
|
|
|
# suspended until data is pushed onto the queue. If +non_block+ is true, the
|
|
|
|
# thread isn't suspended, and an exception is raised.
|
|
|
|
#
|
2000-05-30 08:24:17 +04:00
|
|
|
def pop(non_block=false)
|
2002-06-06 06:43:02 +04:00
|
|
|
while (Thread.critical = true; @que.empty?)
|
|
|
|
raise ThreadError, "queue empty" if non_block
|
|
|
|
@waiting.push Thread.current
|
|
|
|
Thread.stop
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2002-06-06 06:43:02 +04:00
|
|
|
@que.shift
|
|
|
|
ensure
|
|
|
|
Thread.critical = false
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2001-05-16 13:05:54 +04:00
|
|
|
alias shift pop
|
|
|
|
alias deq pop
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Returns +true+ is the queue is empty.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def empty?
|
2001-05-16 13:05:54 +04:00
|
|
|
@que.empty?
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Removes all objects from the queue.
|
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
def clear
|
2001-05-16 13:05:54 +04:00
|
|
|
@que.clear
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Returns the length of the queue.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def length
|
|
|
|
@que.length
|
|
|
|
end
|
2003-01-17 12:30:11 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of length.
|
|
|
|
#
|
2000-05-30 08:24:17 +04:00
|
|
|
def size
|
|
|
|
length
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Returns the number of threads waiting on the queue.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def num_waiting
|
|
|
|
@waiting.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# This class represents queues of specified size capacity. The +push+ operation
|
|
|
|
# may be blocked if the capacity is full.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
class SizedQueue<Queue
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Creates a fixed-length queue with a maximum size of +max+.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def initialize(max)
|
2002-05-29 09:20:39 +04:00
|
|
|
raise ArgumentError, "queue size must be positive" unless max > 0
|
1999-01-20 07:59:39 +03:00
|
|
|
@max = max
|
|
|
|
@queue_wait = []
|
|
|
|
@queue_wait.taint # enable tainted comunication
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Returns the maximum size of the queue.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def max
|
|
|
|
@max
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Sets the maximum size of the queue.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def max=(max)
|
1999-08-13 09:45:20 +04:00
|
|
|
Thread.critical = true
|
2001-05-16 13:05:54 +04:00
|
|
|
if max <= @max
|
1999-01-20 07:59:39 +03:00
|
|
|
@max = max
|
1999-08-13 09:45:20 +04:00
|
|
|
Thread.critical = false
|
1999-01-20 07:59:39 +03:00
|
|
|
else
|
|
|
|
diff = max - @max
|
|
|
|
@max = max
|
1999-08-13 09:45:20 +04:00
|
|
|
Thread.critical = false
|
1999-01-20 07:59:39 +03:00
|
|
|
diff.times do
|
2000-03-17 11:58:21 +03:00
|
|
|
begin
|
|
|
|
t = @queue_wait.shift
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
max
|
|
|
|
end
|
|
|
|
|
|
|
|
def push(obj)
|
|
|
|
Thread.critical = true
|
|
|
|
while @que.length >= @max
|
|
|
|
@queue_wait.push Thread.current
|
|
|
|
Thread.stop
|
|
|
|
Thread.critical = true
|
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
2001-05-16 13:05:54 +04:00
|
|
|
alias << push
|
2002-08-27 12:31:08 +04:00
|
|
|
alias enq push
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
def pop(*args)
|
2001-05-16 13:05:54 +04:00
|
|
|
retval = super
|
1999-01-20 07:59:39 +03:00
|
|
|
Thread.critical = true
|
|
|
|
if @que.length < @max
|
2000-03-17 11:58:21 +03:00
|
|
|
begin
|
|
|
|
t = @queue_wait.shift
|
2000-05-12 13:07:57 +04:00
|
|
|
t.wakeup if t
|
2000-03-17 11:58:21 +03:00
|
|
|
rescue ThreadError
|
|
|
|
retry
|
2000-05-12 13:07:57 +04:00
|
|
|
ensure
|
|
|
|
Thread.critical = false
|
2000-03-17 11:58:21 +03:00
|
|
|
end
|
2001-05-16 13:05:54 +04:00
|
|
|
begin
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2001-05-16 13:05:54 +04:00
|
|
|
retval
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2002-08-27 12:31:08 +04:00
|
|
|
alias shift pop
|
|
|
|
alias deq pop
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
def num_waiting
|
|
|
|
@waiting.size + @queue_wait.size
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2003-01-17 12:30:11 +03:00
|
|
|
|
|
|
|
# Documentation comments:
|
|
|
|
# - SizedQueue #push and #pop deserve some documentation, as they are different
|
|
|
|
# from the Queue implementations.
|
|
|
|
# - Some methods are not documented in Pickaxe/Nutshell, and are therefore not
|
|
|
|
# documented here. See FIXME notes.
|
|
|
|
# - Reference to Pickaxe page numbers should be replaced with either a section
|
|
|
|
# name or a summary.
|
|
|
|
# - How do you document aliases?
|
|
|
|
# - How do you make RDoc inherit documentation from superclass?
|