Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/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[https://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
[88] Fix | Delete
require 'monitor.so'
[89] Fix | Delete
[90] Fix | Delete
module MonitorMixin
[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
#
[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.mon_check_owner
[106] Fix | Delete
@monitor.wait_for_cond(@cond, timeout)
[107] Fix | Delete
end
[108] Fix | Delete
[109] Fix | Delete
#
[110] Fix | Delete
# Calls wait repeatedly while the given block yields a truthy value.
[111] Fix | Delete
#
[112] Fix | Delete
def wait_while
[113] Fix | Delete
while yield
[114] Fix | Delete
wait
[115] Fix | Delete
end
[116] Fix | Delete
end
[117] Fix | Delete
[118] Fix | Delete
#
[119] Fix | Delete
# Calls wait repeatedly until the given block yields a truthy value.
[120] Fix | Delete
#
[121] Fix | Delete
def wait_until
[122] Fix | Delete
until yield
[123] Fix | Delete
wait
[124] Fix | Delete
end
[125] Fix | Delete
end
[126] Fix | Delete
[127] Fix | Delete
#
[128] Fix | Delete
# Wakes up the first thread in line waiting for this lock.
[129] Fix | Delete
#
[130] Fix | Delete
def signal
[131] Fix | Delete
@monitor.mon_check_owner
[132] Fix | Delete
@cond.signal
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
#
[136] Fix | Delete
# Wakes up all threads waiting for this lock.
[137] Fix | Delete
#
[138] Fix | Delete
def broadcast
[139] Fix | Delete
@monitor.mon_check_owner
[140] Fix | Delete
@cond.broadcast
[141] Fix | Delete
end
[142] Fix | Delete
[143] Fix | Delete
private
[144] Fix | Delete
[145] Fix | Delete
def initialize(monitor)
[146] Fix | Delete
@monitor = monitor
[147] Fix | Delete
@cond = Thread::ConditionVariable.new
[148] Fix | Delete
end
[149] Fix | Delete
end
[150] Fix | Delete
[151] Fix | Delete
def self.extend_object(obj)
[152] Fix | Delete
super(obj)
[153] Fix | Delete
obj.__send__(:mon_initialize)
[154] Fix | Delete
end
[155] Fix | Delete
[156] Fix | Delete
#
[157] Fix | Delete
# Attempts to enter exclusive section. Returns +false+ if lock fails.
[158] Fix | Delete
#
[159] Fix | Delete
def mon_try_enter
[160] Fix | Delete
@mon_data.try_enter
[161] Fix | Delete
end
[162] Fix | Delete
# For backward compatibility
[163] Fix | Delete
alias try_mon_enter mon_try_enter
[164] Fix | Delete
[165] Fix | Delete
#
[166] Fix | Delete
# Enters exclusive section.
[167] Fix | Delete
#
[168] Fix | Delete
def mon_enter
[169] Fix | Delete
@mon_data.enter
[170] Fix | Delete
end
[171] Fix | Delete
[172] Fix | Delete
#
[173] Fix | Delete
# Leaves exclusive section.
[174] Fix | Delete
#
[175] Fix | Delete
def mon_exit
[176] Fix | Delete
mon_check_owner
[177] Fix | Delete
@mon_data.exit
[178] Fix | Delete
end
[179] Fix | Delete
[180] Fix | Delete
#
[181] Fix | Delete
# Returns true if this monitor is locked by any thread
[182] Fix | Delete
#
[183] Fix | Delete
def mon_locked?
[184] Fix | Delete
@mon_data.mon_locked?
[185] Fix | Delete
end
[186] Fix | Delete
[187] Fix | Delete
#
[188] Fix | Delete
# Returns true if this monitor is locked by current thread.
[189] Fix | Delete
#
[190] Fix | Delete
def mon_owned?
[191] Fix | Delete
@mon_data.mon_owned?
[192] Fix | Delete
end
[193] Fix | Delete
[194] Fix | Delete
#
[195] Fix | Delete
# Enters exclusive section and executes the block. Leaves the exclusive
[196] Fix | Delete
# section automatically when the block exits. See example under
[197] Fix | Delete
# +MonitorMixin+.
[198] Fix | Delete
#
[199] Fix | Delete
def mon_synchronize(&b)
[200] Fix | Delete
@mon_data.synchronize(&b)
[201] Fix | Delete
end
[202] Fix | Delete
alias synchronize mon_synchronize
[203] Fix | Delete
[204] Fix | Delete
#
[205] Fix | Delete
# Creates a new MonitorMixin::ConditionVariable associated with the
[206] Fix | Delete
# Monitor object.
[207] Fix | Delete
#
[208] Fix | Delete
def new_cond
[209] Fix | Delete
unless defined?(@mon_data)
[210] Fix | Delete
mon_initialize
[211] Fix | Delete
@mon_initialized_by_new_cond = true
[212] Fix | Delete
end
[213] Fix | Delete
return ConditionVariable.new(@mon_data)
[214] Fix | Delete
end
[215] Fix | Delete
[216] Fix | Delete
private
[217] Fix | Delete
[218] Fix | Delete
# Use <tt>extend MonitorMixin</tt> or <tt>include MonitorMixin</tt> instead
[219] Fix | Delete
# of this constructor. Have look at the examples above to understand how to
[220] Fix | Delete
# use this module.
[221] Fix | Delete
def initialize(...)
[222] Fix | Delete
super
[223] Fix | Delete
mon_initialize
[224] Fix | Delete
end
[225] Fix | Delete
[226] Fix | Delete
# Initializes the MonitorMixin after being included in a class or when an
[227] Fix | Delete
# object has been extended with the MonitorMixin
[228] Fix | Delete
def mon_initialize
[229] Fix | Delete
if defined?(@mon_data)
[230] Fix | Delete
if defined?(@mon_initialized_by_new_cond)
[231] Fix | Delete
return # already initialized.
[232] Fix | Delete
elsif @mon_data_owner_object_id == self.object_id
[233] Fix | Delete
raise ThreadError, "already initialized"
[234] Fix | Delete
end
[235] Fix | Delete
end
[236] Fix | Delete
@mon_data = ::Monitor.new
[237] Fix | Delete
@mon_data_owner_object_id = self.object_id
[238] Fix | Delete
end
[239] Fix | Delete
[240] Fix | Delete
def mon_check_owner
[241] Fix | Delete
@mon_data.mon_check_owner
[242] Fix | Delete
end
[243] Fix | Delete
end
[244] Fix | Delete
[245] Fix | Delete
# Use the Monitor class when you want to have a lock object for blocks with
[246] Fix | Delete
# mutual exclusion.
[247] Fix | Delete
#
[248] Fix | Delete
# require 'monitor'
[249] Fix | Delete
#
[250] Fix | Delete
# lock = Monitor.new
[251] Fix | Delete
# lock.synchronize do
[252] Fix | Delete
# # exclusive access
[253] Fix | Delete
# end
[254] Fix | Delete
#
[255] Fix | Delete
class Monitor
[256] Fix | Delete
def new_cond
[257] Fix | Delete
::MonitorMixin::ConditionVariable.new(self)
[258] Fix | Delete
end
[259] Fix | Delete
[260] Fix | Delete
# for compatibility
[261] Fix | Delete
alias try_mon_enter try_enter
[262] Fix | Delete
alias mon_try_enter try_enter
[263] Fix | Delete
alias mon_enter enter
[264] Fix | Delete
alias mon_exit exit
[265] Fix | Delete
alias mon_synchronize synchronize
[266] Fix | Delete
end
[267] Fix | Delete
[268] Fix | Delete
# Documentation comments:
[269] Fix | Delete
# - All documentation comes from Nutshell.
[270] Fix | Delete
# - MonitorMixin.new_cond appears in the example, but is not documented in
[271] Fix | Delete
# Nutshell.
[272] Fix | Delete
# - All the internals (internal modules Accessible and Initializable, class
[273] Fix | Delete
# ConditionVariable) appear in RDoc. It might be good to hide them, by
[274] Fix | Delete
# making them private, or marking them :nodoc:, etc.
[275] Fix | Delete
# - RDoc doesn't recognise aliases, so we have mon_synchronize documented, but
[276] Fix | Delete
# not synchronize.
[277] Fix | Delete
# - mon_owner is in Nutshell, but appears as an accessor in a separate module
[278] Fix | Delete
# here, so is hard/impossible to RDoc. Some other useful accessors
[279] Fix | Delete
# (mon_count and some queue stuff) are also in this module, and don't appear
[280] Fix | Delete
# directly in the RDoc output.
[281] Fix | Delete
# - in short, it may be worth changing the code layout in this file to make the
[282] Fix | Delete
# documentation easier
[283] Fix | Delete
[284] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function