Skip to content

Commit 30c2098

Browse files
committed
IVar subclasses use ns_initialize.
1 parent a17c89e commit 30c2098

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

lib/concurrent/future.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ class Future < IVar
2424
# @raise [ArgumentError] if no block is given
2525
def initialize(opts = {}, &block)
2626
raise ArgumentError.new('no block given') unless block_given?
27-
super(IVar::NO_VALUE, opts, &nil)
28-
@state = :unscheduled
29-
@task = block
30-
@executor = Executor.executor_from_options(opts) || Concurrent.global_io_executor
31-
@args = get_arguments_from(opts)
27+
super(IVar::NO_VALUE, opts.merge(__task_from_block__: block), &nil)
3228
end
3329

3430
# Execute an `:unscheduled` `Future`. Immediately sets the state to `:pending` and
@@ -77,7 +73,7 @@ def self.execute(opts = {}, &block)
7773
# @!macro ivar_set_method
7874
def set(value = IVar::NO_VALUE, &block)
7975
check_for_block_or_value!(block_given?, value)
80-
mutex.synchronize do
76+
synchronize do
8177
if @state != :unscheduled
8278
raise MultipleAssignmentError
8379
else
@@ -87,6 +83,16 @@ def set(value = IVar::NO_VALUE, &block)
8783
execute
8884
end
8985

86+
protected
87+
88+
def ns_initialize(value, opts)
89+
super
90+
@state = :unscheduled
91+
@task = opts[:__task_from_block__]
92+
@executor = Executor.executor_from_options(opts) || Concurrent.global_io_executor
93+
@args = get_arguments_from(opts)
94+
end
95+
9096
private
9197

9298
# @!visibility private

lib/concurrent/promise.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,7 @@ class Promise < IVar
203203
# @see http://promises-aplus.github.io/promises-spec/
204204
def initialize(opts = {}, &block)
205205
opts.delete_if { |k, v| v.nil? }
206-
super(IVar::NO_VALUE, opts, &nil)
207-
208-
@executor = Executor.executor_from_options(opts) || Concurrent.global_io_executor
209-
@args = get_arguments_from(opts)
210-
211-
@parent = opts.fetch(:parent) { nil }
212-
@on_fulfill = opts.fetch(:on_fulfill) { Proc.new { |result| result } }
213-
@on_reject = opts.fetch(:on_reject) { Proc.new { |reason| raise reason } }
214-
215-
@promise_body = block || Proc.new { |result| result }
216-
@state = :unscheduled
217-
@children = []
206+
super(IVar::NO_VALUE, opts.merge(__promise_body_from_block__: block), &nil)
218207
end
219208

220209
# Create a new `Promise` and fulfill it immediately.
@@ -266,7 +255,7 @@ def execute
266255
def set(value = IVar::NO_VALUE, &block)
267256
raise PromiseExecutionError.new('supported only on root promise') unless root?
268257
check_for_block_or_value!(block_given?, value)
269-
mutex.synchronize do
258+
synchronize do
270259
if @state != :unscheduled
271260
raise MultipleAssignmentError
272261
else
@@ -319,7 +308,7 @@ def then(rescuer = nil, &block)
319308
on_reject: rescuer
320309
)
321310

322-
mutex.synchronize do
311+
synchronize do
323312
child.state = :pending if @state == :pending
324313
child.on_fulfill(apply_deref_options(@value)) if @state == :fulfilled
325314
child.on_reject(@reason) if @state == :rejected
@@ -447,6 +436,21 @@ def self.any?(*promises)
447436

448437
protected
449438

439+
def ns_initialize(value, opts)
440+
super
441+
442+
@executor = Executor.executor_from_options(opts) || Concurrent.global_io_executor
443+
@args = get_arguments_from(opts)
444+
445+
@parent = opts.fetch(:parent) { nil }
446+
@on_fulfill = opts.fetch(:on_fulfill) { Proc.new { |result| result } }
447+
@on_reject = opts.fetch(:on_reject) { Proc.new { |reason| raise reason } }
448+
449+
@promise_body = opts[:__promise_body_from_block__] || Proc.new { |result| result }
450+
@state = :unscheduled
451+
@children = []
452+
end
453+
450454
# Aggregate a collection of zero or more promises under a composite promise,
451455
# execute the aggregated promises and collect them into a standard Ruby array,
452456
# call the given Ruby `Ennnumerable` predicate (such as `any?`, `all?`, `none?`,
@@ -472,7 +476,7 @@ def self.aggregate(method, *promises)
472476

473477
# @!visibility private
474478
def set_pending
475-
mutex.synchronize do
479+
synchronize do
476480
@state = :pending
477481
@children.each { |c| c.set_pending }
478482
end
@@ -503,7 +507,7 @@ def notify_child(child)
503507

504508
# @!visibility private
505509
def complete(success, value, reason)
506-
children_to_notify = mutex.synchronize do
510+
children_to_notify = synchronize do
507511
set_state!(success, value, reason)
508512
@children.dup
509513
end
@@ -528,7 +532,7 @@ def set_state!(success, value, reason)
528532

529533
# @!visibility private
530534
def synchronized_set_state!(success, value, reason)
531-
mutex.synchronize { set_state!(success, value, reason) }
535+
synchronize { set_state!(success, value, reason) }
532536
end
533537
end
534538
end

lib/concurrent/scheduled_task.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,14 @@ class ScheduledTask < IVar
158158
def initialize(delay, opts = {}, &block)
159159
raise ArgumentError.new('no block given') unless block_given?
160160
@delay = TimerSet.calculate_delay!(delay)
161+
super(IVar::NO_VALUE, opts.merge(__task_from_block__: block), &nil)
162+
end
161163

162-
super(IVar::NO_VALUE, opts, &nil)
163-
164+
def ns_initialize(value, opts)
165+
super
164166
self.observers = CopyOnNotifyObserverSet.new
165167
@state = :unscheduled
166-
@task = block
168+
@task = opts[:__task_from_block__]
167169
@executor = Executor.executor_from_options(opts) || Concurrent.global_io_executor
168170
end
169171

0 commit comments

Comments
 (0)