diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1999-08-13 05:45:20 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1999-08-13 05:45:20 +0000 |
commit | 65a5162550f58047974793cdc8067a970b2435c0 (patch) | |
tree | 082bb7d5568f3b2e36e3fe166e9f3039394fcf44 /lib/sync.rb | |
parent | fcd020c83028f5610d382e85a2df00223e12bd7e (diff) |
1.4.0
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@520 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/sync.rb')
-rw-r--r-- | lib/sync.rb | 272 |
1 files changed, 74 insertions, 198 deletions
diff --git a/lib/sync.rb b/lib/sync.rb index 9f9706d9ee..f1410af1a9 100644 --- a/lib/sync.rb +++ b/lib/sync.rb @@ -1,5 +1,5 @@ # -# sync.rb - カウント付2-フェーズロッククラス +# sync.rb - 2 phase lock with counter # $Release Version: 0.2$ # $Revision$ # $Date$ @@ -44,8 +44,6 @@ unless defined? Thread fail "Thread not available for this ruby interpreter" end -require "final" - module Sync_m RCS_ID='-$Header$-' @@ -54,7 +52,7 @@ module Sync_m SH = :SH EX = :EX - # 例外定義 + # exceptions class Err < StandardError def Err.Fail(*opt) fail self, sprintf(self::Message, *opt) @@ -78,51 +76,27 @@ module Sync_m end end - # include and extend initialize methods. - def Sync_m.extendable_module(obj) - if Fixnum === obj or TRUE === obj or FALSE === obj or nil == obj - raise TypeError, "Sync_m can't extend to this class(#{obj.type})" - else - begin - obj.instance_eval "@sync_locked" - For_general_object - rescue TypeError - For_primitive_object - end - end - end - - def Sync_m.includable_module(cl) - begin - dummy = cl.new - Sync_m.extendable_module(dummy) - rescue NameError - # newが定義されていない時は, DATAとみなす. - For_primitive_object + def Sync_m.append_features(cl) + super + unless cl.instance_of?(Module) + # do nothing for Modules + # make aliases and include the proper module. + 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 - end - - def Sync_m.extend_class(cl) - return super if cl.instance_of?(Module) - - # モジュールの時は何もしない. クラスの場合, 適切なモジュールの決定 - # とaliasを行う. - real = includable_module(cl) - cl.module_eval %q{ - include real - - 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 - } + return self end def Sync_m.extend_object(obj) - obj.extend(Sync_m.extendable_module(obj)) + super + obj.sync_extended end def sync_extended @@ -143,6 +117,7 @@ module Sync_m alias synchronize sync_synchronize end" end + initialize end # accessing @@ -162,16 +137,16 @@ module Sync_m def sync_try_lock(mode = EX) return unlock if sync_mode == UN - Thread.critical = TRUE + Thread.critical = true ret = sync_try_lock_sub(sync_mode) - Thread.critical = FALSE + Thread.critical = false ret end def sync_lock(m = EX) return unlock if m == UN - until (Thread.critical = TRUE; sync_try_lock_sub(m)) + until (Thread.critical = true; sync_try_lock_sub(m)) if sync_sh_locker[Thread.current] sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]] sync_sh_locker.delete(Thread.current) @@ -180,23 +155,23 @@ module Sync_m end Thread.stop end - Thread.critical = FALSE + Thread.critical = false self end def sync_unlock(m = EX) - Thread.critical = TRUE + Thread.critical = true if sync_mode == UN - Thread.critical = FALSE + Thread.critical = false Err::UnknownLocker.Fail(Thread.current) end m = sync_mode if m == EX and sync_mode == SH - runnable = FALSE + runnable = false case m when UN - Thread.critical = FALSE + Thread.critical = false Err::UnknownLocker.Fail(Thread.current) when EX @@ -208,7 +183,7 @@ module Sync_m else self.sync_mode = UN end - runnable = TRUE + runnable = true end else Err::UnknownLocker.Fail(Thread.current) @@ -222,7 +197,7 @@ module Sync_m sync_sh_locker.delete(Thread.current) if sync_sh_locker.empty? and sync_ex_count == 0 self.sync_mode = UN - runnable = TRUE + runnable = true end end end @@ -235,7 +210,7 @@ module Sync_m end wait = sync_upgrade_waiting self.sync_upgrade_waiting = [] - Thread.critical = FALSE + Thread.critical = false for w, v in wait w.run @@ -243,17 +218,46 @@ module Sync_m else wait = sync_waiting self.sync_waiting = [] - Thread.critical = FALSE + Thread.critical = false for w in wait w.run end end end - Thread.critical = FALSE + Thread.critical = false self end + def sync_synchronize(mode = EX) + sync_lock(mode) + begin + yield + ensure + sync_unlock + end + end + + attr :sync_mode, true + attr :sync_waiting, true + attr :sync_upgrade_waiting, true + attr :sync_sh_locker, true + attr :sync_ex_locker, true + attr :sync_ex_count, true + + private + + def initialize(*args) + ret = super + @sync_mode = UN + @sync_waiting = [] + @sync_upgrade_waiting = [] + @sync_sh_locker = Hash.new + @sync_ex_locker = nil + @sync_ex_count = 0 + return ret + end + def sync_try_lock_sub(m) case m when SH @@ -261,18 +265,18 @@ module Sync_m when UN self.sync_mode = m sync_sh_locker[Thread.current] = 1 - ret = TRUE + ret = true when SH count = 0 unless count = sync_sh_locker[Thread.current] sync_sh_locker[Thread.current] = count + 1 - ret = TRUE + ret = true when EX - # 既に, モードがEXである時は, 必ずEXロックとなる. + # 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 + ret = true else - ret = FALSE + ret = false end end when EX @@ -281,157 +285,29 @@ module Sync_m self.sync_mode = m self.sync_ex_locker = Thread.current self.sync_ex_count = 1 - ret = TRUE + ret = true elsif sync_mode == EX && sync_ex_locker == Thread.current self.sync_ex_count = sync_ex_count + 1 - ret = TRUE + ret = true else - ret = FALSE + ret = false end else - Thread.critical = FALSE + Thread.critical = false Err::LockModeFailer.Fail mode end return ret end - private :sync_try_lock_sub - - def sync_synchronize(mode = EX) - sync_lock(mode) - begin - yield - ensure - sync_unlock - end - end - - # internal class - module For_primitive_object - include Sync_m - - LockState = Struct.new("LockState", - :mode, - :waiting, - :upgrade_waiting, - :sh_locker, - :ex_locker, - :ex_count) - - Sync_Locked = Hash.new - - def For_primitive_object.extend_object(obj) - super - obj.sync_extended - # Changed to use `final.rb'. - # Finalizer.add(obj, For_primitive_object, :sync_finalize) - ObjectSpace.define_finalizer(obj) do |id| - For_primitive_object.sync_finalize(id) - end - end - - def initialize - super - Sync_Locked[id] = LockState.new(UN, [], [], Hash.new, nil, 0 ) - self - end - - def sync_extended - super - initialize - end - - def For_primitive_object.sync_finalize(id) - wait = Sync_Locked.delete(id) - # waiting == [] ときだけ GCされるので, 待ち行列の解放は意味がない. - end - - def sync_mode - Sync_Locked[id].mode - end - def sync_mode=(value) - Sync_Locked[id].mode = value - end - - def sync_waiting - Sync_Locked[id].waiting - end - def sync_waiting=(v) - Sync_Locked[id].waiting = v - end - - def sync_upgrade_waiting - Sync_Locked[id].upgrade_waiting - end - def sync_upgrade_waiting=(v) - Sync_Locked[id].upgrade_waiting = v - end - - def sync_sh_locker - Sync_Locked[id].sh_locker - end - def sync_sh_locker=(v) - Sync_Locked[id].sh_locker = v - end - - def sync_ex_locker - Sync_Locked[id].ex_locker - end - def sync_ex_locker=(value) - Sync_Locked[id].ex_locker = value - end - - def sync_ex_count - Sync_Locked[id].ex_count - end - def sync_ex_count=(value) - Sync_Locked[id].ex_count = value - end - - end - - module For_general_object - include Sync_m - - def For_general_object.extend_object(obj) - super - obj.sync_extended - end - - def initialize - super - @sync_mode = UN - @sync_waiting = [] - @sync_upgrade_waiting = [] - @sync_sh_locker = Hash.new - @sync_ex_locker = nil - @sync_ex_count = 0 - self - end - - def sync_extended - super - initialize - end - - attr :sync_mode, TRUE - - attr :sync_waiting, TRUE - attr :sync_upgrade_waiting, TRUE - attr :sync_sh_locker, TRUE - attr :sync_ex_locker, TRUE - attr :sync_ex_count, TRUE - - end end Synchronizer_m = Sync_m class Sync - Sync_m.extend_class self - #include Sync_m + include Sync_m + + private def initialize super end - end Synchronizer = Sync |