A module that provides a two-phase lock with a counter.
Methods
- A
- D
- E
- N
- S
Constants
UN | = | :UN |
lock mode |
||
SH | = | :SH |
EX | = | :EX |
Attributes
[RW] | sync_ex_count | |
[RW] | sync_ex_locker | |
[RW] | sync_mode | |
[RW] | sync_sh_locker | |
[RW] | sync_upgrade_waiting | |
[RW] | sync_waiting |
Class Public methods
append_features(cl)
Link
define_aliases(cl)
Link
# File lib/sync.rb, line 77 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
extend_object(obj)
Link
new(*args)
Link
Instance Public methods
sync_exclusive?()
Link
sync_extend()
Link
sync_inspect()
Link
sync_lock(m = EX)
Link
# File lib/sync.rb, line 136 def sync_lock(m = EX) return unlock if m == UN Thread.handle_interrupt(StandardError => :on_blocking) do while true @sync_mutex.synchronize do begin 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 unless sync_waiting.include?(Thread.current) || sync_upgrade_waiting.reverse_each.any?{|w| w.first == Thread.current } sync_waiting.push Thread.current end end @sync_mutex.sleep end ensure sync_waiting.delete(Thread.current) end end end end self end
sync_locked?()
Link
accessing
sync_synchronize(mode = EX)
Link
sync_try_lock(mode = EX)
Link
locking methods.
sync_unlock(m = EX)
Link
# File lib/sync.rb, line 164 def sync_unlock(m = EX) wakeup_threads = [] @sync_mutex.synchronize do if sync_mode == UN Err::UnknownLocker.Fail(Thread.current) end m = sync_mode if m == EX and sync_mode == SH runnable = false case m when UN 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 runnable = true 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 runnable = true end end end 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 end end end for th in wakeup_threads th.run end self end