Skip to content

Commit e906491

Browse files
committed
Delay is now Synchronization::Object but is slower.
1 parent f65bc44 commit e906491

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

lib/concurrent/delay.rb

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'concurrent/obligation'
44
require 'concurrent/executor/executor_options'
55
require 'concurrent/executor/immediate_executor'
6+
require 'concurrent/synchronization'
67

78
module Concurrent
89

@@ -37,7 +38,7 @@ module Concurrent
3738
# execute on the given executor, allowing the call to timeout.
3839
#
3940
# @see Concurrent::Dereferenceable
40-
class Delay
41+
class Delay < Synchronization::Object
4142
include Obligation
4243
include ExecutorOptions
4344

@@ -59,7 +60,8 @@ class Delay
5960
def initialize(opts = {}, &block)
6061
raise ArgumentError.new('no block given') unless block_given?
6162

62-
init_obligation
63+
super(&nil)
64+
init_obligation(self)
6365
set_deref_options(opts)
6466
@task_executor = get_executor_from(opts)
6567

@@ -145,16 +147,15 @@ def wait(timeout = nil)
145147
# @yield the delayed operation to perform
146148
# @return [true, false] if success
147149
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
155158
end
156-
ensure
157-
mutex.unlock
158159
end
159160

160161
private
@@ -163,10 +164,11 @@ def reconfigure(&block)
163164
def execute_task_once # :nodoc:
164165
# this function has been optimized for performance and
165166
# 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
170172

171173
if execute
172174
@task_executor.post do
@@ -176,10 +178,10 @@ def execute_task_once # :nodoc:
176178
rescue => ex
177179
reason = ex
178180
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
183185
end
184186
end
185187
end

0 commit comments

Comments
 (0)