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.



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

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

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
	@ready.num_waiting.zero?
end

#signal(value = nil) ⇒ Object

Signal to a given task that it should resume operations.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/async/condition.rb', line 43

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.



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

def wait
	@ready.pop
end

#waiting?Boolean

Returns:

  • (Boolean)


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

def waiting?
	!self.empty?
end

#waiting_countObject



37
38
39
# File 'lib/async/condition.rb', line 37

def waiting_count
	@ready.num_waiting
end