Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: monitor.rb
=begin
[0] Fix | Delete
[1] Fix | Delete
= monitor.rb
[2] Fix | Delete
[3] Fix | Delete
Copyright (C) 2001 Shugo Maeda <shugo@ruby-lang.org>
[4] Fix | Delete
[5] Fix | Delete
This library is distributed under the terms of the Ruby license.
[6] Fix | Delete
You can freely distribute/modify this library.
[7] Fix | Delete
[8] Fix | Delete
== example
[9] Fix | Delete
[10] Fix | Delete
This is a simple example.
[11] Fix | Delete
[12] Fix | Delete
require 'monitor.rb'
[13] Fix | Delete
[14] Fix | Delete
buf = []
[15] Fix | Delete
buf.extend(MonitorMixin)
[16] Fix | Delete
empty_cond = buf.new_cond
[17] Fix | Delete
[18] Fix | Delete
# consumer
[19] Fix | Delete
Thread.start do
[20] Fix | Delete
loop do
[21] Fix | Delete
buf.synchronize do
[22] Fix | Delete
empty_cond.wait_while { buf.empty? }
[23] Fix | Delete
print buf.shift
[24] Fix | Delete
end
[25] Fix | Delete
end
[26] Fix | Delete
end
[27] Fix | Delete
[28] Fix | Delete
# producer
[29] Fix | Delete
while line = ARGF.gets
[30] Fix | Delete
buf.synchronize do
[31] Fix | Delete
buf.push(line)
[32] Fix | Delete
empty_cond.signal
[33] Fix | Delete
end
[34] Fix | Delete
end
[35] Fix | Delete
[36] Fix | Delete
The consumer thread waits for the producer thread to push a line
[37] Fix | Delete
to buf while buf.empty?, and the producer thread (main thread)
[38] Fix | Delete
reads a line from ARGF and push it to buf, then call
[39] Fix | Delete
empty_cond.signal.
[40] Fix | Delete
[41] Fix | Delete
=end
[42] Fix | Delete
[43] Fix | Delete
[44] Fix | Delete
#
[45] Fix | Delete
# Adds monitor functionality to an arbitrary object by mixing the module with
[46] Fix | Delete
# +include+. For example:
[47] Fix | Delete
#
[48] Fix | Delete
# require 'monitor.rb'
[49] Fix | Delete
#
[50] Fix | Delete
# buf = []
[51] Fix | Delete
# buf.extend(MonitorMixin)
[52] Fix | Delete
# empty_cond = buf.new_cond
[53] Fix | Delete
#
[54] Fix | Delete
# # consumer
[55] Fix | Delete
# Thread.start do
[56] Fix | Delete
# loop do
[57] Fix | Delete
# buf.synchronize do
[58] Fix | Delete
# empty_cond.wait_while { buf.empty? }
[59] Fix | Delete
# print buf.shift
[60] Fix | Delete
# end
[61] Fix | Delete
# end
[62] Fix | Delete
# end
[63] Fix | Delete
#
[64] Fix | Delete
# # producer
[65] Fix | Delete
# while line = ARGF.gets
[66] Fix | Delete
# buf.synchronize do
[67] Fix | Delete
# buf.push(line)
[68] Fix | Delete
# empty_cond.signal
[69] Fix | Delete
# end
[70] Fix | Delete
# end
[71] Fix | Delete
#
[72] Fix | Delete
# The consumer thread waits for the producer thread to push a line
[73] Fix | Delete
# to buf while buf.empty?, and the producer thread (main thread)
[74] Fix | Delete
# reads a line from ARGF and push it to buf, then call
[75] Fix | Delete
# empty_cond.signal.
[76] Fix | Delete
#
[77] Fix | Delete
module MonitorMixin
[78] Fix | Delete
#
[79] Fix | Delete
# FIXME: This isn't documented in Nutshell.
[80] Fix | Delete
#
[81] Fix | Delete
# Since MonitorMixin.new_cond returns a ConditionVariable, and the example
[82] Fix | Delete
# above calls while_wait and signal, this class should be documented.
[83] Fix | Delete
#
[84] Fix | Delete
class ConditionVariable
[85] Fix | Delete
class Timeout < Exception; end
[86] Fix | Delete
[87] Fix | Delete
# Create a new timer with the argument timeout, and add the
[88] Fix | Delete
# current thread to the list of waiters. Then the thread is
[89] Fix | Delete
# stopped. It will be resumed when a corresponding #signal
[90] Fix | Delete
# occurs.
[91] Fix | Delete
def wait(timeout = nil)
[92] Fix | Delete
@monitor.instance_eval {mon_check_owner()}
[93] Fix | Delete
timer = create_timer(timeout)
[94] Fix | Delete
[95] Fix | Delete
Thread.critical = true
[96] Fix | Delete
count = @monitor.instance_eval {mon_exit_for_cond()}
[97] Fix | Delete
@waiters.push(Thread.current)
[98] Fix | Delete
[99] Fix | Delete
begin
[100] Fix | Delete
Thread.stop
[101] Fix | Delete
return true
[102] Fix | Delete
rescue Timeout
[103] Fix | Delete
return false
[104] Fix | Delete
ensure
[105] Fix | Delete
Thread.critical = true
[106] Fix | Delete
begin
[107] Fix | Delete
if timer && timer.alive?
[108] Fix | Delete
Thread.kill(timer)
[109] Fix | Delete
end
[110] Fix | Delete
if @waiters.include?(Thread.current) # interrupted?
[111] Fix | Delete
@waiters.delete(Thread.current)
[112] Fix | Delete
end
[113] Fix | Delete
@monitor.instance_eval {mon_enter_for_cond(count)}
[114] Fix | Delete
ensure
[115] Fix | Delete
Thread.critical = false
[116] Fix | Delete
end
[117] Fix | Delete
end
[118] Fix | Delete
end
[119] Fix | Delete
[120] Fix | Delete
[121] Fix | Delete
# call #wait while the supplied block returns +true+.
[122] Fix | Delete
def wait_while
[123] Fix | Delete
while yield
[124] Fix | Delete
wait
[125] Fix | Delete
end
[126] Fix | Delete
end
[127] Fix | Delete
[128] Fix | Delete
# call #wait until the supplied block returns +true+.
[129] Fix | Delete
def wait_until
[130] Fix | Delete
until yield
[131] Fix | Delete
wait
[132] Fix | Delete
end
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
# Wake up and run the next waiter
[136] Fix | Delete
def signal
[137] Fix | Delete
@monitor.instance_eval {mon_check_owner()}
[138] Fix | Delete
Thread.critical = true
[139] Fix | Delete
t = @waiters.shift
[140] Fix | Delete
t.wakeup if t
[141] Fix | Delete
Thread.critical = false
[142] Fix | Delete
Thread.pass
[143] Fix | Delete
end
[144] Fix | Delete
[145] Fix | Delete
# Wake up all the waiters.
[146] Fix | Delete
def broadcast
[147] Fix | Delete
@monitor.instance_eval {mon_check_owner()}
[148] Fix | Delete
Thread.critical = true
[149] Fix | Delete
for t in @waiters
[150] Fix | Delete
t.wakeup
[151] Fix | Delete
end
[152] Fix | Delete
@waiters.clear
[153] Fix | Delete
Thread.critical = false
[154] Fix | Delete
Thread.pass
[155] Fix | Delete
end
[156] Fix | Delete
[157] Fix | Delete
def count_waiters
[158] Fix | Delete
return @waiters.length
[159] Fix | Delete
end
[160] Fix | Delete
[161] Fix | Delete
private
[162] Fix | Delete
[163] Fix | Delete
def initialize(monitor)
[164] Fix | Delete
@monitor = monitor
[165] Fix | Delete
@waiters = []
[166] Fix | Delete
end
[167] Fix | Delete
[168] Fix | Delete
def create_timer(timeout)
[169] Fix | Delete
if timeout
[170] Fix | Delete
waiter = Thread.current
[171] Fix | Delete
return Thread.start {
[172] Fix | Delete
Thread.pass
[173] Fix | Delete
sleep(timeout)
[174] Fix | Delete
Thread.critical = true
[175] Fix | Delete
waiter.raise(Timeout.new)
[176] Fix | Delete
}
[177] Fix | Delete
else
[178] Fix | Delete
return nil
[179] Fix | Delete
end
[180] Fix | Delete
end
[181] Fix | Delete
end
[182] Fix | Delete
[183] Fix | Delete
def self.extend_object(obj)
[184] Fix | Delete
super(obj)
[185] Fix | Delete
obj.instance_eval {mon_initialize()}
[186] Fix | Delete
end
[187] Fix | Delete
[188] Fix | Delete
#
[189] Fix | Delete
# Attempts to enter exclusive section. Returns +false+ if lock fails.
[190] Fix | Delete
#
[191] Fix | Delete
def mon_try_enter
[192] Fix | Delete
result = false
[193] Fix | Delete
Thread.critical = true
[194] Fix | Delete
if @mon_owner.nil?
[195] Fix | Delete
@mon_owner = Thread.current
[196] Fix | Delete
end
[197] Fix | Delete
if @mon_owner == Thread.current
[198] Fix | Delete
@mon_count += 1
[199] Fix | Delete
result = true
[200] Fix | Delete
end
[201] Fix | Delete
Thread.critical = false
[202] Fix | Delete
return result
[203] Fix | Delete
end
[204] Fix | Delete
# For backward compatibility
[205] Fix | Delete
alias try_mon_enter mon_try_enter
[206] Fix | Delete
[207] Fix | Delete
#
[208] Fix | Delete
# Enters exclusive section.
[209] Fix | Delete
#
[210] Fix | Delete
def mon_enter
[211] Fix | Delete
Thread.critical = true
[212] Fix | Delete
mon_acquire(@mon_entering_queue)
[213] Fix | Delete
@mon_count += 1
[214] Fix | Delete
ensure
[215] Fix | Delete
Thread.critical = false
[216] Fix | Delete
end
[217] Fix | Delete
[218] Fix | Delete
#
[219] Fix | Delete
# Leaves exclusive section.
[220] Fix | Delete
#
[221] Fix | Delete
def mon_exit
[222] Fix | Delete
mon_check_owner
[223] Fix | Delete
Thread.critical = true
[224] Fix | Delete
@mon_count -= 1
[225] Fix | Delete
if @mon_count == 0
[226] Fix | Delete
mon_release
[227] Fix | Delete
end
[228] Fix | Delete
Thread.critical = false
[229] Fix | Delete
Thread.pass
[230] Fix | Delete
end
[231] Fix | Delete
[232] Fix | Delete
#
[233] Fix | Delete
# Enters exclusive section and executes the block. Leaves the exclusive
[234] Fix | Delete
# section automatically when the block exits. See example under
[235] Fix | Delete
# +MonitorMixin+.
[236] Fix | Delete
#
[237] Fix | Delete
def mon_synchronize
[238] Fix | Delete
mon_enter
[239] Fix | Delete
begin
[240] Fix | Delete
yield
[241] Fix | Delete
ensure
[242] Fix | Delete
mon_exit
[243] Fix | Delete
end
[244] Fix | Delete
end
[245] Fix | Delete
alias synchronize mon_synchronize
[246] Fix | Delete
[247] Fix | Delete
#
[248] Fix | Delete
# FIXME: This isn't documented in Nutshell.
[249] Fix | Delete
#
[250] Fix | Delete
# Create a new condition variable for this monitor.
[251] Fix | Delete
# This facilitates control of the monitor with #signal and #wait.
[252] Fix | Delete
#
[253] Fix | Delete
def new_cond
[254] Fix | Delete
return ConditionVariable.new(self)
[255] Fix | Delete
end
[256] Fix | Delete
[257] Fix | Delete
private
[258] Fix | Delete
[259] Fix | Delete
def initialize(*args)
[260] Fix | Delete
super
[261] Fix | Delete
mon_initialize
[262] Fix | Delete
end
[263] Fix | Delete
[264] Fix | Delete
# called by initialize method to set defaults for instance variables.
[265] Fix | Delete
def mon_initialize
[266] Fix | Delete
@mon_owner = nil
[267] Fix | Delete
@mon_count = 0
[268] Fix | Delete
@mon_entering_queue = []
[269] Fix | Delete
@mon_waiting_queue = []
[270] Fix | Delete
end
[271] Fix | Delete
[272] Fix | Delete
# Throw a ThreadError exception if the current thread
[273] Fix | Delete
# does't own the monitor
[274] Fix | Delete
def mon_check_owner
[275] Fix | Delete
if @mon_owner != Thread.current
[276] Fix | Delete
raise ThreadError, "current thread not owner"
[277] Fix | Delete
end
[278] Fix | Delete
end
[279] Fix | Delete
[280] Fix | Delete
def mon_acquire(queue)
[281] Fix | Delete
while @mon_owner && @mon_owner != Thread.current
[282] Fix | Delete
queue.push(Thread.current)
[283] Fix | Delete
Thread.stop
[284] Fix | Delete
Thread.critical = true
[285] Fix | Delete
end
[286] Fix | Delete
@mon_owner = Thread.current
[287] Fix | Delete
end
[288] Fix | Delete
[289] Fix | Delete
# mon_release requires Thread.critical == true
[290] Fix | Delete
def mon_release
[291] Fix | Delete
@mon_owner = nil
[292] Fix | Delete
while t = @mon_waiting_queue.shift || @mon_entering_queue.shift
[293] Fix | Delete
if t.alive?
[294] Fix | Delete
t.wakeup
[295] Fix | Delete
return
[296] Fix | Delete
end
[297] Fix | Delete
end
[298] Fix | Delete
end
[299] Fix | Delete
[300] Fix | Delete
def mon_enter_for_cond(count)
[301] Fix | Delete
mon_acquire(@mon_waiting_queue)
[302] Fix | Delete
@mon_count = count
[303] Fix | Delete
end
[304] Fix | Delete
[305] Fix | Delete
def mon_exit_for_cond
[306] Fix | Delete
count = @mon_count
[307] Fix | Delete
@mon_count = 0
[308] Fix | Delete
return count
[309] Fix | Delete
ensure
[310] Fix | Delete
mon_release
[311] Fix | Delete
end
[312] Fix | Delete
end
[313] Fix | Delete
[314] Fix | Delete
# Monitors provide means of mutual exclusion for Thread programming.
[315] Fix | Delete
# A critical region is created by means of the synchronize method,
[316] Fix | Delete
# which takes a block.
[317] Fix | Delete
# The condition variables (created with #new_cond) may be used
[318] Fix | Delete
# to control the execution of a monitor with #signal and #wait.
[319] Fix | Delete
#
[320] Fix | Delete
# the Monitor class wraps MonitorMixin, and provides aliases
[321] Fix | Delete
# alias try_enter try_mon_enter
[322] Fix | Delete
# alias enter mon_enter
[323] Fix | Delete
# alias exit mon_exit
[324] Fix | Delete
# to access its methods more concisely.
[325] Fix | Delete
class Monitor
[326] Fix | Delete
include MonitorMixin
[327] Fix | Delete
alias try_enter try_mon_enter
[328] Fix | Delete
alias enter mon_enter
[329] Fix | Delete
alias exit mon_exit
[330] Fix | Delete
end
[331] Fix | Delete
[332] Fix | Delete
[333] Fix | Delete
# Documentation comments:
[334] Fix | Delete
# - All documentation comes from Nutshell.
[335] Fix | Delete
# - MonitorMixin.new_cond appears in the example, but is not documented in
[336] Fix | Delete
# Nutshell.
[337] Fix | Delete
# - All the internals (internal modules Accessible and Initializable, class
[338] Fix | Delete
# ConditionVariable) appear in RDoc. It might be good to hide them, by
[339] Fix | Delete
# making them private, or marking them :nodoc:, etc.
[340] Fix | Delete
# - The entire example from the RD section at the top is replicated in the RDoc
[341] Fix | Delete
# comment for MonitorMixin. Does the RD section need to remain?
[342] Fix | Delete
# - RDoc doesn't recognise aliases, so we have mon_synchronize documented, but
[343] Fix | Delete
# not synchronize.
[344] Fix | Delete
# - mon_owner is in Nutshell, but appears as an accessor in a separate module
[345] Fix | Delete
# here, so is hard/impossible to RDoc. Some other useful accessors
[346] Fix | Delete
# (mon_count and some queue stuff) are also in this module, and don't appear
[347] Fix | Delete
# directly in the RDoc output.
[348] Fix | Delete
# - in short, it may be worth changing the code layout in this file to make the
[349] Fix | Delete
# documentation easier
[350] Fix | Delete
[351] Fix | Delete
# Local variables:
[352] Fix | Delete
# mode: Ruby
[353] Fix | Delete
# tab-width: 8
[354] Fix | Delete
# End:
[355] Fix | Delete
[356] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function