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
|
2012-12-01 12:28:44 +04:00
|
|
|
@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-12-23 14:18:58 +04:00
|
|
|
Thread.handle_interrupt(StandardError => :never) do
|
2012-11-30 21:39:36 +04:00
|
|
|
begin
|
2012-12-23 14:18:58 +04:00
|
|
|
Thread.handle_interrupt(StandardError => :on_blocking) do
|
2012-11-30 21:39:36 +04:00
|
|
|
@waiters_mutex.synchronize do
|
2012-12-01 12:28:44 +04:00
|
|
|
@waiters[Thread.current] = true
|
2012-11-30 21:39:36 +04:00
|
|
|
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-12-23 14:18:58 +04:00
|
|
|
Thread.handle_interrupt(StandardError => :on_blocking) do
|
2012-11-30 21:39:36 +04:00
|
|
|
begin
|
2012-12-01 12:28:44 +04:00
|
|
|
t, _ = @waiters_mutex.synchronize { @waiters.shift }
|
2012-11-30 21:39:36 +04:00
|
|
|
t.run if t
|
|
|
|
rescue ThreadError
|
2012-12-01 12:28:44 +04:00
|
|
|
retry # t was already dead?
|
2012-11-30 21:39:36 +04:00
|
|
|
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-12-23 14:18:58 +04:00
|
|
|
Thread.handle_interrupt(StandardError => :on_blocking) do
|
2012-12-01 12:28:44 +04:00
|
|
|
threads = nil
|
2012-11-30 21:39:36 +04:00
|
|
|
@waiters_mutex.synchronize do
|
2012-12-01 12:28:44 +04:00
|
|
|
threads = @waiters.keys
|
2012-11-30 21:39:36 +04:00
|
|
|
@waiters.clear
|
|
|
|
end
|
2012-12-01 12:28:44 +04:00
|
|
|
for t in threads
|
2012-11-30 21:39:36 +04:00
|
|
|
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 = []
|
2011-08-27 02:22:37 +04:00
|
|
|
@que.taint # enable tainted communication
|
2012-11-30 22:55:09 +04:00
|
|
|
@num_waiting = 0
|
1999-01-20 07:59:39 +03:00
|
|
|
self.taint
|
2006-12-31 18:02:22 +03:00
|
|
|
@mutex = Mutex.new
|
2012-11-30 22:55:09 +04:00
|
|
|
@cond = ConditionVariable.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)
|
2012-12-23 14:18:58 +04:00
|
|
|
Thread.handle_interrupt(StandardError => :on_blocking) do
|
2012-11-30 22:55:09 +04:00
|
|
|
@mutex.synchronize do
|
|
|
|
@que.push obj
|
|
|
|
@cond.signal
|
2006-12-31 18:02:22 +03:00
|
|
|
end
|
2013-03-11 03:01:21 +04:00
|
|
|
self
|
2012-11-30 22:55:09 +04:00
|
|
|
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)
|
2012-12-23 14:18:58 +04:00
|
|
|
Thread.handle_interrupt(StandardError => :on_blocking) do
|
2012-11-30 22:55:09 +04:00
|
|
|
@mutex.synchronize do
|
2012-09-09 16:32:33 +04:00
|
|
|
while true
|
|
|
|
if @que.empty?
|
2012-11-30 22:55:09 +04:00
|
|
|
if non_block
|
|
|
|
raise ThreadError, "queue empty"
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
@num_waiting += 1
|
|
|
|
@cond.wait @mutex
|
|
|
|
ensure
|
|
|
|
@num_waiting -= 1
|
|
|
|
end
|
|
|
|
end
|
2012-09-09 16:32:33 +04:00
|
|
|
else
|
|
|
|
return @que.shift
|
|
|
|
end
|
2006-12-31 18:02:22 +03:00
|
|
|
end
|
2010-04-16 15:10:08 +04:00
|
|
|
end
|
2012-11-30 22:55:09 +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
|
2013-03-11 03:01:21 +04:00
|
|
|
self
|
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
|
2012-11-30 22:55:09 +04:00
|
|
|
@num_waiting
|
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 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
|
2012-11-30 22:55:09 +04:00
|
|
|
@enque_cond = ConditionVariable.new
|
|
|
|
@num_enqueue_waiting = 0
|
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
|
2012-11-30 22:55:09 +04:00
|
|
|
|
|
|
|
@mutex.synchronize do
|
2006-12-31 18:02:22 +03:00
|
|
|
if max <= @max
|
|
|
|
@max = max
|
|
|
|
else
|
|
|
|
diff = max - @max
|
|
|
|
@max = max
|
2012-11-30 22:55:09 +04:00
|
|
|
diff.times do
|
|
|
|
@enque_cond.signal
|
2011-05-19 01:19:18 +04:00
|
|
|
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)
|
2012-12-23 14:18:58 +04:00
|
|
|
Thread.handle_interrupt(RuntimeError => :on_blocking) do
|
2012-11-30 22:55:09 +04:00
|
|
|
@mutex.synchronize do
|
2012-09-09 16:32:33 +04:00
|
|
|
while true
|
|
|
|
break if @que.length < @max
|
2012-11-30 22:55:09 +04:00
|
|
|
@num_enqueue_waiting += 1
|
|
|
|
begin
|
|
|
|
@enque_cond.wait @mutex
|
|
|
|
ensure
|
|
|
|
@num_enqueue_waiting -= 1
|
|
|
|
end
|
2012-09-09 16:32:33 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2012-11-30 22:55:09 +04:00
|
|
|
@que.push obj
|
|
|
|
@cond.signal
|
2006-12-31 18:02:22 +03:00
|
|
|
end
|
2013-03-11 03:01:21 +04:00
|
|
|
self
|
2012-11-30 22:55:09 +04:00
|
|
|
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
|
2012-11-30 22:55:09 +04:00
|
|
|
@mutex.synchronize do
|
2006-12-31 18:02:22 +03:00
|
|
|
if @que.length < @max
|
2012-11-30 22:55:09 +04:00
|
|
|
@enque_cond.signal
|
2001-05-16 13:05:54 +04:00
|
|
|
end
|
2012-11-30 22:55:09 +04:00
|
|
|
end
|
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
|
2012-11-30 22:55:09 +04:00
|
|
|
@num_waiting + @num_enqueue_waiting
|
1999-01-20 07:59:39 +03:00
|
|
|
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?
|