Class: EventMachine::Synchrony::Thread::ConditionVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/em-synchrony/thread.rb

Instance Method Summary collapse

Constructor Details

#initializeConditionVariable

Creates a new ConditionVariable



74
75
76
# File 'lib/em-synchrony/thread.rb', line 74

def initialize
  @waiters = []
end

Instance Method Details

#_wakeup(mutex, fiber) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/em-synchrony/thread.rb', line 94

def _wakeup(mutex, fiber)
  if alive = fiber.alive?
    EM.next_tick {
      mutex._wakeup(fiber)
    }
  end
  alive
end

#broadcastObject

Wakes up all threads waiting for this lock.



116
117
118
119
120
121
122
# File 'lib/em-synchrony/thread.rb', line 116

def broadcast
  @waiters.each do |mutex, fiber|
    _wakeup(mutex, fiber)
  end
  @waiters.clear
  self
end

#signalObject

Wakes up the first thread in line waiting for this lock.



106
107
108
109
110
111
# File 'lib/em-synchrony/thread.rb', line 106

def signal
  while (pair = @waiters.shift)
    break if _wakeup(*pair)
  end
  self
end

#wait(mutex, timeout = nil) ⇒ Object

Releases the lock held in mutex and waits; reacquires the lock on wakeup.

If timeout is given, this method returns after timeout seconds passed, even if no other thread doesn’t signal.



84
85
86
87
88
89
90
91
92
# File 'lib/em-synchrony/thread.rb', line 84

def wait(mutex, timeout=nil)
  current = Fiber.current
  pair = [mutex, current]
  @waiters << pair
  mutex.sleep timeout do
    @waiters.delete pair
  end
  self
end