Class: Latch
- Inherits:
-
Object
show all
- Defined in:
- lib/latch.rb
Defined Under Namespace
Modules: Mixin
Classes: Timeout
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(count = 1) ⇒ Latch
Returns a new instance of Latch.
18
19
20
21
22
23
|
# File 'lib/latch.rb', line 18
def initialize(count = 1)
@count = 1
@mutex = Mutex.new
@mutex.lock
@cv = ConditionVariable.new
end
|
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
16
17
18
|
# File 'lib/latch.rb', line 16
def count
@count
end
|
Instance Method Details
#await(timeout = nil) ⇒ Object
41
42
43
44
45
|
# File 'lib/latch.rb', line 41
def await(timeout = nil)
@cv.wait(@mutex, timeout) if @count > 0
raise @exception if @exception
raise Latch::Timeout if @count > 0
end
|
#decr(n = 1) ⇒ Object
25
26
27
28
|
# File 'lib/latch.rb', line 25
def decr(n = 1)
@count -= n
@cv.broadcast if @count <= 0
end
|
#fail(exception) ⇒ Object
36
37
38
39
|
# File 'lib/latch.rb', line 36
def fail(exception)
@exception = exception
@cv.broadcast
end
|
#try(&block) ⇒ Object
30
31
32
33
34
|
# File 'lib/latch.rb', line 30
def try(&block)
block.call
rescue => e
fail(e)
end
|