@@ -22,10 +22,10 @@ class TimerSet
22
22
# @option opts [object] :executor when provided will run all operations on
23
23
# this executor rather than the global thread pool (overrides :operation)
24
24
def initialize ( opts = { } )
25
- @queue = PriorityQueue . new ( order : :min )
26
- @task_executor = OptionsParser ::get_executor_from ( opts ) || Concurrent . configuration . global_task_pool
25
+ @queue = PriorityQueue . new ( order : :min )
26
+ @task_executor = OptionsParser ::get_executor_from ( opts ) || Concurrent . configuration . global_task_pool
27
27
@timer_executor = SingleThreadExecutor . new
28
- @condition = Condition . new
28
+ @condition = Condition . new
29
29
init_executor
30
30
end
31
31
@@ -64,7 +64,7 @@ def post(intended_time, *args, &task)
64
64
# For a timer, #kill is like an orderly shutdown, except we need to manually
65
65
# (and destructively) clear the queue first
66
66
def kill
67
- @queue . clear
67
+ mutex . synchronize { @queue . clear }
68
68
shutdown
69
69
end
70
70
@@ -124,14 +124,13 @@ def shutdown_execution
124
124
# @!visibility private
125
125
def process_tasks
126
126
loop do
127
- break if @queue . empty?
128
-
129
- task = @queue . peek
127
+ task = mutex . synchronize { @queue . peek }
128
+ break unless task
130
129
interval = task . time - Time . now . to_f
131
130
132
131
if interval <= 0
133
132
@task_executor . post ( *task . args , &task . op )
134
- @queue . pop
133
+ mutex . synchronize { @queue . pop }
135
134
else
136
135
mutex . synchronize do
137
136
@condition . wait ( mutex , [ interval , 60 ] . min )
0 commit comments