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