Class: Async::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/async/condition.rb

Overview

A synchronization primative, which allows fibers to wait until a particular condition is triggered. Signalling the condition directly resumes the waiting fibers and thus blocks the caller.

Direct Known Subclasses

Notification

Instance Method Summary collapse

Constructor Details

#initializeCondition

Returns a new instance of Condition.



28
29
30
# File 'lib/async/condition.rb', line 28

def initialize
  @waiting = []
end

Instance Method Details

#empty?Boolean

Is any fiber waiting on this notification?

Returns:

  • (Boolean)


42
43
44
# File 'lib/async/condition.rb', line 42

def empty?
  @waiting.empty?
end

#signal(value = nil) ⇒ void

This method returns an undefined value.

Signal to a given task that it should resume operations.

Parameters:

  • value (defaults to: nil)

    The value to return to the waiting fibers.

See Also:



50
51
52
53
54
55
56
57
58
59
# File 'lib/async/condition.rb', line 50

def signal(value = nil)
  waiting = @waiting
  @waiting = []
  
  waiting.each do |fiber|
    fiber.resume(value) if fiber.alive?
  end
  
  return nil
end

#waitObject

Queue up the current fiber and wait on yielding the task.

Returns:

  • (Object)


34
35
36
37
38
# File 'lib/async/condition.rb', line 34

def wait
  @waiting << Fiber.current
  
  Task.yield
end