1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
# thread.rb - thread support classes
|
2000-05-01 05:42:38 -04:00
|
|
|
# by Yukihiro Matsumoto <matz@netlab.co.jp>
|
|
|
|
#
|
2001-05-16 05:05:54 -04:00
|
|
|
# Copyright (C) 2001 Yukihiro Matsumoto
|
2000-05-01 05:42:38 -04:00
|
|
|
# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 00:53:16 -04:00
|
|
|
# Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
unless defined? Thread
|
2005-12-29 07:05:16 -05:00
|
|
|
raise "Thread not available for this ruby interpreter"
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? ThreadError
|
2006-12-31 10:02:22 -05:00
|
|
|
class ThreadError < StandardError
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if $DEBUG
|
|
|
|
Thread.abort_on_exception = true
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
# ConditionVariable objects augment class Mutex. Using condition variables,
|
2003-01-17 04:30:11 -05:00
|
|
|
# it is possible to suspend while in the middle of a critical section until a
|
2005-06-07 05:41:17 -04:00
|
|
|
# resource becomes available.
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# require 'thread'
|
|
|
|
#
|
|
|
|
# mutex = Mutex.new
|
|
|
|
# resource = ConditionVariable.new
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-01-17 04:30:11 -05:00
|
|
|
# a = Thread.new {
|
|
|
|
# mutex.synchronize {
|
|
|
|
# # Thread 'a' now needs the resource
|
|
|
|
# resource.wait(mutex)
|
|
|
|
# # 'a' can now have the resource
|
|
|
|
# }
|
|
|
|
# }
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-01-17 04:30:11 -05:00
|
|
|
# b = Thread.new {
|
|
|
|
# mutex.synchronize {
|
|
|
|
# # Thread 'b' has finished using the resource
|
|
|
|
# resource.signal
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
class ConditionVariable
|
2005-06-07 05:41:17 -04:00
|
|
|
#
|
|
|
|
# Creates a new ConditionVariable
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def initialize
|
|
|
|
@waiters = []
|
2007-02-24 02:01:18 -05:00
|
|
|
@waiters_mutex = Mutex.new
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Releases the lock held in +mutex+ and waits; reacquires the lock on wakeup.
|
|
|
|
#
|
2009-09-23 10:01:07 -04:00
|
|
|
# If +timeout+ is given, this method returns after +timeout+ seconds passed,
|
|
|
|
# even if no other thread doesn't signal.
|
|
|
|
#
|
|
|
|
def wait(mutex, timeout=nil)
|
2004-05-27 03:43:38 -04:00
|
|
|
begin
|
2006-12-31 10:02:22 -05:00
|
|
|
# TODO: mutex should not be used
|
2007-02-24 02:01:18 -05:00
|
|
|
@waiters_mutex.synchronize do
|
|
|
|
@waiters.push(Thread.current)
|
|
|
|
end
|
2009-09-23 10:01:07 -04:00
|
|
|
mutex.sleep timeout
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Wakes up the first thread in line waiting for this lock.
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def signal
|
2000-03-17 03:58:21 -05:00
|
|
|
begin
|
2007-02-24 02:01:18 -05:00
|
|
|
t = @waiters_mutex.synchronize { @waiters.shift }
|
2000-03-17 03:58:21 -05:00
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Wakes up all threads waiting for this lock.
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def broadcast
|
2006-12-31 10:02:22 -05:00
|
|
|
# TODO: imcomplete
|
1999-08-13 01:45:20 -04:00
|
|
|
waiters0 = nil
|
2007-02-24 02:01:18 -05:00
|
|
|
@waiters_mutex.synchronize do
|
1999-08-13 01:45:20 -04:00
|
|
|
waiters0 = @waiters.dup
|
1999-01-19 23:59:39 -05:00
|
|
|
@waiters.clear
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
|
|
|
for t in waiters0
|
2000-03-17 03:58:21 -05:00
|
|
|
begin
|
|
|
|
t.run
|
|
|
|
rescue ThreadError
|
|
|
|
end
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
# This class provides a way to synchronize communication between threads.
|
|
|
|
#
|
|
|
|
# Example:
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
# require 'thread'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
# queue = Queue.new
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
# producer = Thread.new do
|
|
|
|
# 5.times do |i|
|
|
|
|
# sleep rand(i) # simulate expense
|
|
|
|
# queue << i
|
|
|
|
# puts "#{i} produced"
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
# consumer = Thread.new do
|
|
|
|
# 5.times do |i|
|
|
|
|
# value = queue.pop
|
|
|
|
# sleep rand(i/2) # simulate expense
|
|
|
|
# puts "consumed #{value}"
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
# consumer.join
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
class Queue
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Creates a new queue.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def initialize
|
|
|
|
@que = []
|
|
|
|
@waiting = []
|
1999-01-19 23:59:39 -05:00
|
|
|
@que.taint # enable tainted comunication
|
|
|
|
@waiting.taint
|
|
|
|
self.taint
|
2006-12-31 10:02:22 -05:00
|
|
|
@mutex = Mutex.new
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Pushes +obj+ to the queue.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def push(obj)
|
2006-12-31 10:02:22 -05:00
|
|
|
t = nil
|
|
|
|
@mutex.synchronize{
|
|
|
|
@que.push obj
|
|
|
|
begin
|
|
|
|
t = @waiting.shift
|
|
|
|
t.wakeup if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
}
|
2001-05-16 05:05:54 -04:00
|
|
|
begin
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
|
|
|
end
|
2000-05-30 00:24:17 -04:00
|
|
|
end
|
2005-06-07 05:41:17 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of push
|
|
|
|
#
|
2001-05-16 05:05:54 -04:00
|
|
|
alias << push
|
2005-06-07 05:41:17 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of push
|
|
|
|
#
|
2001-05-16 05:05:54 -04:00
|
|
|
alias enq push
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-01-17 04:30:11 -05: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 00:24:17 -04:00
|
|
|
def pop(non_block=false)
|
2006-12-31 10:02:22 -05:00
|
|
|
while true
|
|
|
|
@mutex.synchronize{
|
|
|
|
if @que.empty?
|
|
|
|
raise ThreadError, "queue empty" if non_block
|
|
|
|
@waiting.push Thread.current
|
|
|
|
@mutex.sleep
|
|
|
|
else
|
|
|
|
return @que.shift
|
|
|
|
end
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2005-06-07 05:41:17 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of pop
|
|
|
|
#
|
2001-05-16 05:05:54 -04:00
|
|
|
alias shift pop
|
2005-06-07 05:41:17 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of pop
|
|
|
|
#
|
2001-05-16 05:05:54 -04:00
|
|
|
alias deq pop
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
2006-12-31 10:02:22 -05:00
|
|
|
# Returns +true+ if the queue is empty.
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def empty?
|
2001-05-16 05:05:54 -04:00
|
|
|
@que.empty?
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Removes all objects from the queue.
|
|
|
|
#
|
1999-08-13 01:45:20 -04:00
|
|
|
def clear
|
2001-05-16 05:05:54 -04:00
|
|
|
@que.clear
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Returns the length of the queue.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def length
|
|
|
|
@que.length
|
|
|
|
end
|
2003-01-17 04:30:11 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of length.
|
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
alias size length
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Returns the number of threads waiting on the queue.
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def num_waiting
|
|
|
|
@waiting.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
# This class represents queues of specified size capacity. The push operation
|
2003-01-17 04:30:11 -05:00
|
|
|
# may be blocked if the capacity is full.
|
|
|
|
#
|
2005-06-07 05:41:17 -04:00
|
|
|
# See Queue for an example of how a SizedQueue works.
|
|
|
|
#
|
2006-12-31 10:02:22 -05:00
|
|
|
class SizedQueue < Queue
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Creates a fixed-length queue with a maximum size of +max+.
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def initialize(max)
|
2002-05-29 01:20:39 -04:00
|
|
|
raise ArgumentError, "queue size must be positive" unless max > 0
|
1999-01-19 23:59:39 -05:00
|
|
|
@max = max
|
|
|
|
@queue_wait = []
|
|
|
|
@queue_wait.taint # enable tainted comunication
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Returns the maximum size of the queue.
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def max
|
|
|
|
@max
|
|
|
|
end
|
|
|
|
|
2003-01-17 04:30:11 -05:00
|
|
|
#
|
|
|
|
# Sets the maximum size of the queue.
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def max=(max)
|
2006-12-31 10:02:22 -05:00
|
|
|
diff = nil
|
|
|
|
@mutex.synchronize {
|
|
|
|
if max <= @max
|
|
|
|
@max = max
|
|
|
|
else
|
|
|
|
diff = max - @max
|
|
|
|
@max = max
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if diff
|
1999-01-19 23:59:39 -05:00
|
|
|
diff.times do
|
2000-03-17 03:58:21 -05:00
|
|
|
begin
|
|
|
|
t = @queue_wait.shift
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
max
|
|
|
|
end
|
|
|
|
|
2005-06-07 05:41:17 -04:00
|
|
|
#
|
|
|
|
# Pushes +obj+ to the queue. If there is no space left in the queue, waits
|
|
|
|
# until space becomes available.
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def push(obj)
|
2006-12-31 10:02:22 -05:00
|
|
|
t = nil
|
|
|
|
@mutex.synchronize{
|
|
|
|
while true
|
2009-03-04 07:23:37 -05:00
|
|
|
break if @que.length < @max
|
2006-12-31 10:02:22 -05:00
|
|
|
@queue_wait.push Thread.current
|
|
|
|
@mutex.sleep
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
@que.push obj
|
|
|
|
begin
|
|
|
|
t = @waiting.shift
|
|
|
|
t.wakeup if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
begin
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
end
|
2005-06-07 05:41:17 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of push
|
|
|
|
#
|
2001-05-16 05:05:54 -04:00
|
|
|
alias << push
|
2005-06-07 05:41:17 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of push
|
|
|
|
#
|
2002-08-27 04:31:08 -04:00
|
|
|
alias enq push
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2005-06-07 05:41:17 -04:00
|
|
|
#
|
|
|
|
# Retrieves data from the queue and runs a waiting thread, if any.
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def pop(*args)
|
2001-05-16 05:05:54 -04:00
|
|
|
retval = super
|
2006-12-31 10:02:22 -05:00
|
|
|
t = nil
|
|
|
|
@mutex.synchronize {
|
|
|
|
if @que.length < @max
|
|
|
|
begin
|
|
|
|
t = @queue_wait.shift
|
|
|
|
t.wakeup if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
2001-05-16 05:05:54 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
|
|
|
begin
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2001-05-16 05:05:54 -04:00
|
|
|
retval
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2005-06-07 05:41:17 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of pop
|
|
|
|
#
|
2002-08-27 04:31:08 -04:00
|
|
|
alias shift pop
|
2005-06-07 05:41:17 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of pop
|
|
|
|
#
|
2002-08-27 04:31:08 -04:00
|
|
|
alias deq pop
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2005-06-07 05:41:17 -04:00
|
|
|
#
|
|
|
|
# Returns the number of threads waiting on the queue.
|
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
def num_waiting
|
|
|
|
@waiting.size + @queue_wait.size
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2003-01-17 04:30:11 -05:00
|
|
|
|
|
|
|
# Documentation comments:
|
|
|
|
# - How do you make RDoc inherit documentation from superclass?
|