Class: Polyphony::ConditionVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/polyphony/core/sync.rb

Overview

Implements a fiber-aware ConditionVariable

Instance Method Summary collapse

Constructor Details

#initializeConditionVariable

Initializes the condition variable.



196
197
198
# File 'lib/polyphony/core/sync.rb', line 196

def initialize
  @queue = Polyphony::Queue.new
end

Instance Method Details

#broadcastObject

Resumes all waiting fibers.



230
231
232
233
234
235
236
# File 'lib/polyphony/core/sync.rb', line 230

def broadcast
  return if @queue.empty?

  while (fiber = @queue.shift)
    fiber.schedule
  end
end

#signalFiber

Signals the condition variable, causing the first fiber in the waiting queue to be resumed.

Returns:

  • (Fiber)

    resumed fiber



222
223
224
225
226
227
# File 'lib/polyphony/core/sync.rb', line 222

def signal
  return if @queue.empty?

  fiber = @queue.shift
  fiber.schedule
end

#wait(mutex, timeout = nil) ⇒ any

Waits for the condition variable to be signalled.

Parameters:

  • mutex (Polyphony::Mutex)

    mutex to release while waiting for signal

  • timeout (Number, nil) (defaults to: nil)

    timeout in seconds, or nil for no timeout

Returns:

  • (any)


205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/polyphony/core/sync.rb', line 205

def wait(mutex, timeout = nil)
  mutex.conditional_release
  @queue << Fiber.current
  if timeout
    move_on_after(timeout, with_value: false) { Polyphony.backend_wait_event(true); true }
  else
    Polyphony.backend_wait_event(true)
    true
  end
ensure
  mutex.conditional_reacquire
end