2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
# thwait.rb - thread synchronization class
|
2011-05-19 04:07:25 +04:00
|
|
|
# $Release Version: 0.9 $
|
|
|
|
# $Revision: 1.3 $
|
|
|
|
# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd.)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
require "thread.rb"
|
|
|
|
require "e2mmap.rb"
|
|
|
|
|
2003-01-17 18:17:20 +03: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:
|
|
|
|
#
|
2013-11-07 21:02:56 +04:00
|
|
|
# ThreadsWait.all_waits(thr1, thr2, ...) do |t|
|
2003-01-17 18:17:20 +03:00
|
|
|
# STDERR.puts "Thread #{t} has terminated."
|
|
|
|
# end
|
|
|
|
#
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2010-04-28 18:53:58 +04:00
|
|
|
# th = ThreadsWait.new(thread1,...)
|
|
|
|
# th.next_wait # next one to be done
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2010-04-28 18:53:58 +04:00
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
class ThreadsWait
|
2008-06-16 13:52:50 +04:00
|
|
|
extend Exception2MessageMapper
|
1999-01-20 07:59:39 +03:00
|
|
|
def_exception("ErrNoWaitingThread", "No threads for waiting.")
|
2000-09-25 13:15:08 +04:00
|
|
|
def_exception("ErrNoFinishedThread", "No finished threads.")
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
|
|
|
# Waits until all specified threads have terminated. If a block is provided,
|
2010-04-28 18:53:58 +04:00
|
|
|
# it is executed for each thread as they terminate.
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
|
|
|
def ThreadsWait.all_waits(*threads) # :yield: thread
|
1999-01-20 07:59:39 +03:00
|
|
|
tw = ThreadsWait.new(*threads)
|
2000-06-28 12:31:35 +04:00
|
|
|
if block_given?
|
2000-09-25 13:15:08 +04:00
|
|
|
tw.all_waits do |th|
|
2011-05-19 01:19:18 +04:00
|
|
|
yield th
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
tw.all_waits
|
|
|
|
end
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
|
|
|
# Creates a ThreadsWait object, specifying the threads to wait on.
|
|
|
|
# Non-blocking.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def initialize(*threads)
|
|
|
|
@threads = []
|
2016-08-30 09:22:30 +03:00
|
|
|
@wait_queue = Thread::Queue.new
|
1998-01-16 15:13:05 +03:00
|
|
|
join_nowait(*threads) unless threads.empty?
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2010-04-28 18:53:58 +04:00
|
|
|
# Returns the array of threads that have not terminated yet.
|
2014-08-27 16:10:21 +04:00
|
|
|
attr_reader :threads
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
2010-04-28 18:53:58 +04:00
|
|
|
# Returns +true+ if there are no threads in the pool still running.
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def empty?
|
|
|
|
@threads.empty?
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
2010-04-28 18:53:58 +04:00
|
|
|
# Returns +true+ if any thread has terminated and is ready to be collected.
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def finished?
|
|
|
|
!@wait_queue.empty?
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
2010-04-29 13:39:52 +04:00
|
|
|
# Waits for specified threads to terminate, and returns when one of
|
|
|
|
# the threads terminated.
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def join(*threads)
|
|
|
|
join_nowait(*threads)
|
|
|
|
next_wait
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-01-17 18:17:20 +03:00
|
|
|
#
|
|
|
|
# Specifies the threads that this object will wait for, but does not actually
|
|
|
|
# wait.
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
def join_nowait(*threads)
|
2000-09-25 13:15:08 +04:00
|
|
|
threads.flatten!
|
|
|
|
@threads.concat threads
|
1998-01-16 15:13:05 +03:00
|
|
|
for th in threads
|
2000-09-25 13:15:08 +04:00
|
|
|
Thread.start(th) do |t|
|
2011-05-19 01:19:18 +04:00
|
|
|
begin
|
|
|
|
t.join
|
|
|
|
ensure
|
|
|
|
@wait_queue.push t
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-01-17 18:17:20 +03: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 15:13:05 +03:00
|
|
|
def next_wait(nonblock = nil)
|
1999-01-20 07:59:39 +03:00
|
|
|
ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
|
|
|
|
begin
|
|
|
|
@threads.delete(th = @wait_queue.pop(nonblock))
|
|
|
|
th
|
|
|
|
rescue ThreadError
|
2000-09-20 13:16:32 +04:00
|
|
|
ThreadsWait.fail ErrNoFinishedThread
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-01-17 18:17:20 +03: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 15:13:05 +03:00
|
|
|
def all_waits
|
|
|
|
until @threads.empty?
|
|
|
|
th = next_wait
|
2000-06-28 12:31:35 +04:00
|
|
|
yield th if block_given?
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2011-06-29 07:09:34 +04:00
|
|
|
##
|
|
|
|
# An alias for ThreadsWait from thwait.rb
|
2003-01-17 18:17:20 +03:00
|
|
|
|
2011-06-29 07:09:34 +04:00
|
|
|
ThWait = ThreadsWait
|
2003-01-17 18:17:20 +03: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-19 03:19:47 +04:00
|
|
|
# - Source of documentation is evenly split between Nutshell, existing
|
2003-01-17 18:17:20 +03:00
|
|
|
# comments, and my own rephrasing.
|
|
|
|
# - I'm not particularly confident that the comments are all exactly correct.
|