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