Method: Needle::QueryableMutex#lock

Defined in:
lib/needle/thread.rb

#lockObject

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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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