Skip to content

Commit ccc62cd

Browse files
committed
wip: Semaphore is now a Synchronization::Object.
1 parent ef88134 commit ccc62cd

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

lib/concurrent/atomic/semaphore.rb

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
require 'concurrent/atomic/condition'
1+
require 'concurrent/synchronization'
22

33
module Concurrent
4-
class MutexSemaphore
4+
class MutexSemaphore < Synchronization::Object
55
# @!macro [attach] semaphore_method_initialize
66
#
77
# Create a new `Semaphore` with the initial `count`.
@@ -13,9 +13,7 @@ def initialize(count)
1313
unless count.is_a?(Fixnum) && count >= 0
1414
fail ArgumentError, 'count must be an non-negative integer'
1515
end
16-
@mutex = Mutex.new
17-
@condition = Condition.new
18-
@free = count
16+
super(count)
1917
end
2018

2119
# @!macro [attach] semaphore_method_acquire
@@ -33,7 +31,7 @@ def acquire(permits = 1)
3331
unless permits.is_a?(Fixnum) && permits > 0
3432
fail ArgumentError, 'permits must be an integer greater than zero'
3533
end
36-
@mutex.synchronize do
34+
synchronize do
3735
try_acquire_timed(permits, nil)
3836
nil
3937
end
@@ -45,7 +43,7 @@ def acquire(permits = 1)
4543
#
4644
# @return [Integer]
4745
def available_permits
48-
@mutex.synchronize { @free }
46+
synchronize { @free }
4947
end
5048

5149
# @!macro [attach] semaphore_method_drain_permits
@@ -54,7 +52,7 @@ def available_permits
5452
#
5553
# @return [Integer]
5654
def drain_permits
57-
@mutex.synchronize do
55+
synchronize do
5856
@free.tap { |_| @free = 0 }
5957
end
6058
end
@@ -79,7 +77,7 @@ def try_acquire(permits = 1, timeout = nil)
7977
unless permits.is_a?(Fixnum) && permits > 0
8078
fail ArgumentError, 'permits must be an integer greater than zero'
8179
end
82-
@mutex.synchronize do
80+
synchronize do
8381
if timeout.nil?
8482
try_acquire_now(permits)
8583
else
@@ -101,9 +99,9 @@ def release(permits = 1)
10199
unless permits.is_a?(Fixnum) && permits > 0
102100
fail ArgumentError, 'permits must be an integer greater than zero'
103101
end
104-
@mutex.synchronize do
102+
synchronize do
105103
@free += permits
106-
permits.times { @condition.signal }
104+
permits.times { ns_signal }
107105
end
108106
nil
109107
end
@@ -125,10 +123,16 @@ def reduce_permits(reduction)
125123
unless reduction.is_a?(Fixnum) && reduction >= 0
126124
fail ArgumentError, 'reduction must be an non-negative integer'
127125
end
128-
@mutex.synchronize { @free -= reduction }
126+
synchronize { @free -= reduction }
129127
nil
130128
end
131129

130+
protected
131+
132+
def ns_initialize(count)
133+
@free = count
134+
end
135+
132136
private
133137

134138
def try_acquire_now(permits)
@@ -141,12 +145,13 @@ def try_acquire_now(permits)
141145
end
142146

143147
def try_acquire_timed(permits, timeout)
144-
remaining = Condition::Result.new(timeout)
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
148+
ns_wait_until(timeout) { try_acquire_now(permits) }
149+
#remaining = Condition::Result.new(timeout)
150+
#while !try_acquire_now(permits) && remaining.can_wait?
151+
#@condition.signal
152+
#remaining = @condition.wait(@mutex, remaining.remaining_time)
153+
#end
154+
#remaining.can_wait? ? true : false
150155
end
151156
end
152157

0 commit comments

Comments
 (0)