concurrent-ruby 0.8.0 → 0.9.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +97 -2
- data/README.md +103 -54
- data/lib/concurrent.rb +34 -14
- data/lib/concurrent/async.rb +164 -50
- data/lib/concurrent/atom.rb +171 -0
- data/lib/concurrent/atomic/atomic_boolean.rb +57 -107
- data/lib/concurrent/atomic/atomic_fixnum.rb +73 -101
- data/lib/concurrent/atomic/atomic_reference.rb +49 -0
- data/lib/concurrent/atomic/condition.rb +23 -12
- data/lib/concurrent/atomic/count_down_latch.rb +23 -21
- data/lib/concurrent/atomic/cyclic_barrier.rb +47 -47
- data/lib/concurrent/atomic/event.rb +33 -42
- data/lib/concurrent/atomic/read_write_lock.rb +252 -0
- data/lib/concurrent/atomic/semaphore.rb +64 -89
- data/lib/concurrent/atomic/thread_local_var.rb +130 -58
- data/lib/concurrent/atomic/thread_local_var/weak_key_map.rb +236 -0
- data/lib/concurrent/atomic_reference/direct_update.rb +3 -0
- data/lib/concurrent/atomic_reference/jruby.rb +6 -3
- data/lib/concurrent/atomic_reference/mutex_atomic.rb +10 -32
- data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +3 -0
- data/lib/concurrent/atomic_reference/rbx.rb +4 -1
- data/lib/concurrent/atomic_reference/ruby.rb +6 -3
- data/lib/concurrent/atomics.rb +74 -4
- data/lib/concurrent/collection/copy_on_notify_observer_set.rb +115 -0
- data/lib/concurrent/collection/copy_on_write_observer_set.rb +119 -0
- data/lib/concurrent/collection/priority_queue.rb +300 -245
- data/lib/concurrent/concern/deprecation.rb +27 -0
- data/lib/concurrent/concern/dereferenceable.rb +88 -0
- data/lib/concurrent/concern/logging.rb +25 -0
- data/lib/concurrent/concern/obligation.rb +228 -0
- data/lib/concurrent/concern/observable.rb +85 -0
- data/lib/concurrent/configuration.rb +226 -112
- data/lib/concurrent/dataflow.rb +2 -3
- data/lib/concurrent/delay.rb +141 -50
- data/lib/concurrent/edge.rb +30 -0
- data/lib/concurrent/errors.rb +10 -0
- data/lib/concurrent/exchanger.rb +25 -1
- data/lib/concurrent/executor/cached_thread_pool.rb +46 -33
- data/lib/concurrent/executor/executor.rb +46 -299
- data/lib/concurrent/executor/executor_service.rb +521 -0
- data/lib/concurrent/executor/fixed_thread_pool.rb +206 -26
- data/lib/concurrent/executor/immediate_executor.rb +9 -9
- data/lib/concurrent/executor/indirect_immediate_executor.rb +4 -3
- data/lib/concurrent/executor/java_cached_thread_pool.rb +18 -16
- data/lib/concurrent/executor/java_fixed_thread_pool.rb +11 -18
- data/lib/concurrent/executor/java_single_thread_executor.rb +17 -16
- data/lib/concurrent/executor/java_thread_pool_executor.rb +55 -102
- data/lib/concurrent/executor/ruby_cached_thread_pool.rb +9 -18
- data/lib/concurrent/executor/ruby_fixed_thread_pool.rb +10 -21
- data/lib/concurrent/executor/ruby_single_thread_executor.rb +14 -16
- data/lib/concurrent/executor/ruby_thread_pool_executor.rb +250 -166
- data/lib/concurrent/executor/safe_task_executor.rb +5 -4
- data/lib/concurrent/executor/serialized_execution.rb +22 -18
- data/lib/concurrent/executor/{per_thread_executor.rb → simple_executor_service.rb} +29 -20
- data/lib/concurrent/executor/single_thread_executor.rb +32 -21
- data/lib/concurrent/executor/thread_pool_executor.rb +72 -60
- data/lib/concurrent/executor/timer_set.rb +96 -84
- data/lib/concurrent/executors.rb +1 -1
- data/lib/concurrent/future.rb +70 -38
- data/lib/concurrent/immutable_struct.rb +89 -0
- data/lib/concurrent/ivar.rb +152 -60
- data/lib/concurrent/lazy_register.rb +40 -20
- data/lib/concurrent/maybe.rb +226 -0
- data/lib/concurrent/mutable_struct.rb +227 -0
- data/lib/concurrent/mvar.rb +44 -43
- data/lib/concurrent/promise.rb +208 -134
- data/lib/concurrent/scheduled_task.rb +339 -43
- data/lib/concurrent/settable_struct.rb +127 -0
- data/lib/concurrent/synchronization.rb +17 -0
- data/lib/concurrent/synchronization/abstract_object.rb +163 -0
- data/lib/concurrent/synchronization/abstract_struct.rb +158 -0
- data/lib/concurrent/synchronization/condition.rb +53 -0
- data/lib/concurrent/synchronization/java_object.rb +35 -0
- data/lib/concurrent/synchronization/lock.rb +32 -0
- data/lib/concurrent/synchronization/monitor_object.rb +24 -0
- data/lib/concurrent/synchronization/mutex_object.rb +43 -0
- data/lib/concurrent/synchronization/object.rb +78 -0
- data/lib/concurrent/synchronization/rbx_object.rb +75 -0
- data/lib/concurrent/timer_task.rb +87 -100
- data/lib/concurrent/tvar.rb +42 -38
- data/lib/concurrent/utilities.rb +3 -1
- data/lib/concurrent/utility/at_exit.rb +97 -0
- data/lib/concurrent/utility/engine.rb +40 -0
- data/lib/concurrent/utility/monotonic_time.rb +59 -0
- data/lib/concurrent/utility/native_extension_loader.rb +56 -0
- data/lib/concurrent/utility/processor_counter.rb +156 -0
- data/lib/concurrent/utility/timeout.rb +18 -14
- data/lib/concurrent/utility/timer.rb +11 -6
- data/lib/concurrent/version.rb +2 -1
- data/lib/concurrent_ruby.rb +1 -0
- metadata +47 -83
- data/lib/concurrent/actor.rb +0 -103
- data/lib/concurrent/actor/behaviour.rb +0 -70
- data/lib/concurrent/actor/behaviour/abstract.rb +0 -48
- data/lib/concurrent/actor/behaviour/awaits.rb +0 -21
- data/lib/concurrent/actor/behaviour/buffer.rb +0 -54
- data/lib/concurrent/actor/behaviour/errors_on_unknown_message.rb +0 -12
- data/lib/concurrent/actor/behaviour/executes_context.rb +0 -18
- data/lib/concurrent/actor/behaviour/linking.rb +0 -45
- data/lib/concurrent/actor/behaviour/pausing.rb +0 -77
- data/lib/concurrent/actor/behaviour/removes_child.rb +0 -16
- data/lib/concurrent/actor/behaviour/sets_results.rb +0 -36
- data/lib/concurrent/actor/behaviour/supervised.rb +0 -59
- data/lib/concurrent/actor/behaviour/supervising.rb +0 -34
- data/lib/concurrent/actor/behaviour/terminates_children.rb +0 -13
- data/lib/concurrent/actor/behaviour/termination.rb +0 -54
- data/lib/concurrent/actor/context.rb +0 -154
- data/lib/concurrent/actor/core.rb +0 -217
- data/lib/concurrent/actor/default_dead_letter_handler.rb +0 -9
- data/lib/concurrent/actor/envelope.rb +0 -41
- data/lib/concurrent/actor/errors.rb +0 -27
- data/lib/concurrent/actor/internal_delegations.rb +0 -49
- data/lib/concurrent/actor/public_delegations.rb +0 -40
- data/lib/concurrent/actor/reference.rb +0 -81
- data/lib/concurrent/actor/root.rb +0 -37
- data/lib/concurrent/actor/type_check.rb +0 -48
- data/lib/concurrent/actor/utils.rb +0 -10
- data/lib/concurrent/actor/utils/ad_hoc.rb +0 -21
- data/lib/concurrent/actor/utils/balancer.rb +0 -42
- data/lib/concurrent/actor/utils/broadcast.rb +0 -52
- data/lib/concurrent/actor/utils/pool.rb +0 -59
- data/lib/concurrent/actress.rb +0 -3
- data/lib/concurrent/agent.rb +0 -209
- data/lib/concurrent/atomic.rb +0 -92
- data/lib/concurrent/atomic/copy_on_notify_observer_set.rb +0 -118
- data/lib/concurrent/atomic/copy_on_write_observer_set.rb +0 -117
- data/lib/concurrent/atomic/synchronization.rb +0 -51
- data/lib/concurrent/channel/buffered_channel.rb +0 -85
- data/lib/concurrent/channel/channel.rb +0 -41
- data/lib/concurrent/channel/unbuffered_channel.rb +0 -35
- data/lib/concurrent/channel/waitable_list.rb +0 -40
- data/lib/concurrent/channels.rb +0 -5
- data/lib/concurrent/collection/blocking_ring_buffer.rb +0 -71
- data/lib/concurrent/collection/ring_buffer.rb +0 -59
- data/lib/concurrent/collections.rb +0 -3
- data/lib/concurrent/dereferenceable.rb +0 -108
- data/lib/concurrent/executor/ruby_thread_pool_worker.rb +0 -73
- data/lib/concurrent/logging.rb +0 -20
- data/lib/concurrent/obligation.rb +0 -171
- data/lib/concurrent/observable.rb +0 -73
- data/lib/concurrent/options_parser.rb +0 -52
- data/lib/concurrent/utility/processor_count.rb +0 -152
- data/lib/extension_helper.rb +0 -37
@@ -0,0 +1,252 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'concurrent/atomic/atomic_reference'
|
3
|
+
require 'concurrent/errors'
|
4
|
+
require 'concurrent/synchronization'
|
5
|
+
|
6
|
+
module Concurrent
|
7
|
+
|
8
|
+
# Ruby read-write lock implementation
|
9
|
+
#
|
10
|
+
# Allows any number of concurrent readers, but only one concurrent writer
|
11
|
+
# (And if the "write" lock is taken, any readers who come along will have to wait)
|
12
|
+
#
|
13
|
+
# If readers are already active when a writer comes along, the writer will wait for
|
14
|
+
# all the readers to finish before going ahead.
|
15
|
+
# Any additional readers that come when the writer is already waiting, will also
|
16
|
+
# wait (so writers are not starved).
|
17
|
+
#
|
18
|
+
# This implementation is based on `java.util.concurrent.ReentrantReadWriteLock`.
|
19
|
+
#
|
20
|
+
# @example
|
21
|
+
# lock = Concurrent::ReadWriteLock.new
|
22
|
+
# lock.with_read_lock { data.retrieve }
|
23
|
+
# lock.with_write_lock { data.modify! }
|
24
|
+
#
|
25
|
+
# @note Do **not** try to acquire the write lock while already holding a read lock
|
26
|
+
# **or** try to acquire the write lock while you already have it.
|
27
|
+
# This will lead to deadlock
|
28
|
+
#
|
29
|
+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html java.util.concurrent.ReentrantReadWriteLock
|
30
|
+
class ReadWriteLock < Synchronization::Object
|
31
|
+
|
32
|
+
# @!visibility private
|
33
|
+
WAITING_WRITER = 1 << 15
|
34
|
+
|
35
|
+
# @!visibility private
|
36
|
+
RUNNING_WRITER = 1 << 30
|
37
|
+
|
38
|
+
# @!visibility private
|
39
|
+
MAX_READERS = WAITING_WRITER - 1
|
40
|
+
|
41
|
+
# @!visibility private
|
42
|
+
MAX_WRITERS = RUNNING_WRITER - MAX_READERS - 1
|
43
|
+
|
44
|
+
# Implementation notes:
|
45
|
+
# A goal is to make the uncontended path for both readers/writers lock-free
|
46
|
+
# Only if there is reader-writer or writer-writer contention, should locks be used
|
47
|
+
# Internal state is represented by a single integer ("counter"), and updated
|
48
|
+
# using atomic compare-and-swap operations
|
49
|
+
# When the counter is 0, the lock is free
|
50
|
+
# Each reader increments the counter by 1 when acquiring a read lock
|
51
|
+
# (and decrements by 1 when releasing the read lock)
|
52
|
+
# The counter is increased by (1 << 15) for each writer waiting to acquire the
|
53
|
+
# write lock, and by (1 << 30) if the write lock is taken
|
54
|
+
|
55
|
+
# Create a new `ReadWriteLock` in the unlocked state.
|
56
|
+
def initialize
|
57
|
+
@Counter = AtomicReference.new(0) # single integer which represents lock state
|
58
|
+
@ReadLock = Synchronization::Lock.new
|
59
|
+
@WriteLock = Synchronization::Lock.new
|
60
|
+
ensure_ivar_visibility!
|
61
|
+
super()
|
62
|
+
end
|
63
|
+
|
64
|
+
# Execute a block operation within a read lock.
|
65
|
+
#
|
66
|
+
# @yield the task to be performed within the lock.
|
67
|
+
#
|
68
|
+
# @return [Object] the result of the block operation.
|
69
|
+
#
|
70
|
+
# @raise [ArgumentError] when no block is given.
|
71
|
+
# @raise [Concurrent::ResourceLimitError] if the maximum number of readers
|
72
|
+
# is exceeded.
|
73
|
+
def with_read_lock
|
74
|
+
raise ArgumentError.new('no block given') unless block_given?
|
75
|
+
acquire_read_lock
|
76
|
+
begin
|
77
|
+
yield
|
78
|
+
ensure
|
79
|
+
release_read_lock
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Execute a block operation within a write lock.
|
84
|
+
#
|
85
|
+
# @yield the task to be performed within the lock.
|
86
|
+
#
|
87
|
+
# @return [Object] the result of the block operation.
|
88
|
+
#
|
89
|
+
# @raise [ArgumentError] when no block is given.
|
90
|
+
# @raise [Concurrent::ResourceLimitError] if the maximum number of readers
|
91
|
+
# is exceeded.
|
92
|
+
def with_write_lock
|
93
|
+
raise ArgumentError.new('no block given') unless block_given?
|
94
|
+
acquire_write_lock
|
95
|
+
begin
|
96
|
+
yield
|
97
|
+
ensure
|
98
|
+
release_write_lock
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Acquire a read lock. If a write lock has been acquired will block until
|
103
|
+
# it is released. Will not block if other read locks have been acquired.
|
104
|
+
#
|
105
|
+
# @return [Boolean] true if the lock is successfully acquired
|
106
|
+
#
|
107
|
+
# @raise [Concurrent::ResourceLimitError] if the maximum number of readers
|
108
|
+
# is exceeded.
|
109
|
+
def acquire_read_lock
|
110
|
+
while true
|
111
|
+
c = @Counter.value
|
112
|
+
raise ResourceLimitError.new('Too many reader threads') if max_readers?(c)
|
113
|
+
|
114
|
+
# If a writer is waiting when we first queue up, we need to wait
|
115
|
+
if waiting_writer?(c)
|
116
|
+
@ReadLock.wait_until { !waiting_writer? }
|
117
|
+
|
118
|
+
# after a reader has waited once, they are allowed to "barge" ahead of waiting writers
|
119
|
+
# but if a writer is *running*, the reader still needs to wait (naturally)
|
120
|
+
while true
|
121
|
+
c = @Counter.value
|
122
|
+
if running_writer?(c)
|
123
|
+
@ReadLock.wait_until { !running_writer? }
|
124
|
+
else
|
125
|
+
return if @Counter.compare_and_swap(c, c+1)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
else
|
129
|
+
break if @Counter.compare_and_swap(c, c+1)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
true
|
133
|
+
end
|
134
|
+
|
135
|
+
# Release a previously acquired read lock.
|
136
|
+
#
|
137
|
+
# @return [Boolean] true if the lock is successfully released
|
138
|
+
def release_read_lock
|
139
|
+
while true
|
140
|
+
c = @Counter.value
|
141
|
+
if @Counter.compare_and_swap(c, c-1)
|
142
|
+
# If one or more writers were waiting, and we were the last reader, wake a writer up
|
143
|
+
if waiting_writer?(c) && running_readers(c) == 1
|
144
|
+
@WriteLock.signal
|
145
|
+
end
|
146
|
+
break
|
147
|
+
end
|
148
|
+
end
|
149
|
+
true
|
150
|
+
end
|
151
|
+
|
152
|
+
# Acquire a write lock. Will block and wait for all active readers and writers.
|
153
|
+
#
|
154
|
+
# @return [Boolean] true if the lock is successfully acquired
|
155
|
+
#
|
156
|
+
# @raise [Concurrent::ResourceLimitError] if the maximum number of writers
|
157
|
+
# is exceeded.
|
158
|
+
def acquire_write_lock
|
159
|
+
while true
|
160
|
+
c = @Counter.value
|
161
|
+
raise ResourceLimitError.new('Too many writer threads') if max_writers?(c)
|
162
|
+
|
163
|
+
if c == 0 # no readers OR writers running
|
164
|
+
# if we successfully swap the RUNNING_WRITER bit on, then we can go ahead
|
165
|
+
break if @Counter.compare_and_swap(0, RUNNING_WRITER)
|
166
|
+
elsif @Counter.compare_and_swap(c, c+WAITING_WRITER)
|
167
|
+
while true
|
168
|
+
# Now we have successfully incremented, so no more readers will be able to increment
|
169
|
+
# (they will wait instead)
|
170
|
+
# However, readers OR writers could decrement right here, OR another writer could increment
|
171
|
+
@WriteLock.wait_until do
|
172
|
+
# So we have to do another check inside the synchronized section
|
173
|
+
# If a writer OR reader is running, then go to sleep
|
174
|
+
c = @Counter.value
|
175
|
+
!running_writer?(c) && !running_readers?(c)
|
176
|
+
end
|
177
|
+
|
178
|
+
# We just came out of a wait
|
179
|
+
# If we successfully turn the RUNNING_WRITER bit on with an atomic swap,
|
180
|
+
# Then we are OK to stop waiting and go ahead
|
181
|
+
# Otherwise go back and wait again
|
182
|
+
c = @Counter.value
|
183
|
+
break if !running_writer?(c) && !running_readers?(c) && @Counter.compare_and_swap(c, c+RUNNING_WRITER-WAITING_WRITER)
|
184
|
+
end
|
185
|
+
break
|
186
|
+
end
|
187
|
+
end
|
188
|
+
true
|
189
|
+
end
|
190
|
+
|
191
|
+
# Release a previously acquired write lock.
|
192
|
+
#
|
193
|
+
# @return [Boolean] true if the lock is successfully released
|
194
|
+
def release_write_lock
|
195
|
+
c = @Counter.update { |c| c-RUNNING_WRITER }
|
196
|
+
@ReadLock.broadcast
|
197
|
+
@WriteLock.signal if waiting_writers(c) > 0
|
198
|
+
true
|
199
|
+
end
|
200
|
+
|
201
|
+
# Queries if the write lock is held by any thread.
|
202
|
+
#
|
203
|
+
# @return [Boolean] true if the write lock is held else false`
|
204
|
+
def write_locked?
|
205
|
+
@Counter.value >= RUNNING_WRITER
|
206
|
+
end
|
207
|
+
|
208
|
+
# Queries whether any threads are waiting to acquire the read or write lock.
|
209
|
+
#
|
210
|
+
# @return [Boolean] true if any threads are waiting for a lock else false
|
211
|
+
def has_waiters?
|
212
|
+
waiting_writer?(@Counter.value)
|
213
|
+
end
|
214
|
+
|
215
|
+
private
|
216
|
+
|
217
|
+
# @!visibility private
|
218
|
+
def running_readers(c = @Counter.value)
|
219
|
+
c & MAX_READERS
|
220
|
+
end
|
221
|
+
|
222
|
+
# @!visibility private
|
223
|
+
def running_readers?(c = @Counter.value)
|
224
|
+
(c & MAX_READERS) > 0
|
225
|
+
end
|
226
|
+
|
227
|
+
# @!visibility private
|
228
|
+
def running_writer?(c = @Counter.value)
|
229
|
+
c >= RUNNING_WRITER
|
230
|
+
end
|
231
|
+
|
232
|
+
# @!visibility private
|
233
|
+
def waiting_writers(c = @Counter.value)
|
234
|
+
(c & MAX_WRITERS) / WAITING_WRITER
|
235
|
+
end
|
236
|
+
|
237
|
+
# @!visibility private
|
238
|
+
def waiting_writer?(c = @Counter.value)
|
239
|
+
c >= WAITING_WRITER
|
240
|
+
end
|
241
|
+
|
242
|
+
# @!visibility private
|
243
|
+
def max_readers?(c = @Counter.value)
|
244
|
+
(c & MAX_READERS) == MAX_READERS
|
245
|
+
end
|
246
|
+
|
247
|
+
# @!visibility private
|
248
|
+
def max_writers?(c = @Counter.value)
|
249
|
+
(c & MAX_WRITERS) == MAX_WRITERS
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
@@ -1,7 +1,21 @@
|
|
1
|
-
require 'concurrent/
|
1
|
+
require 'concurrent/synchronization'
|
2
2
|
|
3
3
|
module Concurrent
|
4
|
-
|
4
|
+
|
5
|
+
# @!macro [attach] semaphore
|
6
|
+
#
|
7
|
+
# A counting semaphore. Conceptually, a semaphore maintains a set of
|
8
|
+
# permits. Each {#acquire} blocks if necessary until a permit is
|
9
|
+
# available, and then takes it. Each {#release} adds a permit, potentially
|
10
|
+
# releasing a blocking acquirer.
|
11
|
+
# However, no actual permit objects are used; the Semaphore just keeps a
|
12
|
+
# count of the number available and acts accordingly.
|
13
|
+
#
|
14
|
+
# @!visibility private
|
15
|
+
#
|
16
|
+
# @!macro internal_implementation_note
|
17
|
+
class MutexSemaphore < Synchronization::Object
|
18
|
+
|
5
19
|
# @!macro [attach] semaphore_method_initialize
|
6
20
|
#
|
7
21
|
# Create a new `Semaphore` with the initial `count`.
|
@@ -13,9 +27,8 @@ module Concurrent
|
|
13
27
|
unless count.is_a?(Fixnum) && count >= 0
|
14
28
|
fail ArgumentError, 'count must be an non-negative integer'
|
15
29
|
end
|
16
|
-
|
17
|
-
|
18
|
-
@free = count
|
30
|
+
super()
|
31
|
+
synchronize { ns_initialize count }
|
19
32
|
end
|
20
33
|
|
21
34
|
# @!macro [attach] semaphore_method_acquire
|
@@ -33,7 +46,7 @@ module Concurrent
|
|
33
46
|
unless permits.is_a?(Fixnum) && permits > 0
|
34
47
|
fail ArgumentError, 'permits must be an integer greater than zero'
|
35
48
|
end
|
36
|
-
|
49
|
+
synchronize do
|
37
50
|
try_acquire_timed(permits, nil)
|
38
51
|
nil
|
39
52
|
end
|
@@ -45,7 +58,7 @@ module Concurrent
|
|
45
58
|
#
|
46
59
|
# @return [Integer]
|
47
60
|
def available_permits
|
48
|
-
|
61
|
+
synchronize { @free }
|
49
62
|
end
|
50
63
|
|
51
64
|
# @!macro [attach] semaphore_method_drain_permits
|
@@ -54,7 +67,7 @@ module Concurrent
|
|
54
67
|
#
|
55
68
|
# @return [Integer]
|
56
69
|
def drain_permits
|
57
|
-
|
70
|
+
synchronize do
|
58
71
|
@free.tap { |_| @free = 0 }
|
59
72
|
end
|
60
73
|
end
|
@@ -79,7 +92,7 @@ module Concurrent
|
|
79
92
|
unless permits.is_a?(Fixnum) && permits > 0
|
80
93
|
fail ArgumentError, 'permits must be an integer greater than zero'
|
81
94
|
end
|
82
|
-
|
95
|
+
synchronize do
|
83
96
|
if timeout.nil?
|
84
97
|
try_acquire_now(permits)
|
85
98
|
else
|
@@ -101,15 +114,15 @@ module Concurrent
|
|
101
114
|
unless permits.is_a?(Fixnum) && permits > 0
|
102
115
|
fail ArgumentError, 'permits must be an integer greater than zero'
|
103
116
|
end
|
104
|
-
|
117
|
+
synchronize do
|
105
118
|
@free += permits
|
106
|
-
permits.times {
|
119
|
+
permits.times { ns_signal }
|
107
120
|
end
|
108
121
|
nil
|
109
122
|
end
|
110
123
|
|
111
124
|
# @!macro [attach] semaphore_method_reduce_permits
|
112
|
-
#
|
125
|
+
#
|
113
126
|
# @api private
|
114
127
|
#
|
115
128
|
# Shrinks the number of available permits by the indicated reduction.
|
@@ -125,12 +138,20 @@ module Concurrent
|
|
125
138
|
unless reduction.is_a?(Fixnum) && reduction >= 0
|
126
139
|
fail ArgumentError, 'reduction must be an non-negative integer'
|
127
140
|
end
|
128
|
-
|
129
|
-
nil
|
141
|
+
synchronize { @free -= reduction }
|
142
|
+
nil
|
143
|
+
end
|
144
|
+
|
145
|
+
protected
|
146
|
+
|
147
|
+
# @!visibility private
|
148
|
+
def ns_initialize(count)
|
149
|
+
@free = count
|
130
150
|
end
|
131
151
|
|
132
152
|
private
|
133
153
|
|
154
|
+
# @!visibility private
|
134
155
|
def try_acquire_now(permits)
|
135
156
|
if @free >= permits
|
136
157
|
@free -= permits
|
@@ -140,93 +161,47 @@ module Concurrent
|
|
140
161
|
end
|
141
162
|
end
|
142
163
|
|
164
|
+
# @!visibility private
|
143
165
|
def try_acquire_timed(permits, timeout)
|
144
|
-
|
145
|
-
while !try_acquire_now(permits) && remaining.can_wait?
|
146
|
-
@condition.signal
|
147
|
-
remaining = @condition.wait(@mutex, remaining.remaining_time)
|
148
|
-
end
|
149
|
-
remaining.can_wait? ? true : false
|
166
|
+
ns_wait_until(timeout) { try_acquire_now(permits) }
|
150
167
|
end
|
151
168
|
end
|
152
169
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
class JavaSemaphore
|
163
|
-
# @!macro semaphore_method_initialize
|
164
|
-
def initialize(count)
|
165
|
-
unless count.is_a?(Fixnum) && count >= 0
|
166
|
-
fail(ArgumentError,
|
167
|
-
'count must be in integer greater than or equal zero')
|
168
|
-
end
|
169
|
-
@semaphore = java.util.concurrent.Semaphore.new(count)
|
170
|
-
end
|
170
|
+
# @!visibility private
|
171
|
+
# @!macro internal_implementation_note
|
172
|
+
SemaphoreImplementation = case
|
173
|
+
when Concurrent.on_jruby?
|
174
|
+
JavaSemaphore
|
175
|
+
else
|
176
|
+
MutexSemaphore
|
177
|
+
end
|
178
|
+
private_constant :SemaphoreImplementation
|
171
179
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
end
|
177
|
-
@semaphore.acquire(permits)
|
178
|
-
end
|
180
|
+
# @!macro semaphore
|
181
|
+
#
|
182
|
+
# @see Concurrent::MutexSemaphore
|
183
|
+
class Semaphore < SemaphoreImplementation
|
179
184
|
|
180
|
-
|
181
|
-
|
182
|
-
@semaphore.availablePermits
|
183
|
-
end
|
185
|
+
# @!method initialize(count)
|
186
|
+
# @!macro semaphore_method_initialize
|
184
187
|
|
185
|
-
|
186
|
-
|
187
|
-
@semaphore.drainPermits
|
188
|
-
end
|
188
|
+
# @!method acquire(permits = 1)
|
189
|
+
# @!macro semaphore_method_acquire
|
189
190
|
|
190
|
-
|
191
|
-
|
192
|
-
unless permits.is_a?(Fixnum) && permits > 0
|
193
|
-
fail ArgumentError, 'permits must be an integer greater than zero'
|
194
|
-
end
|
195
|
-
if timeout.nil?
|
196
|
-
@semaphore.tryAcquire(permits)
|
197
|
-
else
|
198
|
-
@semaphore.tryAcquire(permits,
|
199
|
-
timeout,
|
200
|
-
java.util.concurrent.TimeUnit::SECONDS)
|
201
|
-
end
|
202
|
-
end
|
191
|
+
# @!method available_permits
|
192
|
+
# @!macro semaphore_method_available_permits
|
203
193
|
|
204
|
-
|
205
|
-
|
206
|
-
unless permits.is_a?(Fixnum) && permits > 0
|
207
|
-
fail ArgumentError, 'permits must be an integer greater than zero'
|
208
|
-
end
|
209
|
-
@semaphore.release(permits)
|
210
|
-
true
|
211
|
-
end
|
194
|
+
# @!method drain_permits
|
195
|
+
# @!macro semaphore_method_drain_permits
|
212
196
|
|
213
|
-
|
214
|
-
|
215
|
-
unless reduction.is_a?(Fixnum) && reduction >= 0
|
216
|
-
fail ArgumentError, 'reduction must be an non-negative integer'
|
217
|
-
end
|
218
|
-
@semaphore.reducePermits(reduction)
|
219
|
-
end
|
220
|
-
end
|
197
|
+
# @!method try_acquire(permits = 1, timeout = nil)
|
198
|
+
# @!macro semaphore_method_try_acquire
|
221
199
|
|
222
|
-
# @!
|
223
|
-
|
224
|
-
end
|
200
|
+
# @!method release(permits = 1)
|
201
|
+
# @!macro semaphore_method_release
|
225
202
|
|
226
|
-
|
203
|
+
# @!method reduce_permits(reduction)
|
204
|
+
# @!macro semaphore_method_reduce_permits
|
227
205
|
|
228
|
-
# @!macro semaphore
|
229
|
-
class Semaphore < MutexSemaphore
|
230
|
-
end
|
231
206
|
end
|
232
207
|
end
|