Class: Async::Condition

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

Overview

A synchronization primitive, which allows fibers to wait until a particular condition is (edge) triggered.

Direct Known Subclasses

Notification

Instance Method Summary collapse

Constructor Details

#initializeCondition

Returns a new instance of Condition.



14
15
16
# File 'lib/async/condition.rb', line 14

def initialize
  @waiting = []
end

Instance Method Details

#empty?Boolean

Is any fiber waiting on this notification?

Returns:

  • (Boolean)


47
48
49
# File 'lib/async/condition.rb', line 47

def empty?
  @waiting.empty?
end

#signal(value = nil) ⇒ Object

Signal to a given task that it should resume operations.



53
54
55
56
57
58
59
60
61
62
# File 'lib/async/condition.rb', line 53

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

#waitObject

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



36
37
38
39
40
41
42
43
# File 'lib/async/condition.rb', line 36

def wait
  queue = Queue.new(Fiber.current)
  @waiting << queue
  
  Fiber.scheduler.transfer
ensure
  queue.nullify
end