class EventMachine::Synchrony::Thread::Mutex

Fiber-aware drop-in replacements for thread objects

Public Class Methods

new() click to toggle source
# File lib/em-synchrony/thread.rb, line 7
def initialize
  @waiters = []
  @slept = {}
end

Public Instance Methods

_wakeup(fiber) click to toggle source
# File lib/em-synchrony/thread.rb, line 24
def _wakeup(fiber)
  fiber.resume if @slept.delete(fiber)
end
lock() click to toggle source
# File lib/em-synchrony/thread.rb, line 12
def lock
  current = Fiber.current
  raise FiberError if @waiters.include?(current)
  @waiters << current
  Fiber.yield unless @waiters.first == current
  true
end
locked?() click to toggle source
# File lib/em-synchrony/thread.rb, line 20
def locked?
  !@waiters.empty?
end
sleep(timeout = nil) { || ... } click to toggle source
# File lib/em-synchrony/thread.rb, line 28
def sleep(timeout = nil)
  unlock    
  beg = Time.now
  current = Fiber.current
  @slept[current] = true
  if timeout
    timer = EM.add_timer(timeout) do
      _wakeup(current)
    end
    Fiber.yield
    EM.cancel_timer timer # if we resumes not via timer
  else
    Fiber.yield
  end
  @slept.delete current
  yield if block_given?
  lock
  Time.now - beg
end
synchronize() { || ... } click to toggle source
# File lib/em-synchrony/thread.rb, line 61
def synchronize
  lock
  yield
ensure
  unlock
end
try_lock() click to toggle source
# File lib/em-synchrony/thread.rb, line 48
def try_lock
  lock unless locked?
end
unlock() click to toggle source
# File lib/em-synchrony/thread.rb, line 52
def unlock
  raise FiberError unless @waiters.first == Fiber.current  
  @waiters.shift
  unless @waiters.empty?
    EM.next_tick{ @waiters.first.resume }
  end
  self
end