1- require 'concurrent/ivar'
2- require 'concurrent/utility/timer'
3- require 'concurrent/executor/executor'
4- require 'concurrent/executor/safe_task_executor'
1+ require 'concurrent/configuration'
2+ require 'concurrent/executor/timer_set'
53
64module Concurrent
75
@@ -133,22 +131,15 @@ module Concurrent
133131 # #>> The task completed at 2013-11-07 12:26:09 -0500 with value 'What does the fox say?'
134132 #
135133 # @!macro monotonic_clock_warning
136- class ScheduledTask < IVar
137-
138- attr_reader :delay
134+ class ScheduledTask < TimerSet ::Task
139135
140136 # Schedule a task for execution at a specified future time.
141137 #
142138 # @yield the task to be performed
143139 #
144140 # @param [Float] delay the number of seconds to wait for before executing the task
145141 #
146- # @param [Hash] opts the options controlling how the future will be processed
147- # @option opts [Boolean] :operation (false) when `true` will execute the future on the global
148- # operation pool (for long-running operations), when `false` will execute the future on the
149- # global task pool (for short-running tasks)
150- # @option opts [object] :executor when provided will run all operations on
151- # this executor rather than the global thread pool (overrides :operation)
142+ # @!macro executor_and_deref_options
152143 #
153144 # @!macro [attach] deprecated_scheduling_by_clock_time
154145 #
@@ -158,16 +149,12 @@ class ScheduledTask < IVar
158149 # but will not be supported in the 1.0 release.
159150 def initialize ( delay , opts = { } , &block )
160151 raise ArgumentError . new ( 'no block given' ) unless block_given?
161- @delay = TimerSet . calculate_delay! ( delay )
162- super ( IVar ::NO_VALUE , opts . merge ( __task_from_block__ : block ) , &nil )
163- end
164-
165- def ns_initialize ( value , opts )
166- super
167- self . observers = CopyOnNotifyObserverSet . new
168- @state = :unscheduled
169- @task = opts [ :__task_from_block__ ]
170- @executor = Executor . executor_from_options ( opts ) || Concurrent . global_io_executor
152+ super ( Concurrent . global_timer_set , delay , [ ] , block , &nil )
153+ synchronize do
154+ ns_set_state ( :unscheduled )
155+ @__original_delay__ = delay
156+ @time = nil
157+ end
171158 end
172159
173160 # Execute an `:unscheduled` `ScheduledTask`. Immediately sets the state to `:pending`
@@ -177,51 +164,25 @@ def ns_initialize(value, opts)
177164 # @return [ScheduledTask] a reference to `self`
178165 def execute
179166 if compare_and_set_state ( :pending , :unscheduled )
180- @schedule_time = Time . now + @delay
181- Concurrent ::timer ( @delay ) { @executor . post ( &method ( :process_task ) ) }
182- self
167+ synchronize { ns_reschedule ( @__original_delay__ , false ) }
183168 end
169+ self
184170 end
185171
186172 # Create a new `ScheduledTask` object with the given block, execute it, and return the
187173 # `:pending` object.
188174 #
189175 # @param [Float] delay the number of seconds to wait for before executing the task
190176 #
191- # @param [Hash] opts the options controlling how the future will be processed
192- # @option opts [Boolean] :operation (false) when `true` will execute the future on the global
193- # operation pool (for long-running operations), when `false` will execute the future on the
194- # global task pool (for short-running tasks)
195- # @option opts [object] :executor when provided will run all operations on
196- # this executor rather than the global thread pool (overrides :operation)
177+ # @!macro executor_and_deref_options
197178 #
198179 # @return [ScheduledTask] the newly created `ScheduledTask` in the `:pending` state
199180 #
200181 # @raise [ArgumentError] if no block is given
201182 #
202183 # @!macro deprecated_scheduling_by_clock_time
203184 def self . execute ( delay , opts = { } , &block )
204- return ScheduledTask . new ( delay , opts , &block ) . execute
205- end
206-
207- # @deprecated
208- def schedule_time
209- warn '[DEPRECATED] time is now based on a monotonic clock'
210- @schedule_time
211- end
212-
213- # Has the task been cancelled?
214- #
215- # @return [Boolean] true if the task is in the given state else false
216- def cancelled?
217- state == :cancelled
218- end
219-
220- # In the task execution in progress?
221- #
222- # @return [Boolean] true if the task is in the given state else false
223- def processing?
224- state == :processing
185+ new ( delay , &block ) . execute
225186 end
226187
227188 # In the task execution in progress?
@@ -239,26 +200,17 @@ def in_progress?
239200 #
240201 # @return [Boolean] true if task execution is successfully cancelled
241202 # else false
242- def cancel
243- if_state ( :unscheduled , :pending ) do
244- @state = :cancelled
245- event . set
246- true
247- end
203+ #
204+ # @deprecated
205+ def stop
206+ warn '[DEPRECATED] use #processing? instead'
207+ cancel
248208 end
249- alias_method :stop , :cancel
250-
251- protected :set , :fail , :complete
252209
253- private
254-
255- # @!visibility private
256- def process_task
257- safe_execute ( @task ) do |success , val , reason |
258- event . set
259- time = Time . now
260- observers . notify_and_delete_observers { [ time , self . value , reason ] }
261- end
210+ # @deprecated
211+ def delay
212+ warn '[DEPRECATED] use #original_delay instead'
213+ original_delay
262214 end
263215 end
264216end
0 commit comments