Class: Green::ConditionVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/green/semaphore.rb

Instance Method Summary collapse

Constructor Details

#initializeConditionVariable

Returns a new instance of ConditionVariable.



124
125
126
# File 'lib/green/semaphore.rb', line 124

def initialize
  @waiters = []
end

Instance Method Details

#_wakeup(mutex, green) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/green/semaphore.rb', line 144

def _wakeup(mutex, green)
  if alive = green.alive?
    Green.hub.callback {
      mutex._wakeup(green)
    }
  end
  alive
end

#broadcastObject

Wakes up all threads waiting for this lock.



166
167
168
169
170
171
172
# File 'lib/green/semaphore.rb', line 166

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

#signalObject

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



156
157
158
159
160
161
# File 'lib/green/semaphore.rb', line 156

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.



134
135
136
137
138
139
140
141
142
# File 'lib/green/semaphore.rb', line 134

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