3
3
require 'concurrent/obligation'
4
4
require 'concurrent/executor/executor_options'
5
5
require 'concurrent/executor/immediate_executor'
6
+ require 'concurrent/synchronization'
6
7
7
8
module Concurrent
8
9
@@ -37,7 +38,7 @@ module Concurrent
37
38
# execute on the given executor, allowing the call to timeout.
38
39
#
39
40
# @see Concurrent::Dereferenceable
40
- class Delay
41
+ class Delay < Synchronization :: Object
41
42
include Obligation
42
43
include ExecutorOptions
43
44
@@ -59,7 +60,8 @@ class Delay
59
60
def initialize ( opts = { } , &block )
60
61
raise ArgumentError . new ( 'no block given' ) unless block_given?
61
62
62
- init_obligation
63
+ super ( &nil )
64
+ init_obligation ( self )
63
65
set_deref_options ( opts )
64
66
@task_executor = get_executor_from ( opts )
65
67
@@ -145,16 +147,15 @@ def wait(timeout = nil)
145
147
# @yield the delayed operation to perform
146
148
# @return [true, false] if success
147
149
def reconfigure ( &block )
148
- mutex . lock
149
- raise ArgumentError . new ( 'no block given' ) unless block_given?
150
- unless @computing
151
- @task = block
152
- true
153
- else
154
- false
150
+ mutex . synchronize do
151
+ raise ArgumentError . new ( 'no block given' ) unless block_given?
152
+ unless @computing
153
+ @task = block
154
+ true
155
+ else
156
+ false
157
+ end
155
158
end
156
- ensure
157
- mutex . unlock
158
159
end
159
160
160
161
private
@@ -163,10 +164,11 @@ def reconfigure(&block)
163
164
def execute_task_once # :nodoc:
164
165
# this function has been optimized for performance and
165
166
# should not be modified without running new benchmarks
166
- mutex . lock
167
- execute = @computing = true unless @computing
168
- task = @task
169
- mutex . unlock
167
+ execute = task = nil
168
+ mutex . synchronize do
169
+ execute = @computing = true unless @computing
170
+ task = @task
171
+ end
170
172
171
173
if execute
172
174
@task_executor . post do
@@ -176,10 +178,10 @@ def execute_task_once # :nodoc:
176
178
rescue => ex
177
179
reason = ex
178
180
end
179
- mutex . lock
180
- set_state ( success , result , reason )
181
- event . set
182
- mutex . unlock
181
+ mutex . synchronize do
182
+ set_state ( success , result , reason )
183
+ event . set
184
+ end
183
185
end
184
186
end
185
187
end
0 commit comments