Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/share/ruby
File: sync.rb
# frozen_string_literal: false
[0] Fix | Delete
#
[1] Fix | Delete
# sync.rb - 2 phase lock with counter
[2] Fix | Delete
# $Release Version: 1.0$
[3] Fix | Delete
# $Revision: 56183 $
[4] Fix | Delete
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
[5] Fix | Delete
#
[6] Fix | Delete
# --
[7] Fix | Delete
# Sync_m, Synchronizer_m
[8] Fix | Delete
# Usage:
[9] Fix | Delete
# obj.extend(Sync_m)
[10] Fix | Delete
# or
[11] Fix | Delete
# class Foo
[12] Fix | Delete
# include Sync_m
[13] Fix | Delete
# :
[14] Fix | Delete
# end
[15] Fix | Delete
#
[16] Fix | Delete
# Sync_m#sync_mode
[17] Fix | Delete
# Sync_m#sync_locked?, locked?
[18] Fix | Delete
# Sync_m#sync_shared?, shared?
[19] Fix | Delete
# Sync_m#sync_exclusive?, sync_exclusive?
[20] Fix | Delete
# Sync_m#sync_try_lock, try_lock
[21] Fix | Delete
# Sync_m#sync_lock, lock
[22] Fix | Delete
# Sync_m#sync_unlock, unlock
[23] Fix | Delete
#
[24] Fix | Delete
# Sync, Synchronizer:
[25] Fix | Delete
# Usage:
[26] Fix | Delete
# sync = Sync.new
[27] Fix | Delete
#
[28] Fix | Delete
# Sync#mode
[29] Fix | Delete
# Sync#locked?
[30] Fix | Delete
# Sync#shared?
[31] Fix | Delete
# Sync#exclusive?
[32] Fix | Delete
# Sync#try_lock(mode) -- mode = :EX, :SH, :UN
[33] Fix | Delete
# Sync#lock(mode) -- mode = :EX, :SH, :UN
[34] Fix | Delete
# Sync#unlock
[35] Fix | Delete
# Sync#synchronize(mode) {...}
[36] Fix | Delete
#
[37] Fix | Delete
#
[38] Fix | Delete
[39] Fix | Delete
unless defined? Thread
[40] Fix | Delete
raise "Thread not available for this ruby interpreter"
[41] Fix | Delete
end
[42] Fix | Delete
[43] Fix | Delete
##
[44] Fix | Delete
# A module that provides a two-phase lock with a counter.
[45] Fix | Delete
[46] Fix | Delete
module Sync_m
[47] Fix | Delete
# lock mode
[48] Fix | Delete
UN = :UN
[49] Fix | Delete
SH = :SH
[50] Fix | Delete
EX = :EX
[51] Fix | Delete
[52] Fix | Delete
# exceptions
[53] Fix | Delete
class Err < StandardError
[54] Fix | Delete
def Err.Fail(*opt)
[55] Fix | Delete
fail self, sprintf(self::Message, *opt)
[56] Fix | Delete
end
[57] Fix | Delete
[58] Fix | Delete
class UnknownLocker < Err
[59] Fix | Delete
Message = "Thread(%s) not locked."
[60] Fix | Delete
def UnknownLocker.Fail(th)
[61] Fix | Delete
super(th.inspect)
[62] Fix | Delete
end
[63] Fix | Delete
end
[64] Fix | Delete
[65] Fix | Delete
class LockModeFailer < Err
[66] Fix | Delete
Message = "Unknown lock mode(%s)"
[67] Fix | Delete
def LockModeFailer.Fail(mode)
[68] Fix | Delete
if mode.id2name
[69] Fix | Delete
mode = mode.id2name
[70] Fix | Delete
end
[71] Fix | Delete
super(mode)
[72] Fix | Delete
end
[73] Fix | Delete
end
[74] Fix | Delete
end
[75] Fix | Delete
[76] Fix | Delete
def Sync_m.define_aliases(cl)
[77] Fix | Delete
cl.module_eval %q{
[78] Fix | Delete
alias locked? sync_locked?
[79] Fix | Delete
alias shared? sync_shared?
[80] Fix | Delete
alias exclusive? sync_exclusive?
[81] Fix | Delete
alias lock sync_lock
[82] Fix | Delete
alias unlock sync_unlock
[83] Fix | Delete
alias try_lock sync_try_lock
[84] Fix | Delete
alias synchronize sync_synchronize
[85] Fix | Delete
}
[86] Fix | Delete
end
[87] Fix | Delete
[88] Fix | Delete
def Sync_m.append_features(cl)
[89] Fix | Delete
super
[90] Fix | Delete
# do nothing for Modules
[91] Fix | Delete
# make aliases for Classes.
[92] Fix | Delete
define_aliases(cl) unless cl.instance_of?(Module)
[93] Fix | Delete
self
[94] Fix | Delete
end
[95] Fix | Delete
[96] Fix | Delete
def Sync_m.extend_object(obj)
[97] Fix | Delete
super
[98] Fix | Delete
obj.sync_extend
[99] Fix | Delete
end
[100] Fix | Delete
[101] Fix | Delete
def sync_extend
[102] Fix | Delete
unless (defined? locked? and
[103] Fix | Delete
defined? shared? and
[104] Fix | Delete
defined? exclusive? and
[105] Fix | Delete
defined? lock and
[106] Fix | Delete
defined? unlock and
[107] Fix | Delete
defined? try_lock and
[108] Fix | Delete
defined? synchronize)
[109] Fix | Delete
Sync_m.define_aliases(singleton_class)
[110] Fix | Delete
end
[111] Fix | Delete
sync_initialize
[112] Fix | Delete
end
[113] Fix | Delete
[114] Fix | Delete
# accessing
[115] Fix | Delete
def sync_locked?
[116] Fix | Delete
sync_mode != UN
[117] Fix | Delete
end
[118] Fix | Delete
[119] Fix | Delete
def sync_shared?
[120] Fix | Delete
sync_mode == SH
[121] Fix | Delete
end
[122] Fix | Delete
[123] Fix | Delete
def sync_exclusive?
[124] Fix | Delete
sync_mode == EX
[125] Fix | Delete
end
[126] Fix | Delete
[127] Fix | Delete
# locking methods.
[128] Fix | Delete
def sync_try_lock(mode = EX)
[129] Fix | Delete
return unlock if mode == UN
[130] Fix | Delete
@sync_mutex.synchronize do
[131] Fix | Delete
sync_try_lock_sub(mode)
[132] Fix | Delete
end
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
def sync_lock(m = EX)
[136] Fix | Delete
return unlock if m == UN
[137] Fix | Delete
Thread.handle_interrupt(StandardError => :on_blocking) do
[138] Fix | Delete
while true
[139] Fix | Delete
@sync_mutex.synchronize do
[140] Fix | Delete
begin
[141] Fix | Delete
if sync_try_lock_sub(m)
[142] Fix | Delete
return self
[143] Fix | Delete
else
[144] Fix | Delete
if sync_sh_locker[Thread.current]
[145] Fix | Delete
sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]]
[146] Fix | Delete
sync_sh_locker.delete(Thread.current)
[147] Fix | Delete
else
[148] Fix | Delete
unless sync_waiting.include?(Thread.current) || sync_upgrade_waiting.reverse_each.any?{|w| w.first == Thread.current }
[149] Fix | Delete
sync_waiting.push Thread.current
[150] Fix | Delete
end
[151] Fix | Delete
end
[152] Fix | Delete
@sync_mutex.sleep
[153] Fix | Delete
end
[154] Fix | Delete
ensure
[155] Fix | Delete
sync_waiting.delete(Thread.current)
[156] Fix | Delete
end
[157] Fix | Delete
end
[158] Fix | Delete
end
[159] Fix | Delete
end
[160] Fix | Delete
self
[161] Fix | Delete
end
[162] Fix | Delete
[163] Fix | Delete
def sync_unlock(m = EX)
[164] Fix | Delete
wakeup_threads = []
[165] Fix | Delete
@sync_mutex.synchronize do
[166] Fix | Delete
if sync_mode == UN
[167] Fix | Delete
Err::UnknownLocker.Fail(Thread.current)
[168] Fix | Delete
end
[169] Fix | Delete
[170] Fix | Delete
m = sync_mode if m == EX and sync_mode == SH
[171] Fix | Delete
[172] Fix | Delete
runnable = false
[173] Fix | Delete
case m
[174] Fix | Delete
when UN
[175] Fix | Delete
Err::UnknownLocker.Fail(Thread.current)
[176] Fix | Delete
[177] Fix | Delete
when EX
[178] Fix | Delete
if sync_ex_locker == Thread.current
[179] Fix | Delete
if (self.sync_ex_count = sync_ex_count - 1) == 0
[180] Fix | Delete
self.sync_ex_locker = nil
[181] Fix | Delete
if sync_sh_locker.include?(Thread.current)
[182] Fix | Delete
self.sync_mode = SH
[183] Fix | Delete
else
[184] Fix | Delete
self.sync_mode = UN
[185] Fix | Delete
end
[186] Fix | Delete
runnable = true
[187] Fix | Delete
end
[188] Fix | Delete
else
[189] Fix | Delete
Err::UnknownLocker.Fail(Thread.current)
[190] Fix | Delete
end
[191] Fix | Delete
[192] Fix | Delete
when SH
[193] Fix | Delete
if (count = sync_sh_locker[Thread.current]).nil?
[194] Fix | Delete
Err::UnknownLocker.Fail(Thread.current)
[195] Fix | Delete
else
[196] Fix | Delete
if (sync_sh_locker[Thread.current] = count - 1) == 0
[197] Fix | Delete
sync_sh_locker.delete(Thread.current)
[198] Fix | Delete
if sync_sh_locker.empty? and sync_ex_count == 0
[199] Fix | Delete
self.sync_mode = UN
[200] Fix | Delete
runnable = true
[201] Fix | Delete
end
[202] Fix | Delete
end
[203] Fix | Delete
end
[204] Fix | Delete
end
[205] Fix | Delete
[206] Fix | Delete
if runnable
[207] Fix | Delete
if sync_upgrade_waiting.size > 0
[208] Fix | Delete
th, count = sync_upgrade_waiting.shift
[209] Fix | Delete
sync_sh_locker[th] = count
[210] Fix | Delete
th.wakeup
[211] Fix | Delete
wakeup_threads.push th
[212] Fix | Delete
else
[213] Fix | Delete
wait = sync_waiting
[214] Fix | Delete
self.sync_waiting = []
[215] Fix | Delete
for th in wait
[216] Fix | Delete
th.wakeup
[217] Fix | Delete
wakeup_threads.push th
[218] Fix | Delete
end
[219] Fix | Delete
end
[220] Fix | Delete
end
[221] Fix | Delete
end
[222] Fix | Delete
for th in wakeup_threads
[223] Fix | Delete
th.run
[224] Fix | Delete
end
[225] Fix | Delete
self
[226] Fix | Delete
end
[227] Fix | Delete
[228] Fix | Delete
def sync_synchronize(mode = EX)
[229] Fix | Delete
Thread.handle_interrupt(StandardError => :on_blocking) do
[230] Fix | Delete
sync_lock(mode)
[231] Fix | Delete
begin
[232] Fix | Delete
yield
[233] Fix | Delete
ensure
[234] Fix | Delete
sync_unlock
[235] Fix | Delete
end
[236] Fix | Delete
end
[237] Fix | Delete
end
[238] Fix | Delete
[239] Fix | Delete
attr_accessor :sync_mode
[240] Fix | Delete
[241] Fix | Delete
attr_accessor :sync_waiting
[242] Fix | Delete
attr_accessor :sync_upgrade_waiting
[243] Fix | Delete
attr_accessor :sync_sh_locker
[244] Fix | Delete
attr_accessor :sync_ex_locker
[245] Fix | Delete
attr_accessor :sync_ex_count
[246] Fix | Delete
[247] Fix | Delete
def sync_inspect
[248] Fix | Delete
sync_iv = instance_variables.select{|iv| /^@sync_/ =~ iv.id2name}.collect{|iv| iv.id2name + '=' + instance_eval(iv.id2name).inspect}.join(",")
[249] Fix | Delete
print "<#{self.class}.extend Sync_m: #{inspect}, <Sync_m: #{sync_iv}>"
[250] Fix | Delete
end
[251] Fix | Delete
[252] Fix | Delete
private
[253] Fix | Delete
[254] Fix | Delete
def sync_initialize
[255] Fix | Delete
@sync_mode = UN
[256] Fix | Delete
@sync_waiting = []
[257] Fix | Delete
@sync_upgrade_waiting = []
[258] Fix | Delete
@sync_sh_locker = Hash.new
[259] Fix | Delete
@sync_ex_locker = nil
[260] Fix | Delete
@sync_ex_count = 0
[261] Fix | Delete
[262] Fix | Delete
@sync_mutex = Thread::Mutex.new
[263] Fix | Delete
end
[264] Fix | Delete
[265] Fix | Delete
def initialize(*args)
[266] Fix | Delete
super
[267] Fix | Delete
sync_initialize
[268] Fix | Delete
end
[269] Fix | Delete
[270] Fix | Delete
def sync_try_lock_sub(m)
[271] Fix | Delete
case m
[272] Fix | Delete
when SH
[273] Fix | Delete
case sync_mode
[274] Fix | Delete
when UN
[275] Fix | Delete
self.sync_mode = m
[276] Fix | Delete
sync_sh_locker[Thread.current] = 1
[277] Fix | Delete
ret = true
[278] Fix | Delete
when SH
[279] Fix | Delete
count = 0 unless count = sync_sh_locker[Thread.current]
[280] Fix | Delete
sync_sh_locker[Thread.current] = count + 1
[281] Fix | Delete
ret = true
[282] Fix | Delete
when EX
[283] Fix | Delete
# in EX mode, lock will upgrade to EX lock
[284] Fix | Delete
if sync_ex_locker == Thread.current
[285] Fix | Delete
self.sync_ex_count = sync_ex_count + 1
[286] Fix | Delete
ret = true
[287] Fix | Delete
else
[288] Fix | Delete
ret = false
[289] Fix | Delete
end
[290] Fix | Delete
end
[291] Fix | Delete
when EX
[292] Fix | Delete
if sync_mode == UN or
[293] Fix | Delete
sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
[294] Fix | Delete
self.sync_mode = m
[295] Fix | Delete
self.sync_ex_locker = Thread.current
[296] Fix | Delete
self.sync_ex_count = 1
[297] Fix | Delete
ret = true
[298] Fix | Delete
elsif sync_mode == EX && sync_ex_locker == Thread.current
[299] Fix | Delete
self.sync_ex_count = sync_ex_count + 1
[300] Fix | Delete
ret = true
[301] Fix | Delete
else
[302] Fix | Delete
ret = false
[303] Fix | Delete
end
[304] Fix | Delete
else
[305] Fix | Delete
Err::LockModeFailer.Fail m
[306] Fix | Delete
end
[307] Fix | Delete
return ret
[308] Fix | Delete
end
[309] Fix | Delete
end
[310] Fix | Delete
[311] Fix | Delete
##
[312] Fix | Delete
# An alias for Sync_m from sync.rb
[313] Fix | Delete
[314] Fix | Delete
Synchronizer_m = Sync_m
[315] Fix | Delete
[316] Fix | Delete
##
[317] Fix | Delete
# A class that provides two-phase lock with a counter. See Sync_m for
[318] Fix | Delete
# details.
[319] Fix | Delete
[320] Fix | Delete
class Sync
[321] Fix | Delete
include Sync_m
[322] Fix | Delete
end
[323] Fix | Delete
[324] Fix | Delete
##
[325] Fix | Delete
# An alias for Sync from sync.rb. See Sync_m for details.
[326] Fix | Delete
[327] Fix | Delete
Synchronizer = Sync
[328] Fix | Delete
[329] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function