2 # thwait.rb - thread synchronization class
3 # $Release Version: 0.9 $
5 # by Keiju ISHITSUKA(Nihpon Rational Software Co.,Ltd.)
9 # provides synchronization for multiple threads.
12 # * ThreadsWait.all_waits(thread1,...)
13 # waits until all of specified threads are terminated.
14 # if a block is supplied for the method, evaluates it for
15 # each thread termination.
16 # * th = ThreadsWait.new(thread1,...)
17 # creates synchronization object, specifying thread(s) to wait.
21 # list threads to be synchronized
23 # is there any thread to be synchronized.
25 # is there already terminated thread.
26 # * th.join(thread1,...)
27 # wait for specified thread(s).
28 # * th.join_nowait(threa1,...)
29 # specifies thread(s) to wait. non-blocking.
31 # waits until any of specified threads is terminated.
33 # waits until all of specified threads are terminated.
34 # if a block is supplied for the method, evaluates it for
35 # each thread termination.
42 # This class watches for termination of multiple threads. Basic functionality
43 # (wait until specified threads have terminated) can be accessed through the
44 # class method ThreadsWait::all_waits. Finer control can be gained using
49 # ThreadsWait.all_wait(thr1, thr2, ...) do |t|
50 # STDERR.puts "Thread #{t} has terminated."
54 RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
56 extend Exception2MessageMapper
57 def_exception("ErrNoWaitingThread", "No threads for waiting.")
58 def_exception("ErrNoFinishedThread", "No finished threads.")
61 # Waits until all specified threads have terminated. If a block is provided,
62 # it is executed for each thread termination.
64 def ThreadsWait.all_waits(*threads) # :yield: thread
65 tw = ThreadsWait.new(*threads)
76 # Creates a ThreadsWait object, specifying the threads to wait on.
79 def initialize(*threads)
81 @wait_queue = Queue.new
82 join_nowait(*threads) unless threads.empty?
85 # Returns the array of threads in the wait queue.
89 # Returns +true+ if there are no threads to be synchronized.
96 # Returns +true+ if any thread has terminated.
103 # Waits for specified threads to terminate.
106 join_nowait(*threads)
111 # Specifies the threads that this object will wait for, but does not actually
114 def join_nowait(*threads)
116 @threads.concat threads
118 Thread.start(th) do |t|
129 # Waits until any of the specified threads has terminated, and returns the one
132 # If there is no thread to wait, raises +ErrNoWaitingThread+. If +nonblock+
133 # is true, and there is no terminated thread, raises +ErrNoFinishedThread+.
135 def next_wait(nonblock = nil)
136 ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
138 @threads.delete(th = @wait_queue.pop(nonblock))
141 ThreadsWait.fail ErrNoFinishedThread
146 # Waits until all of the specified threads are terminated. If a block is
147 # supplied for the method, it is executed for each thread termination.
149 # Raises exceptions in the same manner as +next_wait+.
152 until @threads.empty?
154 yield th if block_given?
162 # Documentation comments:
163 # - Source of documentation is evenly split between Nutshell, existing
164 # comments, and my own rephrasing.
165 # - I'm not particularly confident that the comments are all exactly correct.
166 # - The history, etc., up the top appears in the RDoc output. Perhaps it would
167 # be better to direct that not to appear, and put something else there