Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: thwait.rb
#
[0] Fix | Delete
# thwait.rb - thread synchronization class
[1] Fix | Delete
# $Release Version: 0.9 $
[2] Fix | Delete
# $Revision: 1.3 $
[3] Fix | Delete
# $Date: 1998/06/26 03:19:34 $
[4] Fix | Delete
# by Keiju ISHITSUKA(Nihpon Rational Software Co.,Ltd.)
[5] Fix | Delete
#
[6] Fix | Delete
# --
[7] Fix | Delete
# feature:
[8] Fix | Delete
# provides synchronization for multiple threads.
[9] Fix | Delete
#
[10] Fix | Delete
# class methods:
[11] Fix | Delete
# * ThreadsWait.all_waits(thread1,...)
[12] Fix | Delete
# waits until all of specified threads are terminated.
[13] Fix | Delete
# if a block is supplied for the method, evaluates it for
[14] Fix | Delete
# each thread termination.
[15] Fix | Delete
# * th = ThreadsWait.new(thread1,...)
[16] Fix | Delete
# creates synchronization object, specifying thread(s) to wait.
[17] Fix | Delete
#
[18] Fix | Delete
# methods:
[19] Fix | Delete
# * th.threads
[20] Fix | Delete
# list threads to be synchronized
[21] Fix | Delete
# * th.empty?
[22] Fix | Delete
# is there any thread to be synchronized.
[23] Fix | Delete
# * th.finished?
[24] Fix | Delete
# is there already terminated thread.
[25] Fix | Delete
# * th.join(thread1,...)
[26] Fix | Delete
# wait for specified thread(s).
[27] Fix | Delete
# * th.join_nowait(threa1,...)
[28] Fix | Delete
# specifies thread(s) to wait. non-blocking.
[29] Fix | Delete
# * th.next_wait
[30] Fix | Delete
# waits until any of specified threads is terminated.
[31] Fix | Delete
# * th.all_waits
[32] Fix | Delete
# waits until all of specified threads are terminated.
[33] Fix | Delete
# if a block is supplied for the method, evaluates it for
[34] Fix | Delete
# each thread termination.
[35] Fix | Delete
#
[36] Fix | Delete
[37] Fix | Delete
require "thread.rb"
[38] Fix | Delete
require "e2mmap.rb"
[39] Fix | Delete
[40] Fix | Delete
#
[41] Fix | Delete
# This class watches for termination of multiple threads. Basic functionality
[42] Fix | Delete
# (wait until specified threads have terminated) can be accessed through the
[43] Fix | Delete
# class method ThreadsWait::all_waits. Finer control can be gained using
[44] Fix | Delete
# instance methods.
[45] Fix | Delete
#
[46] Fix | Delete
# Example:
[47] Fix | Delete
#
[48] Fix | Delete
# ThreadsWait.all_wait(thr1, thr2, ...) do |t|
[49] Fix | Delete
# STDERR.puts "Thread #{t} has terminated."
[50] Fix | Delete
# end
[51] Fix | Delete
#
[52] Fix | Delete
class ThreadsWait
[53] Fix | Delete
RCS_ID='-$Id: thwait.rb,v 1.3 1998/06/26 03:19:34 keiju Exp keiju $-'
[54] Fix | Delete
[55] Fix | Delete
Exception2MessageMapper.extend_to(binding)
[56] Fix | Delete
def_exception("ErrNoWaitingThread", "No threads for waiting.")
[57] Fix | Delete
def_exception("ErrNoFinishedThread", "No finished threads.")
[58] Fix | Delete
[59] Fix | Delete
#
[60] Fix | Delete
# Waits until all specified threads have terminated. If a block is provided,
[61] Fix | Delete
# it is executed for each thread termination.
[62] Fix | Delete
#
[63] Fix | Delete
def ThreadsWait.all_waits(*threads) # :yield: thread
[64] Fix | Delete
tw = ThreadsWait.new(*threads)
[65] Fix | Delete
if block_given?
[66] Fix | Delete
tw.all_waits do |th|
[67] Fix | Delete
yield th
[68] Fix | Delete
end
[69] Fix | Delete
else
[70] Fix | Delete
tw.all_waits
[71] Fix | Delete
end
[72] Fix | Delete
end
[73] Fix | Delete
[74] Fix | Delete
#
[75] Fix | Delete
# Creates a ThreadsWait object, specifying the threads to wait on.
[76] Fix | Delete
# Non-blocking.
[77] Fix | Delete
#
[78] Fix | Delete
def initialize(*threads)
[79] Fix | Delete
@threads = []
[80] Fix | Delete
@wait_queue = Queue.new
[81] Fix | Delete
join_nowait(*threads) unless threads.empty?
[82] Fix | Delete
end
[83] Fix | Delete
[84] Fix | Delete
# Returns the array of threads in the wait queue.
[85] Fix | Delete
attr :threads
[86] Fix | Delete
[87] Fix | Delete
#
[88] Fix | Delete
# Returns +true+ if there are no threads to be synchronized.
[89] Fix | Delete
#
[90] Fix | Delete
def empty?
[91] Fix | Delete
@threads.empty?
[92] Fix | Delete
end
[93] Fix | Delete
[94] Fix | Delete
#
[95] Fix | Delete
# Returns +true+ if any thread has terminated.
[96] Fix | Delete
#
[97] Fix | Delete
def finished?
[98] Fix | Delete
!@wait_queue.empty?
[99] Fix | Delete
end
[100] Fix | Delete
[101] Fix | Delete
#
[102] Fix | Delete
# Waits for specified threads to terminate, and returns when one of
[103] Fix | Delete
# the threads terminated.
[104] Fix | Delete
#
[105] Fix | Delete
def join(*threads)
[106] Fix | Delete
join_nowait(*threads)
[107] Fix | Delete
next_wait
[108] Fix | Delete
end
[109] Fix | Delete
[110] Fix | Delete
#
[111] Fix | Delete
# Specifies the threads that this object will wait for, but does not actually
[112] Fix | Delete
# wait.
[113] Fix | Delete
#
[114] Fix | Delete
def join_nowait(*threads)
[115] Fix | Delete
threads.flatten!
[116] Fix | Delete
@threads.concat threads
[117] Fix | Delete
for th in threads
[118] Fix | Delete
Thread.start(th) do |t|
[119] Fix | Delete
begin
[120] Fix | Delete
t.join
[121] Fix | Delete
ensure
[122] Fix | Delete
@wait_queue.push t
[123] Fix | Delete
end
[124] Fix | Delete
end
[125] Fix | Delete
end
[126] Fix | Delete
end
[127] Fix | Delete
[128] Fix | Delete
#
[129] Fix | Delete
# Waits until any of the specified threads has terminated, and returns the one
[130] Fix | Delete
# that does.
[131] Fix | Delete
#
[132] Fix | Delete
# If there is no thread to wait, raises +ErrNoWaitingThread+. If +nonblock+
[133] Fix | Delete
# is true, and there is no terminated thread, raises +ErrNoFinishedThread+.
[134] Fix | Delete
#
[135] Fix | Delete
def next_wait(nonblock = nil)
[136] Fix | Delete
ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
[137] Fix | Delete
begin
[138] Fix | Delete
@threads.delete(th = @wait_queue.pop(nonblock))
[139] Fix | Delete
th
[140] Fix | Delete
rescue ThreadError
[141] Fix | Delete
ThreadsWait.fail ErrNoFinishedThread
[142] Fix | Delete
end
[143] Fix | Delete
end
[144] Fix | Delete
[145] Fix | Delete
#
[146] Fix | Delete
# Waits until all of the specified threads are terminated. If a block is
[147] Fix | Delete
# supplied for the method, it is executed for each thread termination.
[148] Fix | Delete
#
[149] Fix | Delete
# Raises exceptions in the same manner as +next_wait+.
[150] Fix | Delete
#
[151] Fix | Delete
def all_waits
[152] Fix | Delete
until @threads.empty?
[153] Fix | Delete
th = next_wait
[154] Fix | Delete
yield th if block_given?
[155] Fix | Delete
end
[156] Fix | Delete
end
[157] Fix | Delete
end
[158] Fix | Delete
[159] Fix | Delete
ThWait = ThreadsWait
[160] Fix | Delete
[161] Fix | Delete
[162] Fix | Delete
# Documentation comments:
[163] Fix | Delete
# - Source of documentation is evenly split between Nutshell, existing
[164] Fix | Delete
# comments, and my own rephrasing.
[165] Fix | Delete
# - I'm not particularly confident that the comments are all exactly correct.
[166] Fix | Delete
# - The history, etc., up the top appears in the RDoc output. Perhaps it would
[167] Fix | Delete
# be better to direct that not to appear, and put something else there
[168] Fix | Delete
# instead.
[169] Fix | Delete
[170] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function