1998-01-16 07:13:05 -05:00
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# thwait.rb - thread synchronization class
|
|
|
|
# $Release Version: 0.9 $
|
|
|
|
# $Revision: 1.3 $
|
2010-03-19 23:30:59 -04:00
|
|
|
# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd.)
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
# --
|
1999-01-19 23:59:39 -05:00
|
|
|
# feature:
|
|
|
|
# provides synchronization for multiple threads.
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# class methods:
|
|
|
|
# * ThreadsWait.all_waits(thread1,...)
|
|
|
|
# waits until all of specified threads are terminated.
|
|
|
|
# if a block is supplied for the method, evaluates it for
|
|
|
|
# each thread termination.
|
|
|
|
# * th = ThreadsWait.new(thread1,...)
|
|
|
|
# creates synchronization object, specifying thread(s) to wait.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
1999-01-19 23:59:39 -05:00
|
|
|
# methods:
|
|
|
|
# * th.threads
|
|
|
|
# list threads to be synchronized
|
|
|
|
# * th.empty?
|
|
|
|
# is there any thread to be synchronized.
|
|
|
|
# * th.finished?
|
|
|
|
# is there already terminated thread.
|
2009-03-05 22:56:38 -05:00
|
|
|
# * th.join(thread1,...)
|
1999-01-19 23:59:39 -05:00
|
|
|
# wait for specified thread(s).
|
|
|
|
# * th.join_nowait(threa1,...)
|
|
|
|
# specifies thread(s) to wait. non-blocking.
|
|
|
|
# * th.next_wait
|
|
|
|
# waits until any of specified threads is terminated.
|
|
|
|
# * th.all_waits
|
|
|
|
# waits until all of specified threads are terminated.
|
|
|
|
# if a block is supplied for the method, evaluates it for
|
|
|
|
# each thread termination.
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
require "thread.rb"
|
|
|
|
require "e2mmap.rb"
|
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
|
|
|
# This class watches for termination of multiple threads. Basic functionality
|
|
|
|
# (wait until specified threads have terminated) can be accessed through the
|
|
|
|
# class method ThreadsWait::all_waits. Finer control can be gained using
|
|
|
|
# instance methods.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# ThreadsWait.all_wait(thr1, thr2, ...) do |t|
|
|
|
|
# STDERR.puts "Thread #{t} has terminated."
|
|
|
|
# end
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
class ThreadsWait
|
1999-01-19 23:59:39 -05:00
|
|
|
RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-06-16 05:52:50 -04:00
|
|
|
extend Exception2MessageMapper
|
1999-01-19 23:59:39 -05:00
|
|
|
def_exception("ErrNoWaitingThread", "No threads for waiting.")
|
2000-09-25 05:15:08 -04:00
|
|
|
def_exception("ErrNoFinishedThread", "No finished threads.")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
|
|
|
# Waits until all specified threads have terminated. If a block is provided,
|
|
|
|
# it is executed for each thread termination.
|
|
|
|
#
|
|
|
|
def ThreadsWait.all_waits(*threads) # :yield: thread
|
1999-01-19 23:59:39 -05:00
|
|
|
tw = ThreadsWait.new(*threads)
|
2000-06-28 04:31:35 -04:00
|
|
|
if block_given?
|
2000-09-25 05:15:08 -04:00
|
|
|
tw.all_waits do |th|
|
1998-01-16 07:13:05 -05:00
|
|
|
yield th
|
|
|
|
end
|
|
|
|
else
|
|
|
|
tw.all_waits
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
|
|
|
# Creates a ThreadsWait object, specifying the threads to wait on.
|
|
|
|
# Non-blocking.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def initialize(*threads)
|
|
|
|
@threads = []
|
|
|
|
@wait_queue = Queue.new
|
|
|
|
join_nowait(*threads) unless threads.empty?
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
# Returns the array of threads in the wait queue.
|
1998-01-16 07:13:05 -05:00
|
|
|
attr :threads
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
2003-01-20 11:05:47 -05:00
|
|
|
# Returns +true+ if there are no threads to be synchronized.
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def empty?
|
|
|
|
@threads.empty?
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
2003-01-20 11:05:47 -05:00
|
|
|
# Returns +true+ if any thread has terminated.
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def finished?
|
|
|
|
!@wait_queue.empty?
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
|
|
|
# Waits for specified threads to terminate.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def join(*threads)
|
|
|
|
join_nowait(*threads)
|
|
|
|
next_wait
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
|
|
|
# Specifies the threads that this object will wait for, but does not actually
|
|
|
|
# wait.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def join_nowait(*threads)
|
2000-09-25 05:15:08 -04:00
|
|
|
threads.flatten!
|
|
|
|
@threads.concat threads
|
1998-01-16 07:13:05 -05:00
|
|
|
for th in threads
|
2000-09-25 05:15:08 -04:00
|
|
|
Thread.start(th) do |t|
|
2004-11-25 06:15:33 -05:00
|
|
|
begin
|
|
|
|
t.join
|
|
|
|
ensure
|
|
|
|
@wait_queue.push t
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
|
|
|
# Waits until any of the specified threads has terminated, and returns the one
|
|
|
|
# that does.
|
|
|
|
#
|
|
|
|
# If there is no thread to wait, raises +ErrNoWaitingThread+. If +nonblock+
|
|
|
|
# is true, and there is no terminated thread, raises +ErrNoFinishedThread+.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def next_wait(nonblock = nil)
|
1999-01-19 23:59:39 -05:00
|
|
|
ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
|
|
|
|
begin
|
|
|
|
@threads.delete(th = @wait_queue.pop(nonblock))
|
|
|
|
th
|
|
|
|
rescue ThreadError
|
2000-09-20 05:16:32 -04:00
|
|
|
ThreadsWait.fail ErrNoFinishedThread
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-01-17 10:17:20 -05:00
|
|
|
#
|
|
|
|
# Waits until all of the specified threads are terminated. If a block is
|
|
|
|
# supplied for the method, it is executed for each thread termination.
|
|
|
|
#
|
|
|
|
# Raises exceptions in the same manner as +next_wait+.
|
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
def all_waits
|
|
|
|
until @threads.empty?
|
|
|
|
th = next_wait
|
2000-06-28 04:31:35 -04:00
|
|
|
yield th if block_given?
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
ThWait = ThreadsWait
|
2003-01-17 10:17:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Documentation comments:
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# - Source of documentation is evenly split between Nutshell, existing
|
2003-01-17 10:17:20 -05:00
|
|
|
# comments, and my own rephrasing.
|
|
|
|
# - I'm not particularly confident that the comments are all exactly correct.
|
|
|
|
# - The history, etc., up the top appears in the RDoc output. Perhaps it would
|
|
|
|
# be better to direct that not to appear, and put something else there
|
|
|
|
# instead.
|