Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: monitor.rb
# = monitor.rb
[0] Fix | Delete
#
[1] Fix | Delete
# Copyright (C) 2001 Shugo Maeda <shugo@ruby-lang.org>
[2] Fix | Delete
#
[3] Fix | Delete
# This library is distributed under the terms of the Ruby license.
[4] Fix | Delete
# You can freely distribute/modify this library.
[5] Fix | Delete
#
[6] Fix | Delete
[7] Fix | Delete
require 'thread'
[8] Fix | Delete
[9] Fix | Delete
#
[10] Fix | Delete
# In concurrent programming, a monitor is an object or module intended to be
[11] Fix | Delete
# used safely by more than one thread. The defining characteristic of a
[12] Fix | Delete
# monitor is that its methods are executed with mutual exclusion. That is, at
[13] Fix | Delete
# each point in time, at most one thread may be executing any of its methods.
[14] Fix | Delete
# This mutual exclusion greatly simplifies reasoning about the implementation
[15] Fix | Delete
# of monitors compared to reasoning about parallel code that updates a data
[16] Fix | Delete
# structure.
[17] Fix | Delete
#
[18] Fix | Delete
# You can read more about the general principles on the Wikipedia page for
[19] Fix | Delete
# Monitors[http://en.wikipedia.org/wiki/Monitor_%28synchronization%29]
[20] Fix | Delete
#
[21] Fix | Delete
# == Examples
[22] Fix | Delete
#
[23] Fix | Delete
# === Simple object.extend
[24] Fix | Delete
#
[25] Fix | Delete
# require 'monitor.rb'
[26] Fix | Delete
#
[27] Fix | Delete
# buf = []
[28] Fix | Delete
# buf.extend(MonitorMixin)
[29] Fix | Delete
# empty_cond = buf.new_cond
[30] Fix | Delete
#
[31] Fix | Delete
# # consumer
[32] Fix | Delete
# Thread.start do
[33] Fix | Delete
# loop do
[34] Fix | Delete
# buf.synchronize do
[35] Fix | Delete
# empty_cond.wait_while { buf.empty? }
[36] Fix | Delete
# print buf.shift
[37] Fix | Delete
# end
[38] Fix | Delete
# end
[39] Fix | Delete
# end
[40] Fix | Delete
#
[41] Fix | Delete
# # producer
[42] Fix | Delete
# while line = ARGF.gets
[43] Fix | Delete
# buf.synchronize do
[44] Fix | Delete
# buf.push(line)
[45] Fix | Delete
# empty_cond.signal
[46] Fix | Delete
# end
[47] Fix | Delete
# end
[48] Fix | Delete
#
[49] Fix | Delete
# The consumer thread waits for the producer thread to push a line to buf
[50] Fix | Delete
# while <tt>buf.empty?</tt>. The producer thread (main thread) reads a
[51] Fix | Delete
# line from ARGF and pushes it into buf then calls <tt>empty_cond.signal</tt>
[52] Fix | Delete
# to notify the consumer thread of new data.
[53] Fix | Delete
#
[54] Fix | Delete
# === Simple Class include
[55] Fix | Delete
#
[56] Fix | Delete
# require 'monitor'
[57] Fix | Delete
#
[58] Fix | Delete
# class SynchronizedArray < Array
[59] Fix | Delete
#
[60] Fix | Delete
# include MonitorMixin
[61] Fix | Delete
#
[62] Fix | Delete
# def initialize(*args)
[63] Fix | Delete
# super(*args)
[64] Fix | Delete
# end
[65] Fix | Delete
#
[66] Fix | Delete
# alias :old_shift :shift
[67] Fix | Delete
# alias :old_unshift :unshift
[68] Fix | Delete
#
[69] Fix | Delete
# def shift(n=1)
[70] Fix | Delete
# self.synchronize do
[71] Fix | Delete
# self.old_shift(n)
[72] Fix | Delete
# end
[73] Fix | Delete
# end
[74] Fix | Delete
#
[75] Fix | Delete
# def unshift(item)
[76] Fix | Delete
# self.synchronize do
[77] Fix | Delete
# self.old_unshift(item)
[78] Fix | Delete
# end
[79] Fix | Delete
# end
[80] Fix | Delete
#
[81] Fix | Delete
# # other methods ...
[82] Fix | Delete
# end
[83] Fix | Delete
#
[84] Fix | Delete
# +SynchronizedArray+ implements an Array with synchronized access to items.
[85] Fix | Delete
# This Class is implemented as subclass of Array which includes the
[86] Fix | Delete
# MonitorMixin module.
[87] Fix | Delete
#
[88] Fix | Delete
module MonitorMixin
[89] Fix | Delete
#
[90] Fix | Delete
# FIXME: This isn't documented in Nutshell.
[91] Fix | Delete
#
[92] Fix | Delete
# Since MonitorMixin.new_cond returns a ConditionVariable, and the example
[93] Fix | Delete
# above calls while_wait and signal, this class should be documented.
[94] Fix | Delete
#
[95] Fix | Delete
class ConditionVariable
[96] Fix | Delete
class Timeout < Exception; end
[97] Fix | Delete
[98] Fix | Delete
#
[99] Fix | Delete
# Releases the lock held in the associated monitor and waits; reacquires the lock on wakeup.
[100] Fix | Delete
#
[101] Fix | Delete
# If +timeout+ is given, this method returns after +timeout+ seconds passed,
[102] Fix | Delete
# even if no other thread doesn't signal.
[103] Fix | Delete
#
[104] Fix | Delete
def wait(timeout = nil)
[105] Fix | Delete
@monitor.__send__(:mon_check_owner)
[106] Fix | Delete
count = @monitor.__send__(:mon_exit_for_cond)
[107] Fix | Delete
begin
[108] Fix | Delete
@cond.wait(@monitor.instance_variable_get(:@mon_mutex), timeout)
[109] Fix | Delete
return true
[110] Fix | Delete
ensure
[111] Fix | Delete
@monitor.__send__(:mon_enter_for_cond, count)
[112] Fix | Delete
end
[113] Fix | Delete
end
[114] Fix | Delete
[115] Fix | Delete
#
[116] Fix | Delete
# Calls wait repeatedly while the given block yields a truthy value.
[117] Fix | Delete
#
[118] Fix | Delete
def wait_while
[119] Fix | Delete
while yield
[120] Fix | Delete
wait
[121] Fix | Delete
end
[122] Fix | Delete
end
[123] Fix | Delete
[124] Fix | Delete
#
[125] Fix | Delete
# Calls wait repeatedly until the given block yields a truthy value.
[126] Fix | Delete
#
[127] Fix | Delete
def wait_until
[128] Fix | Delete
until yield
[129] Fix | Delete
wait
[130] Fix | Delete
end
[131] Fix | Delete
end
[132] Fix | Delete
[133] Fix | Delete
#
[134] Fix | Delete
# Wakes up the first thread in line waiting for this lock.
[135] Fix | Delete
#
[136] Fix | Delete
def signal
[137] Fix | Delete
@monitor.__send__(:mon_check_owner)
[138] Fix | Delete
@cond.signal
[139] Fix | Delete
end
[140] Fix | Delete
[141] Fix | Delete
#
[142] Fix | Delete
# Wakes up all threads waiting for this lock.
[143] Fix | Delete
#
[144] Fix | Delete
def broadcast
[145] Fix | Delete
@monitor.__send__(:mon_check_owner)
[146] Fix | Delete
@cond.broadcast
[147] Fix | Delete
end
[148] Fix | Delete
[149] Fix | Delete
private
[150] Fix | Delete
[151] Fix | Delete
def initialize(monitor)
[152] Fix | Delete
@monitor = monitor
[153] Fix | Delete
@cond = ::ConditionVariable.new
[154] Fix | Delete
end
[155] Fix | Delete
end
[156] Fix | Delete
[157] Fix | Delete
def self.extend_object(obj)
[158] Fix | Delete
super(obj)
[159] Fix | Delete
obj.__send__(:mon_initialize)
[160] Fix | Delete
end
[161] Fix | Delete
[162] Fix | Delete
#
[163] Fix | Delete
# Attempts to enter exclusive section. Returns +false+ if lock fails.
[164] Fix | Delete
#
[165] Fix | Delete
def mon_try_enter
[166] Fix | Delete
if @mon_owner != Thread.current
[167] Fix | Delete
unless @mon_mutex.try_lock
[168] Fix | Delete
return false
[169] Fix | Delete
end
[170] Fix | Delete
@mon_owner = Thread.current
[171] Fix | Delete
end
[172] Fix | Delete
@mon_count += 1
[173] Fix | Delete
return true
[174] Fix | Delete
end
[175] Fix | Delete
# For backward compatibility
[176] Fix | Delete
alias try_mon_enter mon_try_enter
[177] Fix | Delete
[178] Fix | Delete
#
[179] Fix | Delete
# Enters exclusive section.
[180] Fix | Delete
#
[181] Fix | Delete
def mon_enter
[182] Fix | Delete
if @mon_owner != Thread.current
[183] Fix | Delete
@mon_mutex.lock
[184] Fix | Delete
@mon_owner = Thread.current
[185] Fix | Delete
end
[186] Fix | Delete
@mon_count += 1
[187] Fix | Delete
end
[188] Fix | Delete
[189] Fix | Delete
#
[190] Fix | Delete
# Leaves exclusive section.
[191] Fix | Delete
#
[192] Fix | Delete
def mon_exit
[193] Fix | Delete
mon_check_owner
[194] Fix | Delete
@mon_count -=1
[195] Fix | Delete
if @mon_count == 0
[196] Fix | Delete
@mon_owner = nil
[197] Fix | Delete
@mon_mutex.unlock
[198] Fix | Delete
end
[199] Fix | Delete
end
[200] Fix | Delete
[201] Fix | Delete
#
[202] Fix | Delete
# Enters exclusive section and executes the block. Leaves the exclusive
[203] Fix | Delete
# section automatically when the block exits. See example under
[204] Fix | Delete
# +MonitorMixin+.
[205] Fix | Delete
#
[206] Fix | Delete
def mon_synchronize
[207] Fix | Delete
mon_enter
[208] Fix | Delete
begin
[209] Fix | Delete
yield
[210] Fix | Delete
ensure
[211] Fix | Delete
mon_exit
[212] Fix | Delete
end
[213] Fix | Delete
end
[214] Fix | Delete
alias synchronize mon_synchronize
[215] Fix | Delete
[216] Fix | Delete
#
[217] Fix | Delete
# Creates a new MonitorMixin::ConditionVariable associated with the
[218] Fix | Delete
# receiver.
[219] Fix | Delete
#
[220] Fix | Delete
def new_cond
[221] Fix | Delete
return ConditionVariable.new(self)
[222] Fix | Delete
end
[223] Fix | Delete
[224] Fix | Delete
private
[225] Fix | Delete
[226] Fix | Delete
# Use <tt>extend MonitorMixin</tt> or <tt>include MonitorMixin</tt> instead
[227] Fix | Delete
# of this constructor. Have look at the examples above to understand how to
[228] Fix | Delete
# use this module.
[229] Fix | Delete
def initialize(*args)
[230] Fix | Delete
super
[231] Fix | Delete
mon_initialize
[232] Fix | Delete
end
[233] Fix | Delete
[234] Fix | Delete
# Initializes the MonitorMixin after being included in a class or when an
[235] Fix | Delete
# object has been extended with the MonitorMixin
[236] Fix | Delete
def mon_initialize
[237] Fix | Delete
@mon_owner = nil
[238] Fix | Delete
@mon_count = 0
[239] Fix | Delete
@mon_mutex = Mutex.new
[240] Fix | Delete
end
[241] Fix | Delete
[242] Fix | Delete
def mon_check_owner
[243] Fix | Delete
if @mon_owner != Thread.current
[244] Fix | Delete
raise ThreadError, "current thread not owner"
[245] Fix | Delete
end
[246] Fix | Delete
end
[247] Fix | Delete
[248] Fix | Delete
def mon_enter_for_cond(count)
[249] Fix | Delete
@mon_owner = Thread.current
[250] Fix | Delete
@mon_count = count
[251] Fix | Delete
end
[252] Fix | Delete
[253] Fix | Delete
def mon_exit_for_cond
[254] Fix | Delete
count = @mon_count
[255] Fix | Delete
@mon_owner = nil
[256] Fix | Delete
@mon_count = 0
[257] Fix | Delete
return count
[258] Fix | Delete
end
[259] Fix | Delete
end
[260] Fix | Delete
[261] Fix | Delete
# Use the Monitor class when you want to have a lock object for blocks with
[262] Fix | Delete
# mutual exclusion.
[263] Fix | Delete
#
[264] Fix | Delete
# require 'monitor'
[265] Fix | Delete
#
[266] Fix | Delete
# lock = Monitor.new
[267] Fix | Delete
# lock.synchronize do
[268] Fix | Delete
# # exclusive access
[269] Fix | Delete
# end
[270] Fix | Delete
#
[271] Fix | Delete
class Monitor
[272] Fix | Delete
include MonitorMixin
[273] Fix | Delete
alias try_enter try_mon_enter
[274] Fix | Delete
alias enter mon_enter
[275] Fix | Delete
alias exit mon_exit
[276] Fix | Delete
end
[277] Fix | Delete
[278] Fix | Delete
[279] Fix | Delete
# Documentation comments:
[280] Fix | Delete
# - All documentation comes from Nutshell.
[281] Fix | Delete
# - MonitorMixin.new_cond appears in the example, but is not documented in
[282] Fix | Delete
# Nutshell.
[283] Fix | Delete
# - All the internals (internal modules Accessible and Initializable, class
[284] Fix | Delete
# ConditionVariable) appear in RDoc. It might be good to hide them, by
[285] Fix | Delete
# making them private, or marking them :nodoc:, etc.
[286] Fix | Delete
# - RDoc doesn't recognise aliases, so we have mon_synchronize documented, but
[287] Fix | Delete
# not synchronize.
[288] Fix | Delete
# - mon_owner is in Nutshell, but appears as an accessor in a separate module
[289] Fix | Delete
# here, so is hard/impossible to RDoc. Some other useful accessors
[290] Fix | Delete
# (mon_count and some queue stuff) are also in this module, and don't appear
[291] Fix | Delete
# directly in the RDoc output.
[292] Fix | Delete
# - in short, it may be worth changing the code layout in this file to make the
[293] Fix | Delete
# documentation easier
[294] Fix | Delete
[295] Fix | Delete
# Local variables:
[296] Fix | Delete
# mode: Ruby
[297] Fix | Delete
# tab-width: 8
[298] Fix | Delete
# End:
[299] Fix | Delete
[300] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function