Class: Async::Barrier

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

Overview

A semaphore is used to control access to a common resource in a concurrent system. A useful way to think of a semaphore as used in the real-world systems is as a record of how many units of a particular resource are available, coupled with operations to adjust that record safely (i.e. to avoid race conditions) as units are required or become free, and, if necessary, wait until a unit of the resource becomes available.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil) ⇒ Barrier

Returns a new instance of Barrier.



26
27
28
29
30
# File 'lib/async/barrier.rb', line 26

def initialize(parent: nil)
	@tasks = []
	
	@parent = parent
end

Instance Attribute Details

#tasksObject (readonly)

All tasks which have been invoked into the barrier.



33
34
35
# File 'lib/async/barrier.rb', line 33

def tasks
  @tasks
end

Instance Method Details

#async(*arguments, parent: (@parent or Task.current), **options, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/async/barrier.rb', line 39

def async(*arguments, parent: (@parent or Task.current), **options, &block)
	task = parent.async(*arguments, **options, &block)
	
	@tasks << task
	
	return task
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
	@tasks.empty?
end

#sizeObject



35
36
37
# File 'lib/async/barrier.rb', line 35

def size
	@tasks.size
end

#waitObject

Wait for tasks in FIFO order.



52
53
54
55
56
# File 'lib/async/barrier.rb', line 52

def wait
	while task = @tasks.shift
		task.wait
	end
end