class Needle::QueryableMutex
A subclass of Mutex that allows clients to discover which thread has the mutex locked. This is necessary for (among other things) discovering cycles in the dependency graph of services.
Public Class Methods
new()
click to toggle source
Create a new unlocked QueryableMutex.
Calls superclass method
# File lib/needle/thread.rb, line 27 def initialize super @locking_thread = nil end
Public Instance Methods
lock()
click to toggle source
Checks to see if the current thread has the mutex locked, and if it does, raises an exception. Otherwise, locks the mutex and sets the locking thread to Thread.current.
# File lib/needle/thread.rb, line 35 def lock if @locking_thread == Thread.current raise ThreadError, "an attempt was made to reacquire an existing lock " + "(this could happen if you have a circular dependency on a service)" end while (Thread.critical = true; @locked) @waiting.push Thread.current Thread.stop end @locked = true @locking_thread = Thread.current Thread.critical = false self end
self_locked?()
click to toggle source
Return true
if the current thread has locked this mutex.
# File lib/needle/thread.rb, line 87 def self_locked? @locking_thread == Thread.current end
try_lock()
click to toggle source
Attempts to acquire the lock. Returns true
if the lock was
acquired, and false
if the mutex was already locked.
# File lib/needle/thread.rb, line 54 def try_lock result = false Thread.critical = true unless @locked @locked = true result = true @locking_thread = Thread.current end Thread.critical = false result end
unlock()
click to toggle source
Unlocks the mutex and sets the locking thread to nil
.
# File lib/needle/thread.rb, line 67 def unlock return unless @locked Thread.critical = true @locked = false begin t = @waiting.shift t.wakeup if t rescue ThreadError retry end @locking_thread = nil Thread.critical = false begin t.run if t rescue ThreadError end self end