Added RDoc comments. See comments at EOF for TODOs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3354 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
99a3e3fdfa
commit
b1e4030244
@ -39,6 +39,18 @@
|
|||||||
require "thread.rb"
|
require "thread.rb"
|
||||||
require "e2mmap.rb"
|
require "e2mmap.rb"
|
||||||
|
|
||||||
|
#
|
||||||
|
# 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
|
||||||
|
#
|
||||||
class ThreadsWait
|
class ThreadsWait
|
||||||
RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
|
RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
|
||||||
|
|
||||||
@ -46,7 +58,11 @@ class ThreadsWait
|
|||||||
def_exception("ErrNoWaitingThread", "No threads for waiting.")
|
def_exception("ErrNoWaitingThread", "No threads for waiting.")
|
||||||
def_exception("ErrNoFinishedThread", "No finished threads.")
|
def_exception("ErrNoFinishedThread", "No finished threads.")
|
||||||
|
|
||||||
def ThreadsWait.all_waits(*threads)
|
#
|
||||||
|
# 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
|
||||||
tw = ThreadsWait.new(*threads)
|
tw = ThreadsWait.new(*threads)
|
||||||
if block_given?
|
if block_given?
|
||||||
tw.all_waits do |th|
|
tw.all_waits do |th|
|
||||||
@ -57,43 +73,45 @@ class ThreadsWait
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Creates a ThreadsWait object, specifying the threads to wait on.
|
||||||
|
# Non-blocking.
|
||||||
|
#
|
||||||
def initialize(*threads)
|
def initialize(*threads)
|
||||||
@threads = []
|
@threads = []
|
||||||
@wait_queue = Queue.new
|
@wait_queue = Queue.new
|
||||||
join_nowait(*threads) unless threads.empty?
|
join_nowait(*threads) unless threads.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
# accessing
|
# Returns the array of threads in the wait queue.
|
||||||
# threads - list threads to be synchronized
|
|
||||||
attr :threads
|
attr :threads
|
||||||
|
|
||||||
# testing
|
#
|
||||||
# empty?
|
# Returns true if there are no threads to be synchronized.
|
||||||
# finished?
|
#
|
||||||
|
|
||||||
# is there any thread to be synchronized.
|
|
||||||
def empty?
|
def empty?
|
||||||
@threads.empty?
|
@threads.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
# is there already terminated thread.
|
#
|
||||||
|
# Returns true if any thread has terminated.
|
||||||
|
#
|
||||||
def finished?
|
def finished?
|
||||||
!@wait_queue.empty?
|
!@wait_queue.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
# main process:
|
#
|
||||||
# join
|
# Waits for specified threads to terminate.
|
||||||
# join_nowait
|
#
|
||||||
# next_wait
|
|
||||||
# all_wait
|
|
||||||
|
|
||||||
# adds thread(s) to join, waits for any of waiting threads to terminate.
|
|
||||||
def join(*threads)
|
def join(*threads)
|
||||||
join_nowait(*threads)
|
join_nowait(*threads)
|
||||||
next_wait
|
next_wait
|
||||||
end
|
end
|
||||||
|
|
||||||
# adds thread(s) to join, no wait.
|
#
|
||||||
|
# Specifies the threads that this object will wait for, but does not actually
|
||||||
|
# wait.
|
||||||
|
#
|
||||||
def join_nowait(*threads)
|
def join_nowait(*threads)
|
||||||
threads.flatten!
|
threads.flatten!
|
||||||
@threads.concat threads
|
@threads.concat threads
|
||||||
@ -105,10 +123,13 @@ class ThreadsWait
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# waits for any of waiting threads to terminate
|
#
|
||||||
# if there is no thread to wait, raises ErrNoWaitingThread.
|
# Waits until any of the specified threads has terminated, and returns the one
|
||||||
# if `nonblock' is true, and there is no terminated thread,
|
# that does.
|
||||||
# raises ErrNoFinishedThread.
|
#
|
||||||
|
# If there is no thread to wait, raises +ErrNoWaitingThread+. If +nonblock+
|
||||||
|
# is true, and there is no terminated thread, raises +ErrNoFinishedThread+.
|
||||||
|
#
|
||||||
def next_wait(nonblock = nil)
|
def next_wait(nonblock = nil)
|
||||||
ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
|
ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
|
||||||
begin
|
begin
|
||||||
@ -119,9 +140,12 @@ class ThreadsWait
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# waits until all of specified threads are terminated.
|
#
|
||||||
# if a block is supplied for the method, evaluates it for
|
# Waits until all of the specified threads are terminated. If a block is
|
||||||
# each thread termination.
|
# supplied for the method, it is executed for each thread termination.
|
||||||
|
#
|
||||||
|
# Raises exceptions in the same manner as +next_wait+.
|
||||||
|
#
|
||||||
def all_waits
|
def all_waits
|
||||||
until @threads.empty?
|
until @threads.empty?
|
||||||
th = next_wait
|
th = next_wait
|
||||||
@ -131,3 +155,12 @@ class ThreadsWait
|
|||||||
end
|
end
|
||||||
|
|
||||||
ThWait = ThreadsWait
|
ThWait = ThreadsWait
|
||||||
|
|
||||||
|
|
||||||
|
# Documentation comments:
|
||||||
|
# - Source of doumentation is evenly split between Nutshell, existing
|
||||||
|
# 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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user