Class: Plushie::TimerScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/plushie/timer_scheduler.rb

Overview

Single-thread timer scheduler for timer subscriptions.

Replaces the per-timer Thread.new pattern with one thread that manages all timers via IO.select with a deadline-based timeout. Adding or cancelling a timer wakes the scheduler through a pipe so it can recalculate the next deadline immediately.

Examples:

scheduler = TimerScheduler.new
scheduler.schedule(tag: "tick", interval_ms: 1000, event_queue: queue)
scheduler.cancel("tick")
scheduler.stop

Instance Method Summary collapse

Constructor Details

#initializeTimerScheduler

Returns a new instance of TimerScheduler.



19
20
21
22
23
24
25
26
# File 'lib/plushie/timer_scheduler.rb', line 19

def initialize
  @pipe_r, @pipe_w = IO.pipe
  @timers = {}
  @mutex = Mutex.new
  @stop_mutex = Mutex.new
  @thread = Thread.new { run_loop }
  @thread.name = "plushie-timer-scheduler"
end

Instance Method Details

#cancel(tag) ⇒ Object



40
41
42
43
# File 'lib/plushie/timer_scheduler.rb', line 40

def cancel(tag)
  @mutex.synchronize { @timers.delete(tag) }
  wake
end

#schedule(tag:, interval_ms:, event_queue:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/plushie/timer_scheduler.rb', line 28

def schedule(tag:, interval_ms:, event_queue:)
  interval = interval_ms / 1000.0
  @mutex.synchronize do
    @timers[tag] = {
      interval: interval,
      deadline: Process.clock_gettime(Process::CLOCK_MONOTONIC) + interval,
      queue: event_queue
    }
  end
  wake
end

#stopObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/plushie/timer_scheduler.rb', line 45

def stop
  thread = nil
  @stop_mutex.synchronize do
    thread = @thread
    @thread = nil
    @pipe_w&.close unless @pipe_w&.closed?
    @pipe_r&.close unless @pipe_r&.closed?
  end
  return unless thread
  return if thread == Thread.current

  thread.kill
  thread.join(0.5)
end