Class: Dynflow::Future::CountDownLatch

Inherits:
Object
  • Object
show all
Defined in:
lib/dynflow/future.rb

Overview

‘#future` will become resolved to `true` when “#countdown!“ is called `count` times

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count, future = Future.new) ⇒ CountDownLatch

Returns a new instance of CountDownLatch.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
# File 'lib/dynflow/future.rb', line 12

def initialize(count, future = Future.new)
  raise ArgumentError if count < 0
  @count  = count
  @lock   = Mutex.new
  @future = future
end

Instance Attribute Details

#futureObject (readonly)

Returns the value of attribute future.



10
11
12
# File 'lib/dynflow/future.rb', line 10

def future
  @future
end

Instance Method Details

#countObject



26
27
28
# File 'lib/dynflow/future.rb', line 26

def count
  @lock.synchronize { @count }
end

#countdown!Object



19
20
21
22
23
24
# File 'lib/dynflow/future.rb', line 19

def countdown!
  @lock.synchronize do
    @count -= 1 if @count > 0
    @future.resolve true if @count == 0 && !@future.ready?
  end
end