# frozen_string_literal: false
# sync.rb - 2 phase lock with counter
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
# 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_unlock, unlock
# Sync#try_lock(mode) -- mode = :EX, :SH, :UN
# Sync#lock(mode) -- mode = :EX, :SH, :UN
# Sync#synchronize(mode) {...}
raise "Thread not available for this ruby interpreter"
# A module that provides a two-phase lock with a counter.
class Err < StandardError
fail self, sprintf(self::Message, *opt)
class UnknownLocker < Err
Message = "Thread(%s) not locked."
def UnknownLocker.Fail(th)
class LockModeFailer < Err
Message = "Unknown lock mode(%s)"
def LockModeFailer.Fail(mode)
def Sync_m.define_aliases(cl)
alias locked? sync_locked?
alias shared? sync_shared?
alias exclusive? sync_exclusive?
alias try_lock sync_try_lock
alias synchronize sync_synchronize
def Sync_m.append_features(cl)
# make aliases for Classes.
define_aliases(cl) unless cl.instance_of?(Module)
def Sync_m.extend_object(obj)
unless (defined? locked? and
Sync_m.define_aliases(singleton_class)
def sync_try_lock(mode = EX)
return unlock if mode == UN
@sync_mutex.synchronize do
Thread.handle_interrupt(StandardError => :on_blocking) do
@sync_mutex.synchronize do
if sync_sh_locker[Thread.current]
sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]]
sync_sh_locker.delete(Thread.current)
unless sync_waiting.include?(Thread.current) || sync_upgrade_waiting.reverse_each.any?{|w| w.first == Thread.current }
sync_waiting.push Thread.current
sync_waiting.delete(Thread.current)
@sync_mutex.synchronize do
Err::UnknownLocker.Fail(Thread.current)
m = sync_mode if m == EX and sync_mode == SH
Err::UnknownLocker.Fail(Thread.current)
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)
Err::UnknownLocker.Fail(Thread.current)
if (count = sync_sh_locker[Thread.current]).nil?
Err::UnknownLocker.Fail(Thread.current)
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
if sync_upgrade_waiting.size > 0
th, count = sync_upgrade_waiting.shift
sync_sh_locker[th] = count
def sync_synchronize(mode = EX)
Thread.handle_interrupt(StandardError => :on_blocking) do
attr_accessor :sync_waiting
attr_accessor :sync_upgrade_waiting
attr_accessor :sync_sh_locker
attr_accessor :sync_ex_locker
attr_accessor :sync_ex_count
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}>"
@sync_upgrade_waiting = []
@sync_sh_locker = Hash.new
@sync_mutex = Thread::Mutex.new
sync_sh_locker[Thread.current] = 1
count = 0 unless count = sync_sh_locker[Thread.current]
sync_sh_locker[Thread.current] = count + 1
# in EX mode, lock will upgrade to EX lock
if sync_ex_locker == Thread.current
self.sync_ex_count = sync_ex_count + 1
sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
self.sync_ex_locker = Thread.current
elsif sync_mode == EX && sync_ex_locker == Thread.current
self.sync_ex_count = sync_ex_count + 1
Err::LockModeFailer.Fail m
# An alias for Sync_m from sync.rb
# A class that provides two-phase lock with a counter. See Sync_m for
# An alias for Sync from sync.rb. See Sync_m for details.