1998-01-16 07:13:05 -05:00
|
|
|
#
|
1999-08-13 01:45:20 -04:00
|
|
|
# sync.rb - 2 phase lock with counter
|
2001-06-06 10:19:33 -04:00
|
|
|
# $Release Version: 1.0$
|
1998-01-16 07:13:05 -05:00
|
|
|
# $Revision$
|
|
|
|
# $Date$
|
2001-06-06 10:19:33 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
# --
|
1998-01-16 07:19:22 -05:00
|
|
|
# Sync_m, Synchronizer_m
|
1998-01-16 07:13:05 -05:00
|
|
|
# Usage:
|
1998-01-16 07:19:22 -05:00
|
|
|
# obj.extend(Sync_m)
|
|
|
|
# or
|
|
|
|
# class Foo
|
2001-06-06 10:19:33 -04:00
|
|
|
# include Sync_m
|
1998-01-16 07:19:22 -05:00
|
|
|
# :
|
|
|
|
# end
|
1998-01-16 07:13:05 -05: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
|
|
|
|
#
|
|
|
|
# Sync, Synchronicer:
|
|
|
|
# include Sync_m
|
1998-01-16 07:19:22 -05:00
|
|
|
# Usage:
|
1998-01-16 07:13:05 -05:00
|
|
|
# sync = Sync.new
|
1998-01-16 07:19:22 -05:00
|
|
|
#
|
1998-01-16 07:13:05 -05: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 07:05:16 -05:00
|
|
|
raise "Thread not available for this ruby interpreter"
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module Sync_m
|
|
|
|
RCS_ID='-$Header$-'
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
# lock mode
|
1998-01-16 07:13:05 -05:00
|
|
|
UN = :UN
|
|
|
|
SH = :SH
|
|
|
|
EX = :EX
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
# exceptions
|
1999-01-19 23:59:39 -05:00
|
|
|
class Err < StandardError
|
1998-01-16 07:13:05 -05: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 10: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
|
1999-08-13 01:45:20 -04:00
|
|
|
unless cl.instance_of?(Module)
|
|
|
|
# do nothing for Modules
|
|
|
|
# make aliases and include the proper module.
|
2001-06-06 10:19:33 -04:00
|
|
|
define_aliases(cl)
|
1998-01-16 07:19:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def Sync_m.extend_object(obj)
|
1999-08-13 01:45:20 -04:00
|
|
|
super
|
|
|
|
obj.sync_extended
|
1998-01-16 07:19:22 -05:00
|
|
|
end
|
2001-06-06 10:19:33 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def sync_extended
|
|
|
|
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 10:19:33 -04:00
|
|
|
Sync_m.define_aliases(class<<self;self;end)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2001-06-06 10:19:33 -04:00
|
|
|
sync_initialize
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2001-06-06 10:19:33 -04:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
# accessing
|
1998-01-16 07:13:05 -05: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 07:19:22 -05:00
|
|
|
# locking methods.
|
1998-01-16 07:13:05 -05:00
|
|
|
def sync_try_lock(mode = EX)
|
|
|
|
return unlock if sync_mode == UN
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = true
|
1998-01-16 07:13:05 -05:00
|
|
|
ret = sync_try_lock_sub(sync_mode)
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = false
|
1998-01-16 07:13:05 -05:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync_lock(m = EX)
|
|
|
|
return unlock if m == UN
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
until (Thread.critical = true; sync_try_lock_sub(m))
|
1998-01-16 07:13:05 -05:00
|
|
|
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
|
|
|
|
Thread.stop
|
|
|
|
end
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = false
|
1998-01-16 07:13:05 -05:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync_unlock(m = EX)
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = true
|
1998-01-16 07:13:05 -05:00
|
|
|
if sync_mode == UN
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = false
|
1998-01-16 07:13:05 -05:00
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
|
|
|
end
|
|
|
|
|
|
|
|
m = sync_mode if m == EX and sync_mode == SH
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
runnable = false
|
1998-01-16 07:13:05 -05:00
|
|
|
case m
|
|
|
|
when UN
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = false
|
1998-01-16 07:13:05 -05:00
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
|
|
|
|
|
|
|
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 01:45:20 -04:00
|
|
|
runnable = true
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
1999-08-13 01:45:20 -04:00
|
|
|
runnable = true
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if runnable
|
|
|
|
if sync_upgrade_waiting.size > 0
|
|
|
|
for k, v in sync_upgrade_waiting
|
|
|
|
sync_sh_locker[k] = v
|
|
|
|
end
|
|
|
|
wait = sync_upgrade_waiting
|
|
|
|
self.sync_upgrade_waiting = []
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = false
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
for w, v in wait
|
|
|
|
w.run
|
|
|
|
end
|
|
|
|
else
|
|
|
|
wait = sync_waiting
|
|
|
|
self.sync_waiting = []
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = false
|
1998-01-16 07:13:05 -05:00
|
|
|
for w in wait
|
|
|
|
w.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = false
|
1998-01-16 07:13:05 -05:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
def sync_synchronize(mode = EX)
|
|
|
|
begin
|
2001-06-06 10:19:33 -04:00
|
|
|
sync_lock(mode)
|
1999-08-13 01:45:20 -04:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
sync_unlock
|
|
|
|
end
|
|
|
|
end
|
2001-06-06 10:19:33 -04:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
attr :sync_mode, true
|
2001-06-06 10:19:33 -04:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
attr :sync_waiting, true
|
|
|
|
attr :sync_upgrade_waiting, true
|
|
|
|
attr :sync_sh_locker, true
|
|
|
|
attr :sync_ex_locker, true
|
|
|
|
attr :sync_ex_count, true
|
2001-06-06 10:19:33 -04:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
private
|
|
|
|
|
2001-06-06 10:19:33 -04:00
|
|
|
def sync_initialize
|
1999-08-13 01: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
|
|
|
|
end
|
2001-06-06 10:19:33 -04:00
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
sync_initialize
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
1998-01-16 07:13:05 -05: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 01:45:20 -04:00
|
|
|
ret = true
|
1998-01-16 07:13:05 -05:00
|
|
|
when SH
|
|
|
|
count = 0 unless count = sync_sh_locker[Thread.current]
|
|
|
|
sync_sh_locker[Thread.current] = count + 1
|
1999-08-13 01:45:20 -04:00
|
|
|
ret = true
|
1998-01-16 07:13:05 -05:00
|
|
|
when EX
|
1999-08-13 01:45:20 -04:00
|
|
|
# in EX mode, lock will upgrade to EX lock
|
1998-01-16 07:13:05 -05:00
|
|
|
if sync_ex_locker == Thread.current
|
|
|
|
self.sync_ex_count = sync_ex_count + 1
|
1999-08-13 01:45:20 -04:00
|
|
|
ret = true
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
1999-08-13 01:45:20 -04:00
|
|
|
ret = false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
when EX
|
|
|
|
if sync_mode == UN or
|
|
|
|
sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
|
|
|
|
self.sync_mode = m
|
|
|
|
self.sync_ex_locker = Thread.current
|
|
|
|
self.sync_ex_count = 1
|
1999-08-13 01:45:20 -04:00
|
|
|
ret = true
|
1998-01-16 07:13:05 -05:00
|
|
|
elsif sync_mode == EX && sync_ex_locker == Thread.current
|
|
|
|
self.sync_ex_count = sync_ex_count + 1
|
1999-08-13 01:45:20 -04:00
|
|
|
ret = true
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
1999-08-13 01:45:20 -04:00
|
|
|
ret = false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
else
|
1999-08-13 01:45:20 -04:00
|
|
|
Thread.critical = false
|
1998-01-16 07:13:05 -05:00
|
|
|
Err::LockModeFailer.Fail mode
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Synchronizer_m = Sync_m
|
|
|
|
|
|
|
|
class Sync
|
2001-06-06 10:19:33 -04:00
|
|
|
#Sync_m.extend_class self
|
1999-08-13 01:45:20 -04:00
|
|
|
include Sync_m
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
def initialize
|
1998-01-16 07:19:22 -05:00
|
|
|
super
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2001-06-06 10:19:33 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
Synchronizer = Sync
|