1
1
require 'concurrent/synchronization'
2
2
3
3
module Concurrent
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.
4
13
class MutexSemaphore < Synchronization ::Object
14
+
5
15
# @!macro [attach] semaphore_method_initialize
6
16
#
7
17
# Create a new `Semaphore` with the initial `count`.
@@ -129,12 +139,14 @@ def reduce_permits(reduction)
129
139
130
140
protected
131
141
142
+ # @!visibility private
132
143
def ns_initialize ( count )
133
144
@free = count
134
145
end
135
146
136
147
private
137
148
149
+ # @!visibility private
138
150
def try_acquire_now ( permits )
139
151
if @free >= permits
140
152
@free -= permits
@@ -144,28 +156,45 @@ def try_acquire_now(permits)
144
156
end
145
157
end
146
158
159
+ # @!visibility private
147
160
def try_acquire_timed ( permits , timeout )
148
161
ns_wait_until ( timeout ) { try_acquire_now ( permits ) }
149
162
end
150
163
end
151
164
152
- if Concurrent . on_jruby?
165
+ SemaphoreImplementation = case
166
+ when Concurrent . on_jruby?
167
+ JavaSemaphore
168
+ else
169
+ MutexSemaphore
170
+ end
171
+ private_constant :SemaphoreImplementation
153
172
154
- # @!macro semaphore
155
- #
156
- # A counting semaphore. Conceptually, a semaphore maintains a set of
157
- # permits. Each {#acquire} blocks if necessary until a permit is
158
- # available, and then takes it. Each {#release} adds a permit, potentially
159
- # releasing a blocking acquirer.
160
- # However, no actual permit objects are used; the Semaphore just keeps a
161
- # count of the number available and acts accordingly.
162
- class Semaphore < JavaSemaphore
163
- end
173
+ # @!macro semaphore
174
+ #
175
+ # @see Concurrent::MutexSemaphore
176
+ class Semaphore < SemaphoreImplementation
164
177
165
- else
178
+ # @!method initialize(count)
179
+ # @!macro semaphore_method_initialize
180
+
181
+ # @!method acquire(permits = 1)
182
+ # @!macro semaphore_method_acquire
183
+
184
+ # @!method available_permits
185
+ # @!macro semaphore_method_available_permits
186
+
187
+ # @!method drain_permits
188
+ # @!macro semaphore_method_drain_permits
189
+
190
+ # @!method try_acquire(permits = 1, timeout = nil)
191
+ # @!macro semaphore_method_try_acquire
192
+
193
+ # @!method release(permits = 1)
194
+ # @!macro semaphore_method_release
195
+
196
+ # @!method reduce_permits(reduction)
197
+ # @!macro semaphore_method_reduce_permits
166
198
167
- # @!macro semaphore
168
- class Semaphore < MutexSemaphore
169
- end
170
199
end
171
200
end
0 commit comments