Skip to content

Commit 2847116

Browse files
committed
optimized some other class
1 parent b5074e9 commit 2847116

File tree

6 files changed

+68
-32
lines changed

6 files changed

+68
-32
lines changed

lib/concurrent/delay.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,17 @@ def initialize(opts = {}, &block)
7171
#
7272
# @see Concurrent::Dereferenceable
7373
def value
74-
mutex.synchronize do
74+
mutex.lock
75+
execute_task_once
76+
result = apply_deref_options(@value)
77+
mutex.unlock
78+
79+
result
80+
end
81+
82+
private
83+
84+
def execute_task_once
7585
if @state == :pending
7686
begin
7787
@value = @task.call
@@ -81,8 +91,6 @@ def value
8191
@state = :rejected
8292
end
8393
end
84-
return apply_deref_options(@value)
8594
end
86-
end
8795
end
8896
end

lib/concurrent/dereferenceable.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ module Dereferenceable
2626
#
2727
# @return [Object] the current value of the object
2828
def value
29-
mutex.synchronize{ apply_deref_options(@value) }
29+
mutex.lock
30+
result = apply_deref_options(@value)
31+
mutex.unlock
32+
result
3033
end
34+
3135
alias_method :deref, :value
3236

3337
protected
@@ -36,7 +40,10 @@ def value
3640
#
3741
# @param [Object] val the new value
3842
def value=(val)
39-
mutex.synchronize{ @value = val }
43+
mutex.lock
44+
result = @value = val
45+
mutex.unlock
46+
result
4047
end
4148

4249
# A mutex lock used for synchronizing thread-safe operations. Methods defined
@@ -71,12 +78,13 @@ def init_mutex
7178
# @option opts [String] :copy_on_deref (nil) call the given `Proc` passing the internal value and
7279
# returning the value returned from the proc
7380
def set_deref_options(opts = {})
74-
mutex.synchronize do
75-
@dup_on_deref = opts[:dup_on_deref] || opts[:dup]
76-
@freeze_on_deref = opts[:freeze_on_deref] || opts[:freeze]
77-
@copy_on_deref = opts[:copy_on_deref] || opts[:copy]
78-
@do_nothing_on_deref = ! (@dup_on_deref || @freeze_on_deref || @copy_on_deref)
79-
end
81+
mutex.lock
82+
@dup_on_deref = opts[:dup_on_deref] || opts[:dup]
83+
@freeze_on_deref = opts[:freeze_on_deref] || opts[:freeze]
84+
@copy_on_deref = opts[:copy_on_deref] || opts[:copy]
85+
@do_nothing_on_deref = !(@dup_on_deref || @freeze_on_deref || @copy_on_deref)
86+
mutex.unlock
87+
nil
8088
end
8189

8290
# @!visibility private

lib/concurrent/obligation.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ def value(timeout = nil)
4848
end
4949

5050
def state
51-
mutex.synchronize { @state }
51+
mutex.lock
52+
result = @state
53+
mutex.unlock
54+
result
5255
end
5356

5457
def reason
55-
mutex.synchronize { @reason }
58+
mutex.lock
59+
result = @reason
60+
mutex.unlock
61+
result
5662
end
5763

5864
protected

lib/concurrent/promise.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class Promise
3030
# @see http://wiki.commonjs.org/wiki/Promises/A
3131
# @see http://promises-aplus.github.io/promises-spec/
3232
def initialize(opts = {}, &block)
33-
opts.delete_if {|k, v| v.nil?}
33+
opts.delete_if { |k, v| v.nil? }
3434

3535
@executor = OptionsParser::get_executor_from(opts)
3636
@parent = opts.fetch(:parent) { nil }
37-
@on_fulfill = opts.fetch(:on_fulfill) { Proc.new{ |result| result } }
38-
@on_reject = opts.fetch(:on_reject) { Proc.new{ |reason| raise reason } }
37+
@on_fulfill = opts.fetch(:on_fulfill) { Proc.new { |result| result } }
38+
@on_reject = opts.fetch(:on_reject) { Proc.new { |reason| raise reason } }
3939

40-
@promise_body = block || Proc.new{|result| result }
40+
@promise_body = block || Proc.new { |result| result }
4141
@state = :unscheduled
4242
@children = []
4343

@@ -46,13 +46,13 @@ def initialize(opts = {}, &block)
4646

4747
# @return [Promise]
4848
def self.fulfill(value, opts = {})
49-
Promise.new(opts).tap{ |p| p.send(:synchronized_set_state!, true, value, nil) }
49+
Promise.new(opts).tap { |p| p.send(:synchronized_set_state!, true, value, nil) }
5050
end
5151

5252

5353
# @return [Promise]
5454
def self.reject(reason, opts = {})
55-
Promise.new(opts).tap{ |p| p.send(:synchronized_set_state!, false, nil, reason) }
55+
Promise.new(opts).tap { |p| p.send(:synchronized_set_state!, false, nil, reason) }
5656
end
5757

5858
# @return [Promise]
@@ -77,7 +77,7 @@ def self.execute(opts = {}, &block)
7777
# @return [Promise] the new promise
7878
def then(rescuer = nil, &block)
7979
raise ArgumentError.new('rescuers and block are both missing') if rescuer.nil? && !block_given?
80-
block = Proc.new{ |result| result } if block.nil?
80+
block = Proc.new { |result| result } if block.nil?
8181
child = Promise.new(
8282
parent: self,
8383
executor: @executor,
@@ -105,6 +105,7 @@ def on_success(&block)
105105
def rescue(&block)
106106
self.then(block)
107107
end
108+
108109
alias_method :catch, :rescue
109110
alias_method :on_error, :rescue
110111

@@ -124,13 +125,13 @@ def root? # :nodoc:
124125

125126
# @!visibility private
126127
def on_fulfill(result)
127-
realize Proc.new{ @on_fulfill.call(result) }
128+
realize Proc.new { @on_fulfill.call(result) }
128129
nil
129130
end
130131

131132
# @!visibility private
132133
def on_reject(reason)
133-
realize Proc.new{ @on_reject.call(reason) }
134+
realize Proc.new { @on_reject.call(reason) }
134135
nil
135136
end
136137

@@ -142,14 +143,14 @@ def notify_child(child)
142143
# @!visibility private
143144
def realize(task)
144145
@executor.post do
145-
success, value, reason = SafeTaskExecutor.new( task ).execute
146+
success, value, reason = SafeTaskExecutor.new(task).execute
146147

147148
children_to_notify = mutex.synchronize do
148149
set_state!(success, value, reason)
149150
@children.dup
150151
end
151152

152-
children_to_notify.each{ |child| notify_child(child) }
153+
children_to_notify.each { |child| notify_child(child) }
153154
end
154155
end
155156

@@ -159,9 +160,9 @@ def set_state!(success, value, reason)
159160
end
160161

161162
def synchronized_set_state!(success, value, reason)
162-
mutex.synchronize do
163-
set_state!(success, value, reason)
164-
end
163+
mutex.lock
164+
set_state!(success, value, reason)
165+
mutex.unlock
165166
end
166167
end
167168
end

lib/concurrent/timer_task.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ def self.execute(opts = {}, &task)
247247
# @return [Fixnum] Number of seconds after the task completes before the
248248
# task is performed again.
249249
def execution_interval
250-
mutex.synchronize{ @execution_interval }
250+
mutex.lock
251+
result = @execution_interval
252+
mutex.unlock
253+
result
251254
end
252255

253256
# @!attribute [rw] execution_interval
@@ -257,15 +260,21 @@ def execution_interval=(value)
257260
if (value = value.to_f) <= 0.0
258261
raise ArgumentError.new('must be greater than zero')
259262
else
260-
mutex.synchronize{ @execution_interval = value }
263+
mutex.lock
264+
result = @execution_interval = value
265+
mutex.unlock
266+
result
261267
end
262268
end
263269

264270
# @!attribute [rw] timeout_interval
265271
# @return [Fixnum] Number of seconds the task can run before it is
266272
# considered to have failed.
267273
def timeout_interval
268-
mutex.synchronize{ @timeout_interval }
274+
mutex.lock
275+
result = @timeout_interval
276+
mutex.unlock
277+
result
269278
end
270279

271280
# @!attribute [rw] timeout_interval
@@ -275,7 +284,10 @@ def timeout_interval=(value)
275284
if (value = value.to_f) <= 0.0
276285
raise ArgumentError.new('must be greater than zero')
277286
else
278-
mutex.synchronize{ @timeout_interval = value }
287+
mutex.lock
288+
result = @timeout_interval = value
289+
mutex.unlock
290+
result
279291
end
280292
end
281293

spec/concurrent/dereferenceable_shared.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@
121121
subject = dereferenceable_subject(0)
122122
mutex = double('mutex')
123123
subject.stub(:mutex).and_return(mutex)
124-
mutex.should_receive(:synchronize).at_least(:once)
124+
mutex.should_receive(:lock).at_least(:once)
125+
mutex.should_receive(:unlock).at_least(:once)
125126
subject.value
126127
end
127128

0 commit comments

Comments
 (0)