Module: FiberedMysql2::FiberedMutexWithWaiterPriority

Defined in:
lib/fibered_mysql2/fibered_mutex_with_waiter_priority.rb

Instance Method Summary collapse

Instance Method Details

#sleep(timeout = nil) ⇒ Object

Note: @waiters is a bit confusing because the first waiter is actually the current fiber that has it locked; the rest of @waiters are the actual waiters



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fibered_mysql2/fibered_mutex_with_waiter_priority.rb', line 7

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 resumed not via timer
  else
    Fiber.yield
  end
  @slept.delete(current)
  yield if block_given?

  # Invoca patch: inline lock that puts us at the front of the mutex @waiters queue instead of the back
  # ==========================
  @waiters.unshift(current)
  Fiber.yield if @waiters.size > 1
  # ==========================

  Time.now - beg
end