summaryrefslogtreecommitdiff
path: root/lib/thwait.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/thwait.rb')
-rw-r--r--lib/thwait.rb103
1 files changed, 37 insertions, 66 deletions
diff --git a/lib/thwait.rb b/lib/thwait.rb
index 95d294a85d..8f9e0c2a78 100644
--- a/lib/thwait.rb
+++ b/lib/thwait.rb
@@ -1,42 +1,10 @@
+# frozen_string_literal: false
#
# thwait.rb - thread synchronization class
-# $Release Version: 0.9 $
-# $Revision: 1.3 $
-# $Date: 1998/06/26 03:19:34 $
-# by Keiju ISHITSUKA(Nihpon Rational Software Co.,Ltd.)
-#
-# --
-# feature:
-# provides synchronization for multiple threads.
-#
-# 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.
-#
-# methods:
-# * th.threads
-# list threads to be synchronized
-# * th.empty?
-# is there any thread to be synchronized.
-# * th.finished?
-# is there already terminated thread.
-# * th.join(thread1,...)
-# 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.
-#
+# $Release Version: 0.9 $
+# $Revision: 1.3 $
+# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd.)
-require "thread.rb"
require "e2mmap.rb"
#
@@ -47,67 +15,71 @@ require "e2mmap.rb"
#
# Example:
#
-# ThreadsWait.all_wait(thr1, thr2, ...) do |t|
+# ThreadsWait.all_waits(thr1, thr2, ...) do |t|
# STDERR.puts "Thread #{t} has terminated."
# end
#
+#
+# th = ThreadsWait.new(thread1,...)
+# th.next_wait # next one to be done
+#
+#
class ThreadsWait
- RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
-
- Exception2MessageMapper.extend_to(binding)
+ extend Exception2MessageMapper
def_exception("ErrNoWaitingThread", "No threads for waiting.")
def_exception("ErrNoFinishedThread", "No finished threads.")
-
+
#
# Waits until all specified threads have terminated. If a block is provided,
- # it is executed for each thread termination.
+ # it is executed for each thread as they terminate.
#
def ThreadsWait.all_waits(*threads) # :yield: thread
tw = ThreadsWait.new(*threads)
if block_given?
tw.all_waits do |th|
- yield th
+ yield th
end
else
tw.all_waits
end
end
-
+
#
# Creates a ThreadsWait object, specifying the threads to wait on.
# Non-blocking.
#
def initialize(*threads)
@threads = []
- @wait_queue = Queue.new
+ @wait_queue = Thread::Queue.new
join_nowait(*threads) unless threads.empty?
end
-
- # Returns the array of threads in the wait queue.
- attr :threads
-
+
+ # Returns the array of threads that have not terminated yet.
+ attr_reader :threads
+
#
- # Returns +true+ if there are no threads to be synchronized.
+ # Returns +true+ if there are no threads in the pool still running.
#
def empty?
@threads.empty?
end
-
+
#
- # Returns +true+ if any thread has terminated.
+ # Returns +true+ if any thread has terminated and is ready to be collected.
#
def finished?
!@wait_queue.empty?
end
-
+
#
- # Waits for specified threads to terminate.
+ # Waits for specified threads to terminate, and returns when one of
+ # the threads terminated.
#
def join(*threads)
join_nowait(*threads)
next_wait
end
-
+
#
# Specifies the threads that this object will wait for, but does not actually
# wait.
@@ -117,15 +89,15 @@ class ThreadsWait
@threads.concat threads
for th in threads
Thread.start(th) do |t|
- begin
- t.join
- ensure
- @wait_queue.push t
- end
+ begin
+ t.join
+ ensure
+ @wait_queue.push t
+ end
end
end
end
-
+
#
# Waits until any of the specified threads has terminated, and returns the one
# that does.
@@ -142,7 +114,7 @@ class ThreadsWait
ThreadsWait.fail ErrNoFinishedThread
end
end
-
+
#
# Waits until all of the specified threads are terminated. If a block is
# supplied for the method, it is executed for each thread termination.
@@ -157,13 +129,12 @@ class ThreadsWait
end
end
-ThWait = ThreadsWait
+##
+# An alias for ThreadsWait from thwait.rb
+ThWait = ThreadsWait
# Documentation comments:
# - Source of documentation 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.