Class: Latch
- Inherits:
-
Object
- Object
- Latch
- Defined in:
- lib/latch.rb
Defined Under Namespace
Modules: Mixin Classes: Timeout
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Instance Method Summary collapse
- #await(timeout = nil) ⇒ Object
- #decr(n = 1) ⇒ Object
-
#initialize(count = 1) ⇒ Latch
constructor
A new instance of Latch.
Constructor Details
#initialize(count = 1) ⇒ 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 (readonly)
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
30 31 32 33 |
# File 'lib/latch.rb', line 30 def await(timeout = nil) @cv.wait(@mutex, timeout) 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 |