-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
mutex_count_down_latch.rb
44 lines (35 loc) 路 1014 Bytes
/
mutex_count_down_latch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'concurrent/synchronization'
require 'concurrent/utility/native_integer'
module Concurrent
# @!macro count_down_latch
# @!visibility private
# @!macro internal_implementation_note
class MutexCountDownLatch < Synchronization::LockableObject
# @!macro count_down_latch_method_initialize
def initialize(count = 1)
Utility::NativeInteger.ensure_integer_and_bounds count
Utility::NativeInteger.ensure_positive count
super()
synchronize { ns_initialize count }
end
# @!macro count_down_latch_method_wait
def wait(timeout = nil)
synchronize { ns_wait_until(timeout) { @count == 0 } }
end
# @!macro count_down_latch_method_count_down
def count_down
synchronize do
@count -= 1 if @count > 0
ns_broadcast if @count == 0
end
end
# @!macro count_down_latch_method_count
def count
synchronize { @count }
end
protected
def ns_initialize(count)
@count = count
end
end
end