Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/share/ruby
File: thwait.rb
# frozen_string_literal: false
[0] Fix | Delete
#
[1] Fix | Delete
# thwait.rb - thread synchronization class
[2] Fix | Delete
# $Release Version: 0.9 $
[3] Fix | Delete
# $Revision: 1.3 $
[4] Fix | Delete
# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd.)
[5] Fix | Delete
[6] Fix | Delete
require "e2mmap.rb"
[7] Fix | Delete
[8] Fix | Delete
#
[9] Fix | Delete
# This class watches for termination of multiple threads. Basic functionality
[10] Fix | Delete
# (wait until specified threads have terminated) can be accessed through the
[11] Fix | Delete
# class method ThreadsWait::all_waits. Finer control can be gained using
[12] Fix | Delete
# instance methods.
[13] Fix | Delete
#
[14] Fix | Delete
# Example:
[15] Fix | Delete
#
[16] Fix | Delete
# ThreadsWait.all_waits(thr1, thr2, ...) do |t|
[17] Fix | Delete
# STDERR.puts "Thread #{t} has terminated."
[18] Fix | Delete
# end
[19] Fix | Delete
#
[20] Fix | Delete
#
[21] Fix | Delete
# th = ThreadsWait.new(thread1,...)
[22] Fix | Delete
# th.next_wait # next one to be done
[23] Fix | Delete
#
[24] Fix | Delete
#
[25] Fix | Delete
class ThreadsWait
[26] Fix | Delete
extend Exception2MessageMapper
[27] Fix | Delete
def_exception("ErrNoWaitingThread", "No threads for waiting.")
[28] Fix | Delete
def_exception("ErrNoFinishedThread", "No finished threads.")
[29] Fix | Delete
[30] Fix | Delete
#
[31] Fix | Delete
# Waits until all specified threads have terminated. If a block is provided,
[32] Fix | Delete
# it is executed for each thread as they terminate.
[33] Fix | Delete
#
[34] Fix | Delete
def ThreadsWait.all_waits(*threads) # :yield: thread
[35] Fix | Delete
tw = ThreadsWait.new(*threads)
[36] Fix | Delete
if block_given?
[37] Fix | Delete
tw.all_waits do |th|
[38] Fix | Delete
yield th
[39] Fix | Delete
end
[40] Fix | Delete
else
[41] Fix | Delete
tw.all_waits
[42] Fix | Delete
end
[43] Fix | Delete
end
[44] Fix | Delete
[45] Fix | Delete
#
[46] Fix | Delete
# Creates a ThreadsWait object, specifying the threads to wait on.
[47] Fix | Delete
# Non-blocking.
[48] Fix | Delete
#
[49] Fix | Delete
def initialize(*threads)
[50] Fix | Delete
@threads = []
[51] Fix | Delete
@wait_queue = Thread::Queue.new
[52] Fix | Delete
join_nowait(*threads) unless threads.empty?
[53] Fix | Delete
end
[54] Fix | Delete
[55] Fix | Delete
# Returns the array of threads that have not terminated yet.
[56] Fix | Delete
attr_reader :threads
[57] Fix | Delete
[58] Fix | Delete
#
[59] Fix | Delete
# Returns +true+ if there are no threads in the pool still running.
[60] Fix | Delete
#
[61] Fix | Delete
def empty?
[62] Fix | Delete
@threads.empty?
[63] Fix | Delete
end
[64] Fix | Delete
[65] Fix | Delete
#
[66] Fix | Delete
# Returns +true+ if any thread has terminated and is ready to be collected.
[67] Fix | Delete
#
[68] Fix | Delete
def finished?
[69] Fix | Delete
!@wait_queue.empty?
[70] Fix | Delete
end
[71] Fix | Delete
[72] Fix | Delete
#
[73] Fix | Delete
# Waits for specified threads to terminate, and returns when one of
[74] Fix | Delete
# the threads terminated.
[75] Fix | Delete
#
[76] Fix | Delete
def join(*threads)
[77] Fix | Delete
join_nowait(*threads)
[78] Fix | Delete
next_wait
[79] Fix | Delete
end
[80] Fix | Delete
[81] Fix | Delete
#
[82] Fix | Delete
# Specifies the threads that this object will wait for, but does not actually
[83] Fix | Delete
# wait.
[84] Fix | Delete
#
[85] Fix | Delete
def join_nowait(*threads)
[86] Fix | Delete
threads.flatten!
[87] Fix | Delete
@threads.concat threads
[88] Fix | Delete
for th in threads
[89] Fix | Delete
Thread.start(th) do |t|
[90] Fix | Delete
begin
[91] Fix | Delete
t.join
[92] Fix | Delete
ensure
[93] Fix | Delete
@wait_queue.push t
[94] Fix | Delete
end
[95] Fix | Delete
end
[96] Fix | Delete
end
[97] Fix | Delete
end
[98] Fix | Delete
[99] Fix | Delete
#
[100] Fix | Delete
# Waits until any of the specified threads has terminated, and returns the one
[101] Fix | Delete
# that does.
[102] Fix | Delete
#
[103] Fix | Delete
# If there is no thread to wait, raises +ErrNoWaitingThread+. If +nonblock+
[104] Fix | Delete
# is true, and there is no terminated thread, raises +ErrNoFinishedThread+.
[105] Fix | Delete
#
[106] Fix | Delete
def next_wait(nonblock = nil)
[107] Fix | Delete
ThreadsWait.fail ErrNoWaitingThread if @threads.empty?
[108] Fix | Delete
begin
[109] Fix | Delete
@threads.delete(th = @wait_queue.pop(nonblock))
[110] Fix | Delete
th
[111] Fix | Delete
rescue ThreadError
[112] Fix | Delete
ThreadsWait.fail ErrNoFinishedThread
[113] Fix | Delete
end
[114] Fix | Delete
end
[115] Fix | Delete
[116] Fix | Delete
#
[117] Fix | Delete
# Waits until all of the specified threads are terminated. If a block is
[118] Fix | Delete
# supplied for the method, it is executed for each thread termination.
[119] Fix | Delete
#
[120] Fix | Delete
# Raises exceptions in the same manner as +next_wait+.
[121] Fix | Delete
#
[122] Fix | Delete
def all_waits
[123] Fix | Delete
until @threads.empty?
[124] Fix | Delete
th = next_wait
[125] Fix | Delete
yield th if block_given?
[126] Fix | Delete
end
[127] Fix | Delete
end
[128] Fix | Delete
end
[129] Fix | Delete
[130] Fix | Delete
##
[131] Fix | Delete
# An alias for ThreadsWait from thwait.rb
[132] Fix | Delete
[133] Fix | Delete
ThWait = ThreadsWait
[134] Fix | Delete
[135] Fix | Delete
# Documentation comments:
[136] Fix | Delete
# - Source of documentation is evenly split between Nutshell, existing
[137] Fix | Delete
# comments, and my own rephrasing.
[138] Fix | Delete
# - I'm not particularly confident that the comments are all exactly correct.
[139] Fix | Delete
[140] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function