class Timer
Handle returned from #timer. Wraps the underlying
Async::Task and exposes a thread-safe #shutdown matching
the Concurrent::TimerTask interface that callers expect.
Timers running on the dedicated reactor are cancelled through
the executor's inbox; timers running on the caller's reactor
are cancelled directly.
Definitions
def initialize
Initialize an empty timer handle.
Implementation
def initialize
@inbox = nil
@mutex = ::Thread::Mutex.new
@task = nil
end
def task=(task)
Set the underlying task. Called by the executor thread once the timer has been scheduled.
Implementation
def task=(task)
@mutex.synchronize{@task = task}
end
def shutdown
Cancel the timer. Idempotent; safe to call from any thread or fiber.
Implementation
def shutdown
task = nil
@mutex.synchronize do
task = @task
@task = nil
end
return unless task
if inbox = @inbox
begin
inbox.push(proc{task.stop})
rescue ::ClosedQueueError
# Executor already shut down; the timer task was
# stopped along with its parent reactor.
end
else
task.stop
end
end