1998-01-16 15:13:05 +03:00
|
|
|
#
|
2011-05-19 04:07:25 +04:00
|
|
|
# thread.rb - thread support classes
|
|
|
|
# by Yukihiro Matsumoto <matz@netlab.co.jp>
|
2000-05-01 13:42:38 +04:00
|
|
|
#
|
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
|
2005-12-29 15:05:16 +03:00
|
|
|
raise "Thread not available for this ruby interpreter"
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? ThreadError
|
2006-12-31 18:02:22 +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
|
|
|
|
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2005-06-07 13:41:17 +04:00
|
|
|
# ConditionVariable objects augment class Mutex. Using condition variables,
|
2003-01-17 12:30:11 +03:00
|
|
|
# it is possible to suspend while in the middle of a critical section until a
|
2005-06-07 13:41:17 +04:00
|
|
|
# resource becomes available.
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# require 'thread'
|
|
|
|
#
|
|
|
|
# mutex = Mutex.new
|
|
|
|
# resource = ConditionVariable.new
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-01-17 12:30:11 +03:00
|
|
|
# a = Thread.new {
|
|
|
|
# mutex.synchronize {
|
|
|
|
# # Thread 'a' now needs the resource
|
|
|
|
# resource.wait(mutex)
|
|
|
|
# # 'a' can now have the resource
|
|
|
|
# }
|
|
|
|
# }
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2003-01-17 12:30:11 +03:00
|
|
|
# b = Thread.new {
|
|
|
|
# mutex.synchronize {
|
|
|
|
# # Thread 'b' has finished using the resource
|
|
|
|
# resource.signal
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
class ConditionVariable
|
2005-06-07 13:41:17 +04:00
|
|
|
#
|
|
|
|
# Creates a new ConditionVariable
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def initialize
|
|
|
|
@waiters = []
|
2007-02-24 10:01:18 +03:00
|
|
|
@waiters_mutex = Mutex.new
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
|
|
|
# Releases the lock held in +mutex+ and waits; reacquires the lock on wakeup.
|
|
|
|
#
|
2009-09-23 18: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)
|
2012-11-30 21:39:36 +04:00
|
|
|
Thread.async_interrupt_timing(StandardError => :defer) do
|
|
|
|
begin
|
|
|
|
Thread.async_interrupt_timing(StandardError => :on_blocking) do
|
|
|
|
@waiters_mutex.synchronize do
|
|
|
|
@waiters.push(Thread.current)
|
|
|
|
end
|
|
|
|
mutex.sleep timeout
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@waiters_mutex.synchronize do
|
|
|
|
@waiters.delete(Thread.current)
|
|
|
|
end
|
2010-05-09 21:10:18 +04:00
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2010-01-27 17:30:37 +03:00
|
|
|
self
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
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
|
2012-11-30 21:39:36 +04:00
|
|
|
Thread.async_interrupt_timing(RuntimeError => :on_blocking) do
|
|
|
|
begin
|
|
|
|
t = @waiters_mutex.synchronize {@waiters.shift}
|
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry # t was alread dead?
|
|
|
|
end
|
2000-03-17 11:58:21 +03:00
|
|
|
end
|
2010-01-27 17:30:37 +03:00
|
|
|
self
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
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
|
2012-11-30 21:39:36 +04:00
|
|
|
Thread.async_interrupt_timing(RuntimeError => :on_blocking) do
|
|
|
|
waiters0 = nil
|
|
|
|
@waiters_mutex.synchronize do
|
|
|
|
waiters0 = @waiters.dup
|
|
|
|
@waiters.clear
|
|
|
|
end
|
|
|
|
for t in waiters0
|
|
|
|
begin
|
|
|
|
t.run
|
|
|
|
rescue ThreadError
|
|
|
|
end
|
2000-03-17 11:58:21 +03:00
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2010-01-27 17:30:37 +03:00
|
|
|
self
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
2005-06-07 13:41:17 +04:00
|
|
|
# This class provides a way to synchronize communication between threads.
|
|
|
|
#
|
|
|
|
# Example:
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
2005-06-07 13:41:17 +04:00
|
|
|
# require 'thread'
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2005-06-07 13:41:17 +04:00
|
|
|
# queue = Queue.new
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2005-06-07 13: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-06 06:56:38 +03:00
|
|
|
#
|
2005-06-07 13: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-06 06:56:38 +03:00
|
|
|
#
|
2005-06-07 13:41:17 +04:00
|
|
|
# consumer.join
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
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 = []
|
2011-08-27 02:22:37 +04:00
|
|
|
@que.taint # enable tainted communication
|
1999-01-20 07:59:39 +03:00
|
|
|
@waiting.taint
|
|
|
|
self.taint
|
2006-12-31 18:02:22 +03:00
|
|
|
@mutex = Mutex.new
|
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)
|
2006-12-31 18:02:22 +03:00
|
|
|
@mutex.synchronize{
|
|
|
|
@que.push obj
|
|
|
|
begin
|
|
|
|
t = @waiting.shift
|
|
|
|
t.wakeup if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
}
|
2000-05-30 08:24:17 +04:00
|
|
|
end
|
2005-06-07 13:41:17 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of push
|
|
|
|
#
|
2001-05-16 13:05:54 +04:00
|
|
|
alias << push
|
2005-06-07 13:41:17 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of push
|
|
|
|
#
|
2001-05-16 13:05:54 +04:00
|
|
|
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)
|
2010-04-16 15:10:08 +04:00
|
|
|
@mutex.synchronize{
|
2012-09-09 16:32:33 +04:00
|
|
|
begin
|
|
|
|
while true
|
|
|
|
if @que.empty?
|
|
|
|
raise ThreadError, "queue empty" if non_block
|
|
|
|
# @waiting.include? check is necessary for avoiding a race against
|
|
|
|
# Thread.wakeup [Bug 5195]
|
|
|
|
@waiting.push Thread.current unless @waiting.include?(Thread.current)
|
|
|
|
@mutex.sleep
|
|
|
|
else
|
|
|
|
return @que.shift
|
|
|
|
end
|
2006-12-31 18:02:22 +03:00
|
|
|
end
|
2012-09-09 16:32:33 +04:00
|
|
|
ensure
|
|
|
|
@waiting.delete(Thread.current)
|
2010-04-16 15:10:08 +04:00
|
|
|
end
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2005-06-07 13:41:17 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of pop
|
|
|
|
#
|
2001-05-16 13:05:54 +04:00
|
|
|
alias shift pop
|
2005-06-07 13:41:17 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of pop
|
|
|
|
#
|
2001-05-16 13:05:54 +04:00
|
|
|
alias deq pop
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
2006-12-31 18:02:22 +03:00
|
|
|
# Returns +true+ if the queue is empty.
|
2003-01-17 12:30:11 +03:00
|
|
|
#
|
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.
|
|
|
|
#
|
2005-06-07 13:41:17 +04:00
|
|
|
alias size length
|
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
|
|
|
#
|
2005-06-07 13:41:17 +04:00
|
|
|
# This class represents queues of specified size capacity. The push operation
|
2003-01-17 12:30:11 +03:00
|
|
|
# may be blocked if the capacity is full.
|
|
|
|
#
|
2005-06-07 13:41:17 +04:00
|
|
|
# See Queue for an example of how a SizedQueue works.
|
|
|
|
#
|
2006-12-31 18:02:22 +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 = []
|
2011-05-19 04:07:25 +04:00
|
|
|
@queue_wait.taint # enable tainted comunication
|
1999-01-20 07:59:39 +03:00
|
|
|
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)
|
2011-09-03 13:46:51 +04:00
|
|
|
raise ArgumentError, "queue size must be positive" unless max > 0
|
2006-12-31 18:02:22 +03:00
|
|
|
diff = nil
|
|
|
|
@mutex.synchronize {
|
|
|
|
if max <= @max
|
|
|
|
@max = max
|
|
|
|
else
|
|
|
|
diff = max - @max
|
|
|
|
@max = max
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if diff
|
1999-01-20 07:59:39 +03:00
|
|
|
diff.times do
|
2011-05-19 01:19:18 +04: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
|
|
|
|
|
2005-06-07 13: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-20 07:59:39 +03:00
|
|
|
def push(obj)
|
2006-12-31 18:02:22 +03:00
|
|
|
@mutex.synchronize{
|
2012-09-09 16:32:33 +04:00
|
|
|
begin
|
|
|
|
while true
|
|
|
|
break if @que.length < @max
|
|
|
|
@queue_wait.push Thread.current unless @queue_wait.include?(Thread.current)
|
|
|
|
@mutex.sleep
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@queue_wait.delete(Thread.current)
|
2006-12-31 18:02:22 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
@que.push obj
|
|
|
|
begin
|
|
|
|
t = @waiting.shift
|
|
|
|
t.wakeup if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2005-06-07 13:41:17 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of push
|
|
|
|
#
|
2001-05-16 13:05:54 +04:00
|
|
|
alias << push
|
2005-06-07 13:41:17 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of push
|
|
|
|
#
|
2002-08-27 12:31:08 +04:00
|
|
|
alias enq push
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2005-06-07 13:41:17 +04:00
|
|
|
#
|
|
|
|
# Retrieves data from the queue and runs a waiting thread, if any.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def pop(*args)
|
2001-05-16 13:05:54 +04:00
|
|
|
retval = super
|
2006-12-31 18:02:22 +03:00
|
|
|
@mutex.synchronize {
|
|
|
|
if @que.length < @max
|
|
|
|
begin
|
|
|
|
t = @queue_wait.shift
|
|
|
|
t.wakeup if t
|
|
|
|
rescue ThreadError
|
|
|
|
retry
|
|
|
|
end
|
2001-05-16 13:05:54 +04:00
|
|
|
end
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2001-05-16 13:05:54 +04:00
|
|
|
retval
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2005-06-07 13:41:17 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of pop
|
|
|
|
#
|
2002-08-27 12:31:08 +04:00
|
|
|
alias shift pop
|
2005-06-07 13:41:17 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Alias of pop
|
|
|
|
#
|
2002-08-27 12:31:08 +04:00
|
|
|
alias deq pop
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2005-06-07 13:41:17 +04:00
|
|
|
#
|
|
|
|
# Returns the number of threads waiting on the queue.
|
|
|
|
#
|
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:
|
|
|
|
# - How do you make RDoc inherit documentation from superclass?
|