2
# thwait.rb - thread synchronization class
3
# $Release Version: 0.9 $
5
# $Date: 1998/06/26 03:19:34 $
6
# by Keiju ISHITSUKA(Nihpon Rational Software Co.,Ltd.)
10
# provides synchronization for multiple threads.
13
# * ThreadsWait.all_waits(thread1,...)
14
# waits until all of specified threads are terminated.
15
# if a block is supplied for the method, evaluates it for
16
# each thread termination.
17
# * th = ThreadsWait.new(thread1,...)
18
# creates synchronization object, specifying thread(s) to wait.
22
# list threads to be synchronized
24
# is there any thread to be synchronized.
26
# is there already terminated thread.
27
# * th.join(thread1,...)
28
# wait for specified thread(s).
29
# * th.join_nowait(threa1,...)
30
# specifies thread(s) to wait. non-blocking.
32
# waits until any of specified threads is terminated.
34
# waits until all of specified threads are terminated.
35
# if a block is supplied for the method, evaluates it for
36
# each thread termination.
43
# This class watches for termination of multiple threads. Basic functionality
44
# (wait until specified threads have terminated) can be accessed through the
45
# class method ThreadsWait::all_waits. Finer control can be gained using
50
# ThreadsWait.all_wait(thr1, thr2, ...) do |t|
51
# STDERR.puts "Thread #{t} has terminated."
55
RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
57
Exception2MessageMapper.extend_to(binding)
58
def_exception("ErrNoWaitingThread", "No threads for waiting.")
59
def_exception("ErrNoFinishedThread", "No finished threads.")
62
# Waits until all specified threads have terminated. If a block is provided,
63
# it is executed for each thread termination.
65
def ThreadsWait.all_waits(*threads) # :yield: thread
66
tw = ThreadsWait.new(*threads)
77
# Creates a ThreadsWait object, specifying the threads to wait on.
80
def initialize(*threads)
82
@wait_queue = Queue.new
83
join_nowait(*threads) unless threads.empty?
86
# Returns the array of threads in the wait queue.
90
# Returns +true+ if there are no threads to be synchronized.
97
# Returns +true+ if any thread has terminated.
104
# Waits for specified threads to terminate.
107
join_nowait(*threads)
112
# Specifies the threads that this object will wait for, but does not actually
115
def join_nowait(*threads)
117
@threads.concat threads
119
Thread.start(th) do |t|
130
# Waits until any of the specified threads has terminated, and returns the one
133
# If there is no thread to wait, raises +ErrNoWaitingThread+. If +nonblock+
134
# is true, and there is no terminated thread, raises +ErrNoFinishedThread+.
136
def next_wait(nonblock = nil)
137
ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
139
@threads.delete(th = @wait_queue.pop(nonblock))
142
ThreadsWait.fail ErrNoFinishedThread
147
# Waits until all of the specified threads are terminated. If a block is
148
# supplied for the method, it is executed for each thread termination.
150
# Raises exceptions in the same manner as +next_wait+.
153
until @threads.empty?
155
yield th if block_given?
163
# Documentation comments:
164
# - Source of documentation is evenly split between Nutshell, existing
165
# comments, and my own rephrasing.
166
# - I'm not particularly confident that the comments are all exactly correct.
167
# - The history, etc., up the top appears in the RDoc output. Perhaps it would
168
# be better to direct that not to appear, and put something else there