Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: sync.rb
#
[0] Fix | Delete
# sync.rb - 2 phase lock with counter
[1] Fix | Delete
# $Release Version: 1.0$
[2] Fix | Delete
# $Revision: 22457 $
[3] Fix | Delete
# $Date: 2009-02-20 01:41:12 +0900 (Fri, 20 Feb 2009) $
[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, Synchronicer:
[25] Fix | Delete
# include Sync_m
[26] Fix | Delete
# Usage:
[27] Fix | Delete
# sync = Sync.new
[28] Fix | Delete
#
[29] Fix | Delete
# Sync#mode
[30] Fix | Delete
# Sync#locked?
[31] Fix | Delete
# Sync#shared?
[32] Fix | Delete
# Sync#exclusive?
[33] Fix | Delete
# Sync#try_lock(mode) -- mode = :EX, :SH, :UN
[34] Fix | Delete
# Sync#lock(mode) -- mode = :EX, :SH, :UN
[35] Fix | Delete
# Sync#unlock
[36] Fix | Delete
# Sync#synchronize(mode) {...}
[37] Fix | Delete
#
[38] Fix | Delete
#
[39] Fix | Delete
[40] Fix | Delete
unless defined? Thread
[41] Fix | Delete
fail "Thread not available for this ruby interpreter"
[42] Fix | Delete
end
[43] Fix | Delete
[44] Fix | Delete
module Sync_m
[45] Fix | Delete
RCS_ID='-$Header$-'
[46] Fix | Delete
[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
Thread.critical = false
[56] Fix | Delete
fail self, sprintf(self::Message, *opt)
[57] Fix | Delete
end
[58] Fix | Delete
[59] Fix | Delete
class UnknownLocker < Err
[60] Fix | Delete
Message = "Thread(%s) not locked."
[61] Fix | Delete
def UnknownLocker.Fail(th)
[62] Fix | Delete
super(th.inspect)
[63] Fix | Delete
end
[64] Fix | Delete
end
[65] Fix | Delete
[66] Fix | Delete
class LockModeFailer < Err
[67] Fix | Delete
Message = "Unknown lock mode(%s)"
[68] Fix | Delete
def LockModeFailer.Fail(mode)
[69] Fix | Delete
if mode.id2name
[70] Fix | Delete
mode = id2name
[71] Fix | Delete
end
[72] Fix | Delete
super(mode)
[73] Fix | Delete
end
[74] Fix | Delete
end
[75] Fix | Delete
end
[76] Fix | Delete
[77] Fix | Delete
def Sync_m.define_aliases(cl)
[78] Fix | Delete
cl.module_eval %q{
[79] Fix | Delete
alias locked? sync_locked?
[80] Fix | Delete
alias shared? sync_shared?
[81] Fix | Delete
alias exclusive? sync_exclusive?
[82] Fix | Delete
alias lock sync_lock
[83] Fix | Delete
alias unlock sync_unlock
[84] Fix | Delete
alias try_lock sync_try_lock
[85] Fix | Delete
alias synchronize sync_synchronize
[86] Fix | Delete
}
[87] Fix | Delete
end
[88] Fix | Delete
[89] Fix | Delete
def Sync_m.append_features(cl)
[90] Fix | Delete
super
[91] Fix | Delete
unless cl.instance_of?(Module)
[92] Fix | Delete
# do nothing for Modules
[93] Fix | Delete
# make aliases and include the proper module.
[94] Fix | Delete
define_aliases(cl)
[95] Fix | Delete
end
[96] Fix | Delete
end
[97] Fix | Delete
[98] Fix | Delete
def Sync_m.extend_object(obj)
[99] Fix | Delete
super
[100] Fix | Delete
obj.sync_extended
[101] Fix | Delete
end
[102] Fix | Delete
[103] Fix | Delete
def sync_extended
[104] Fix | Delete
unless (defined? locked? and
[105] Fix | Delete
defined? shared? and
[106] Fix | Delete
defined? exclusive? and
[107] Fix | Delete
defined? lock and
[108] Fix | Delete
defined? unlock and
[109] Fix | Delete
defined? try_lock and
[110] Fix | Delete
defined? synchronize)
[111] Fix | Delete
Sync_m.define_aliases(class<<self;self;end)
[112] Fix | Delete
end
[113] Fix | Delete
sync_initialize
[114] Fix | Delete
end
[115] Fix | Delete
[116] Fix | Delete
# accessing
[117] Fix | Delete
def sync_locked?
[118] Fix | Delete
sync_mode != UN
[119] Fix | Delete
end
[120] Fix | Delete
[121] Fix | Delete
def sync_shared?
[122] Fix | Delete
sync_mode == SH
[123] Fix | Delete
end
[124] Fix | Delete
[125] Fix | Delete
def sync_exclusive?
[126] Fix | Delete
sync_mode == EX
[127] Fix | Delete
end
[128] Fix | Delete
[129] Fix | Delete
# locking methods.
[130] Fix | Delete
def sync_try_lock(mode = EX)
[131] Fix | Delete
return unlock if mode == UN
[132] Fix | Delete
[133] Fix | Delete
Thread.critical = true
[134] Fix | Delete
ret = sync_try_lock_sub(mode)
[135] Fix | Delete
Thread.critical = false
[136] Fix | Delete
ret
[137] Fix | Delete
end
[138] Fix | Delete
[139] Fix | Delete
def sync_lock(m = EX)
[140] Fix | Delete
return unlock if m == UN
[141] Fix | Delete
[142] Fix | Delete
until (Thread.critical = true; sync_try_lock_sub(m))
[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
sync_waiting.push Thread.current
[148] Fix | Delete
end
[149] Fix | Delete
Thread.stop
[150] Fix | Delete
end
[151] Fix | Delete
Thread.critical = false
[152] Fix | Delete
self
[153] Fix | Delete
end
[154] Fix | Delete
[155] Fix | Delete
def sync_unlock(m = EX)
[156] Fix | Delete
Thread.critical = true
[157] Fix | Delete
if sync_mode == UN
[158] Fix | Delete
Thread.critical = false
[159] Fix | Delete
Err::UnknownLocker.Fail(Thread.current)
[160] Fix | Delete
end
[161] Fix | Delete
[162] Fix | Delete
m = sync_mode if m == EX and sync_mode == SH
[163] Fix | Delete
[164] Fix | Delete
runnable = false
[165] Fix | Delete
case m
[166] Fix | Delete
when UN
[167] Fix | Delete
Thread.critical = false
[168] Fix | Delete
Err::UnknownLocker.Fail(Thread.current)
[169] Fix | Delete
[170] Fix | Delete
when EX
[171] Fix | Delete
if sync_ex_locker == Thread.current
[172] Fix | Delete
if (self.sync_ex_count = sync_ex_count - 1) == 0
[173] Fix | Delete
self.sync_ex_locker = nil
[174] Fix | Delete
if sync_sh_locker.include?(Thread.current)
[175] Fix | Delete
self.sync_mode = SH
[176] Fix | Delete
else
[177] Fix | Delete
self.sync_mode = UN
[178] Fix | Delete
end
[179] Fix | Delete
runnable = true
[180] Fix | Delete
end
[181] Fix | Delete
else
[182] Fix | Delete
Err::UnknownLocker.Fail(Thread.current)
[183] Fix | Delete
end
[184] Fix | Delete
[185] Fix | Delete
when SH
[186] Fix | Delete
if (count = sync_sh_locker[Thread.current]).nil?
[187] Fix | Delete
Err::UnknownLocker.Fail(Thread.current)
[188] Fix | Delete
else
[189] Fix | Delete
if (sync_sh_locker[Thread.current] = count - 1) == 0
[190] Fix | Delete
sync_sh_locker.delete(Thread.current)
[191] Fix | Delete
if sync_sh_locker.empty? and sync_ex_count == 0
[192] Fix | Delete
self.sync_mode = UN
[193] Fix | Delete
runnable = true
[194] Fix | Delete
end
[195] Fix | Delete
end
[196] Fix | Delete
end
[197] Fix | Delete
end
[198] Fix | Delete
[199] Fix | Delete
if runnable
[200] Fix | Delete
if sync_upgrade_waiting.size > 0
[201] Fix | Delete
for k, v in sync_upgrade_waiting
[202] Fix | Delete
sync_sh_locker[k] = v
[203] Fix | Delete
end
[204] Fix | Delete
wait = sync_upgrade_waiting
[205] Fix | Delete
self.sync_upgrade_waiting = []
[206] Fix | Delete
Thread.critical = false
[207] Fix | Delete
[208] Fix | Delete
for w, v in wait
[209] Fix | Delete
w.run
[210] Fix | Delete
end
[211] Fix | Delete
else
[212] Fix | Delete
wait = sync_waiting
[213] Fix | Delete
self.sync_waiting = []
[214] Fix | Delete
Thread.critical = false
[215] Fix | Delete
for w in wait
[216] Fix | Delete
w.run
[217] Fix | Delete
end
[218] Fix | Delete
end
[219] Fix | Delete
end
[220] Fix | Delete
[221] Fix | Delete
Thread.critical = false
[222] Fix | Delete
self
[223] Fix | Delete
end
[224] Fix | Delete
[225] Fix | Delete
def sync_synchronize(mode = EX)
[226] Fix | Delete
begin
[227] Fix | Delete
sync_lock(mode)
[228] Fix | Delete
yield
[229] Fix | Delete
ensure
[230] Fix | Delete
sync_unlock
[231] Fix | Delete
end
[232] Fix | Delete
end
[233] Fix | Delete
[234] Fix | Delete
attr :sync_mode, true
[235] Fix | Delete
[236] Fix | Delete
attr :sync_waiting, true
[237] Fix | Delete
attr :sync_upgrade_waiting, true
[238] Fix | Delete
attr :sync_sh_locker, true
[239] Fix | Delete
attr :sync_ex_locker, true
[240] Fix | Delete
attr :sync_ex_count, true
[241] Fix | Delete
[242] Fix | Delete
private
[243] Fix | Delete
[244] Fix | Delete
def sync_initialize
[245] Fix | Delete
@sync_mode = UN
[246] Fix | Delete
@sync_waiting = []
[247] Fix | Delete
@sync_upgrade_waiting = []
[248] Fix | Delete
@sync_sh_locker = Hash.new
[249] Fix | Delete
@sync_ex_locker = nil
[250] Fix | Delete
@sync_ex_count = 0
[251] Fix | Delete
end
[252] Fix | Delete
[253] Fix | Delete
def initialize(*args)
[254] Fix | Delete
sync_initialize
[255] Fix | Delete
super
[256] Fix | Delete
end
[257] Fix | Delete
[258] Fix | Delete
def sync_try_lock_sub(m)
[259] Fix | Delete
case m
[260] Fix | Delete
when SH
[261] Fix | Delete
case sync_mode
[262] Fix | Delete
when UN
[263] Fix | Delete
self.sync_mode = m
[264] Fix | Delete
sync_sh_locker[Thread.current] = 1
[265] Fix | Delete
ret = true
[266] Fix | Delete
when SH
[267] Fix | Delete
count = 0 unless count = sync_sh_locker[Thread.current]
[268] Fix | Delete
sync_sh_locker[Thread.current] = count + 1
[269] Fix | Delete
ret = true
[270] Fix | Delete
when EX
[271] Fix | Delete
# in EX mode, lock will upgrade to EX lock
[272] Fix | Delete
if sync_ex_locker == Thread.current
[273] Fix | Delete
self.sync_ex_count = sync_ex_count + 1
[274] Fix | Delete
ret = true
[275] Fix | Delete
else
[276] Fix | Delete
ret = false
[277] Fix | Delete
end
[278] Fix | Delete
end
[279] Fix | Delete
when EX
[280] Fix | Delete
if sync_mode == UN or
[281] Fix | Delete
sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
[282] Fix | Delete
self.sync_mode = m
[283] Fix | Delete
self.sync_ex_locker = Thread.current
[284] Fix | Delete
self.sync_ex_count = 1
[285] Fix | Delete
ret = true
[286] Fix | Delete
elsif sync_mode == EX && sync_ex_locker == Thread.current
[287] Fix | Delete
self.sync_ex_count = sync_ex_count + 1
[288] Fix | Delete
ret = true
[289] Fix | Delete
else
[290] Fix | Delete
ret = false
[291] Fix | Delete
end
[292] Fix | Delete
else
[293] Fix | Delete
Thread.critical = false
[294] Fix | Delete
Err::LockModeFailer.Fail mode
[295] Fix | Delete
end
[296] Fix | Delete
return ret
[297] Fix | Delete
end
[298] Fix | Delete
end
[299] Fix | Delete
Synchronizer_m = Sync_m
[300] Fix | Delete
[301] Fix | Delete
class Sync
[302] Fix | Delete
#Sync_m.extend_class self
[303] Fix | Delete
include Sync_m
[304] Fix | Delete
[305] Fix | Delete
def initialize
[306] Fix | Delete
super
[307] Fix | Delete
end
[308] Fix | Delete
[309] Fix | Delete
end
[310] Fix | Delete
Synchronizer = Sync
[311] Fix | Delete
[312] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function