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

Create a new condition.



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

def initialize
	@ready = ::Thread::Queue.new
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/async/condition.rb', line 26

def empty?
	@ready.num_waiting.zero?
end

#signal(value = nil) ⇒ Object

Signal to a given task that it should resume operations.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/async/condition.rb', line 37

def signal(value = nil)
	return if empty?
	
	ready = self.exchange
	
	ready.num_waiting.times do
		ready.push(value)
	end
	
	ready.close
	
	return nil
end

#waitObject

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



21
22
23
# File 'lib/async/condition.rb', line 21

def wait
	@ready.pop
end

#waiting?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/async/condition.rb', line 31

def waiting?
	!self.empty?
end