1998-01-16 15:13:05 +03:00
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
# sync.rb - 2 phase lock with counter
|
2001-06-06 18:19:33 +04:00
|
|
|
# $Release Version: 1.0$
|
1998-01-16 15:13:05 +03:00
|
|
|
# $Revision$
|
2001-06-06 18:19:33 +04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
# --
|
1998-01-16 15:19:22 +03:00
|
|
|
# Sync_m, Synchronizer_m
|
1998-01-16 15:13:05 +03:00
|
|
|
# Usage:
|
1998-01-16 15:19:22 +03:00
|
|
|
# obj.extend(Sync_m)
|
|
|
|
# or
|
|
|
|
# class Foo
|
2001-06-06 18:19:33 +04:00
|
|
|
# include Sync_m
|
1998-01-16 15:19:22 +03:00
|
|
|
# :
|
|
|
|
# end
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
# Sync_m#sync_mode
|
|
|
|
# Sync_m#sync_locked?, locked?
|
|
|
|
# Sync_m#sync_shared?, shared?
|
|
|
|
# Sync_m#sync_exclusive?, sync_exclusive?
|
|
|
|
# Sync_m#sync_try_lock, try_lock
|
|
|
|
# Sync_m#sync_lock, lock
|
|
|
|
# Sync_m#sync_unlock, unlock
|
|
|
|
#
|
2007-03-13 13:13:41 +03:00
|
|
|
# Sync, Synchronizer:
|
|
|
|
# Usage:
|
1998-01-16 15:13:05 +03:00
|
|
|
# sync = Sync.new
|
1998-01-16 15:19:22 +03:00
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
# Sync#mode
|
|
|
|
# Sync#locked?
|
|
|
|
# Sync#shared?
|
|
|
|
# Sync#exclusive?
|
|
|
|
# Sync#try_lock(mode) -- mode = :EX, :SH, :UN
|
|
|
|
# Sync#lock(mode) -- mode = :EX, :SH, :UN
|
|
|
|
# Sync#unlock
|
|
|
|
# Sync#synchronize(mode) {...}
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
unless defined? Thread
|
2005-12-29 15:05:16 +03:00
|
|
|
raise "Thread not available for this ruby interpreter"
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
module Sync_m
|
|
|
|
RCS_ID='-$Header$-'
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
# lock mode
|
1998-01-16 15:13:05 +03:00
|
|
|
UN = :UN
|
|
|
|
SH = :SH
|
|
|
|
EX = :EX
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
# exceptions
|
1999-01-20 07:59:39 +03:00
|
|
|
class Err < StandardError
|
1998-01-16 15:13:05 +03:00
|
|
|
def Err.Fail(*opt)
|
|
|
|
fail self, sprintf(self::Message, *opt)
|
|
|
|
end
|
|
|
|
|
|
|
|
class UnknownLocker < Err
|
|
|
|
Message = "Thread(%s) not locked."
|
|
|
|
def UnknownLocker.Fail(th)
|
|
|
|
super(th.inspect)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class LockModeFailer < Err
|
|
|
|
Message = "Unknown lock mode(%s)"
|
|
|
|
def LockModeFailer.Fail(mode)
|
|
|
|
if mode.id2name
|
|
|
|
mode = id2name
|
|
|
|
end
|
|
|
|
super(mode)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2001-06-06 18:19:33 +04:00
|
|
|
def Sync_m.define_aliases(cl)
|
|
|
|
cl.module_eval %q{
|
|
|
|
alias locked? sync_locked?
|
|
|
|
alias shared? sync_shared?
|
|
|
|
alias exclusive? sync_exclusive?
|
|
|
|
alias lock sync_lock
|
|
|
|
alias unlock sync_unlock
|
|
|
|
alias try_lock sync_try_lock
|
|
|
|
alias synchronize sync_synchronize
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def Sync_m.append_features(cl)
|
|
|
|
super
|
2007-03-13 13:13:41 +03:00
|
|
|
# do nothing for Modules
|
|
|
|
# make aliases for Classes.
|
|
|
|
define_aliases(cl) unless cl.instance_of?(Module)
|
|
|
|
self
|
1998-01-16 15:19:22 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def Sync_m.extend_object(obj)
|
1999-08-13 09:45:20 +04:00
|
|
|
super
|
2007-03-13 13:13:41 +03:00
|
|
|
obj.sync_extend
|
1998-01-16 15:19:22 +03:00
|
|
|
end
|
2007-03-13 13:13:41 +03:00
|
|
|
|
|
|
|
def sync_extend
|
1998-01-16 15:13:05 +03:00
|
|
|
unless (defined? locked? and
|
|
|
|
defined? shared? and
|
|
|
|
defined? exclusive? and
|
|
|
|
defined? lock and
|
|
|
|
defined? unlock and
|
|
|
|
defined? try_lock and
|
|
|
|
defined? synchronize)
|
2001-06-06 18:19:33 +04:00
|
|
|
Sync_m.define_aliases(class<<self;self;end)
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2001-06-06 18:19:33 +04:00
|
|
|
sync_initialize
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2001-06-06 18:19:33 +04:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
# accessing
|
1998-01-16 15:13:05 +03:00
|
|
|
def sync_locked?
|
|
|
|
sync_mode != UN
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync_shared?
|
|
|
|
sync_mode == SH
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync_exclusive?
|
|
|
|
sync_mode == EX
|
|
|
|
end
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
# locking methods.
|
1998-01-16 15:13:05 +03:00
|
|
|
def sync_try_lock(mode = EX)
|
2008-09-10 05:22:35 +04:00
|
|
|
return unlock if mode == UN
|
2007-03-13 13:13:41 +03:00
|
|
|
@sync_mutex.synchronize do
|
2008-09-10 05:22:35 +04:00
|
|
|
ret = sync_try_lock_sub(mode)
|
2007-03-13 13:13:41 +03:00
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync_lock(m = EX)
|
|
|
|
return unlock if m == UN
|
|
|
|
|
2007-03-13 13:13:41 +03:00
|
|
|
while true
|
|
|
|
@sync_mutex.synchronize do
|
|
|
|
if sync_try_lock_sub(m)
|
|
|
|
return self
|
|
|
|
else
|
|
|
|
if sync_sh_locker[Thread.current]
|
|
|
|
sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]]
|
|
|
|
sync_sh_locker.delete(Thread.current)
|
|
|
|
else
|
|
|
|
sync_waiting.push Thread.current
|
|
|
|
end
|
|
|
|
@sync_mutex.sleep
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync_unlock(m = EX)
|
2007-03-13 13:13:41 +03:00
|
|
|
wakeup_threads = []
|
|
|
|
@sync_mutex.synchronize do
|
|
|
|
if sync_mode == UN
|
1998-01-16 15:13:05 +03:00
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
|
|
|
end
|
|
|
|
|
2007-03-13 13:13:41 +03:00
|
|
|
m = sync_mode if m == EX and sync_mode == SH
|
|
|
|
|
|
|
|
runnable = false
|
|
|
|
case m
|
|
|
|
when UN
|
1998-01-16 15:13:05 +03:00
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
2007-03-13 13:13:41 +03:00
|
|
|
|
|
|
|
when EX
|
|
|
|
if sync_ex_locker == Thread.current
|
|
|
|
if (self.sync_ex_count = sync_ex_count - 1) == 0
|
|
|
|
self.sync_ex_locker = nil
|
|
|
|
if sync_sh_locker.include?(Thread.current)
|
|
|
|
self.sync_mode = SH
|
|
|
|
else
|
|
|
|
self.sync_mode = UN
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
runnable = true
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2007-03-13 13:13:41 +03:00
|
|
|
else
|
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
2007-03-13 13:13:41 +03:00
|
|
|
when SH
|
|
|
|
if (count = sync_sh_locker[Thread.current]).nil?
|
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
|
|
|
else
|
|
|
|
if (sync_sh_locker[Thread.current] = count - 1) == 0
|
|
|
|
sync_sh_locker.delete(Thread.current)
|
|
|
|
if sync_sh_locker.empty? and sync_ex_count == 0
|
|
|
|
self.sync_mode = UN
|
|
|
|
runnable = true
|
|
|
|
end
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2007-03-13 13:13:41 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
if runnable
|
|
|
|
if sync_upgrade_waiting.size > 0
|
|
|
|
th, count = sync_upgrade_waiting.shift
|
|
|
|
sync_sh_locker[th] = count
|
|
|
|
th.wakeup
|
|
|
|
wakeup_threads.push th
|
|
|
|
else
|
|
|
|
wait = sync_waiting
|
|
|
|
self.sync_waiting = []
|
|
|
|
for th in wait
|
|
|
|
th.wakeup
|
|
|
|
wakeup_threads.push th
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-03-13 13:13:41 +03:00
|
|
|
for th in wakeup_threads
|
|
|
|
th.run
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
def sync_synchronize(mode = EX)
|
2007-03-13 13:13:41 +03:00
|
|
|
sync_lock(mode)
|
1999-08-13 09:45:20 +04:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
sync_unlock
|
|
|
|
end
|
|
|
|
end
|
2001-06-06 18:19:33 +04:00
|
|
|
|
2006-07-20 21:36:36 +04:00
|
|
|
attr_accessor :sync_mode
|
2001-06-06 18:19:33 +04:00
|
|
|
|
2006-07-20 21:36:36 +04:00
|
|
|
attr_accessor :sync_waiting
|
|
|
|
attr_accessor :sync_upgrade_waiting
|
|
|
|
attr_accessor :sync_sh_locker
|
|
|
|
attr_accessor :sync_ex_locker
|
|
|
|
attr_accessor :sync_ex_count
|
2007-03-13 13:13:41 +03:00
|
|
|
|
|
|
|
def sync_inspect
|
|
|
|
sync_iv = instance_variables.select{|iv| /^@sync_/ =~ iv.id2name}.collect{|iv| iv.id2name + '=' + instance_eval(iv.id2name).inspect}.join(",")
|
|
|
|
print "<#{self.class}.extend Sync_m: #{inspect}, <Sync_m: #{sync_iv}>"
|
|
|
|
end
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
private
|
|
|
|
|
2001-06-06 18:19:33 +04:00
|
|
|
def sync_initialize
|
1999-08-13 09:45:20 +04:00
|
|
|
@sync_mode = UN
|
|
|
|
@sync_waiting = []
|
|
|
|
@sync_upgrade_waiting = []
|
|
|
|
@sync_sh_locker = Hash.new
|
|
|
|
@sync_ex_locker = nil
|
|
|
|
@sync_ex_count = 0
|
2007-03-13 13:13:41 +03:00
|
|
|
|
|
|
|
@sync_mutex = Mutex.new
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2001-06-06 18:19:33 +04:00
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
super
|
2007-03-13 13:13:41 +03:00
|
|
|
sync_initialize
|
2001-06-06 18:19:33 +04:00
|
|
|
end
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
def sync_try_lock_sub(m)
|
|
|
|
case m
|
|
|
|
when SH
|
|
|
|
case sync_mode
|
|
|
|
when UN
|
|
|
|
self.sync_mode = m
|
|
|
|
sync_sh_locker[Thread.current] = 1
|
1999-08-13 09:45:20 +04:00
|
|
|
ret = true
|
1998-01-16 15:13:05 +03:00
|
|
|
when SH
|
|
|
|
count = 0 unless count = sync_sh_locker[Thread.current]
|
|
|
|
sync_sh_locker[Thread.current] = count + 1
|
1999-08-13 09:45:20 +04:00
|
|
|
ret = true
|
1998-01-16 15:13:05 +03:00
|
|
|
when EX
|
1999-08-13 09:45:20 +04:00
|
|
|
# in EX mode, lock will upgrade to EX lock
|
1998-01-16 15:13:05 +03:00
|
|
|
if sync_ex_locker == Thread.current
|
|
|
|
self.sync_ex_count = sync_ex_count + 1
|
1999-08-13 09:45:20 +04:00
|
|
|
ret = true
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
1999-08-13 09:45:20 +04:00
|
|
|
ret = false
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
when EX
|
|
|
|
if sync_mode == UN or
|
2007-03-13 13:13:41 +03:00
|
|
|
sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
|
1998-01-16 15:13:05 +03:00
|
|
|
self.sync_mode = m
|
|
|
|
self.sync_ex_locker = Thread.current
|
|
|
|
self.sync_ex_count = 1
|
1999-08-13 09:45:20 +04:00
|
|
|
ret = true
|
1998-01-16 15:13:05 +03:00
|
|
|
elsif sync_mode == EX && sync_ex_locker == Thread.current
|
|
|
|
self.sync_ex_count = sync_ex_count + 1
|
1999-08-13 09:45:20 +04:00
|
|
|
ret = true
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
1999-08-13 09:45:20 +04:00
|
|
|
ret = false
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
Err::LockModeFailer.Fail mode
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Synchronizer_m = Sync_m
|
|
|
|
|
|
|
|
class Sync
|
1999-08-13 09:45:20 +04:00
|
|
|
include Sync_m
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
Synchronizer = Sync
|