1998-01-16 07:13:05 -05:00
|
|
|
#
|
1999-08-13 01:45:20 -04:00
|
|
|
# sync.rb - 2 phase lock with counter
|
2011-05-18 20:07:25 -04:00
|
|
|
# $Release Version: 1.0$
|
|
|
|
# $Revision$
|
|
|
|
# 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
|
2011-05-18 20:07:25 -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
|
|
|
|
#
|
2007-03-13 06:13:41 -04:00
|
|
|
# Sync, Synchronizer:
|
|
|
|
# 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) {...}
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-06-28 23:09:34 -04:00
|
|
|
##
|
|
|
|
# A module that provides a two-phase lock with a counter.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
module Sync_m
|
1998-01-16 07:19:22 -05:00
|
|
|
# lock mode
|
1998-01-16 07:13:05 -05:00
|
|
|
UN = :UN
|
|
|
|
SH = :SH
|
|
|
|
EX = :EX
|
2009-03-05 22:56:38 -05:00
|
|
|
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
class UnknownLocker < Err
|
|
|
|
Message = "Thread(%s) not locked."
|
|
|
|
def UnknownLocker.Fail(th)
|
2011-05-18 17:19:18 -04:00
|
|
|
super(th.inspect)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
class LockModeFailer < Err
|
|
|
|
Message = "Unknown lock mode(%s)"
|
|
|
|
def LockModeFailer.Fail(mode)
|
2011-05-18 17:19:18 -04:00
|
|
|
if mode.id2name
|
|
|
|
mode = id2name
|
|
|
|
end
|
|
|
|
super(mode)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2001-06-06 10:19:33 -04:00
|
|
|
def Sync_m.append_features(cl)
|
|
|
|
super
|
2007-03-13 06:13:41 -04:00
|
|
|
# do nothing for Modules
|
|
|
|
# make aliases for Classes.
|
|
|
|
define_aliases(cl) unless cl.instance_of?(Module)
|
|
|
|
self
|
1998-01-16 07:19:22 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
def Sync_m.extend_object(obj)
|
1999-08-13 01:45:20 -04:00
|
|
|
super
|
2007-03-13 06:13:41 -04:00
|
|
|
obj.sync_extend
|
1998-01-16 07:19:22 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-03-13 06:13:41 -04:00
|
|
|
def sync_extend
|
1998-01-16 07:13:05 -05:00
|
|
|
unless (defined? locked? and
|
2011-05-18 20:07:25 -04:00
|
|
|
defined? shared? and
|
|
|
|
defined? exclusive? and
|
|
|
|
defined? lock and
|
|
|
|
defined? unlock and
|
|
|
|
defined? try_lock and
|
|
|
|
defined? synchronize)
|
2010-05-23 04:48:44 -04:00
|
|
|
Sync_m.define_aliases(singleton_class)
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def sync_shared?
|
|
|
|
sync_mode == SH
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def sync_exclusive?
|
|
|
|
sync_mode == EX
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
# locking methods.
|
1998-01-16 07:13:05 -05:00
|
|
|
def sync_try_lock(mode = EX)
|
2008-09-09 21:22:35 -04:00
|
|
|
return unlock if mode == UN
|
2007-03-13 06:13:41 -04:00
|
|
|
@sync_mutex.synchronize do
|
2010-11-08 15:59:01 -05:00
|
|
|
sync_try_lock_sub(mode)
|
2007-03-13 06:13:41 -04:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def sync_lock(m = EX)
|
|
|
|
return unlock if m == UN
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(StandardError => :on_blocking) do
|
2012-11-30 13:55:28 -05:00
|
|
|
while true
|
|
|
|
@sync_mutex.synchronize do
|
|
|
|
begin
|
|
|
|
if sync_try_lock_sub(m)
|
|
|
|
return self
|
2012-09-09 07:34:47 -04:00
|
|
|
else
|
2012-11-30 13:55:28 -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
|
|
|
|
unless sync_waiting.include?(Thread.current) || sync_upgrade_waiting.reverse_each.any?{|w| w.first == Thread.current }
|
|
|
|
sync_waiting.push Thread.current
|
|
|
|
end
|
2012-09-09 07:34:47 -04:00
|
|
|
end
|
2012-11-30 13:55:28 -05:00
|
|
|
@sync_mutex.sleep
|
2012-09-09 07:34:47 -04:00
|
|
|
end
|
2012-11-30 13:55:28 -05:00
|
|
|
ensure
|
|
|
|
sync_waiting.delete(Thread.current)
|
2011-05-18 17:19:18 -04:00
|
|
|
end
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def sync_unlock(m = EX)
|
2007-03-13 06:13:41 -04:00
|
|
|
wakeup_threads = []
|
|
|
|
@sync_mutex.synchronize do
|
|
|
|
if sync_mode == UN
|
2011-05-18 17:19:18 -04:00
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-03-13 06:13:41 -04:00
|
|
|
m = sync_mode if m == EX and sync_mode == SH
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-03-13 06:13:41 -04:00
|
|
|
runnable = false
|
|
|
|
case m
|
|
|
|
when UN
|
2011-05-18 17:19:18 -04:00
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-03-13 06:13:41 -04:00
|
|
|
when EX
|
2011-05-18 17:19:18 -04:00
|
|
|
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
|
|
|
|
runnable = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Err::UnknownLocker.Fail(Thread.current)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-03-13 06:13:41 -04:00
|
|
|
when SH
|
2011-05-18 17:19:18 -04:00
|
|
|
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
|
|
|
|
end
|
2007-03-13 06:13:41 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-03-13 06:13:41 -04:00
|
|
|
if runnable
|
2011-05-18 17:19:18 -04:00
|
|
|
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
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2007-03-13 06:13:41 -04:00
|
|
|
for th in wakeup_threads
|
|
|
|
th.run
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
self
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
def sync_synchronize(mode = EX)
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(StandardError => :on_blocking) do
|
2012-11-30 13:55:28 -05:00
|
|
|
sync_lock(mode)
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
sync_unlock
|
|
|
|
end
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
|
|
|
end
|
2001-06-06 10:19:33 -04:00
|
|
|
|
2006-07-20 13:36:36 -04:00
|
|
|
attr_accessor :sync_mode
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-07-20 13: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 06:13:41 -04: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 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
|
2007-03-13 06:13:41 -04:00
|
|
|
|
|
|
|
@sync_mutex = Mutex.new
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
2001-06-06 10:19:33 -04:00
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
super
|
2007-03-13 06:13:41 -04:00
|
|
|
sync_initialize
|
2001-06-06 10:19:33 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def sync_try_lock_sub(m)
|
|
|
|
case m
|
|
|
|
when SH
|
|
|
|
case sync_mode
|
|
|
|
when UN
|
2011-05-18 17:19:18 -04:00
|
|
|
self.sync_mode = m
|
|
|
|
sync_sh_locker[Thread.current] = 1
|
|
|
|
ret = true
|
1998-01-16 07:13:05 -05:00
|
|
|
when SH
|
2011-05-18 17:19:18 -04:00
|
|
|
count = 0 unless count = sync_sh_locker[Thread.current]
|
|
|
|
sync_sh_locker[Thread.current] = count + 1
|
|
|
|
ret = true
|
1998-01-16 07:13:05 -05:00
|
|
|
when EX
|
2011-05-18 17:19:18 -04:00
|
|
|
# in EX mode, lock will upgrade to EX lock
|
|
|
|
if sync_ex_locker == Thread.current
|
|
|
|
self.sync_ex_count = sync_ex_count + 1
|
|
|
|
ret = true
|
|
|
|
else
|
|
|
|
ret = false
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
when EX
|
|
|
|
if sync_mode == UN or
|
2011-05-18 20:07:25 -04:00
|
|
|
sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
|
2011-05-18 17:19:18 -04:00
|
|
|
self.sync_mode = m
|
|
|
|
self.sync_ex_locker = Thread.current
|
|
|
|
self.sync_ex_count = 1
|
|
|
|
ret = true
|
1998-01-16 07:13:05 -05:00
|
|
|
elsif sync_mode == EX && sync_ex_locker == Thread.current
|
2011-05-18 17:19:18 -04:00
|
|
|
self.sync_ex_count = sync_ex_count + 1
|
|
|
|
ret = true
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
ret = false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
Err::LockModeFailer.Fail mode
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
2011-06-28 23:09:34 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# An alias for Sync_m from sync.rb
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
Synchronizer_m = Sync_m
|
|
|
|
|
2011-06-28 23:09:34 -04:00
|
|
|
##
|
|
|
|
# A class that providesa two-phase lock with a counter. See Sync_m for
|
|
|
|
# details.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
class Sync
|
1999-08-13 01:45:20 -04:00
|
|
|
include Sync_m
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2011-06-28 23:09:34 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# An alias for Sync from sync.rb. See Sync_m for details.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
Synchronizer = Sync
|