Class: Vizcore::Renderer::FrameScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/renderer/frame_scheduler.rb

Overview

Fixed-interval scheduler used by the frame broadcast loop.

Constant Summary collapse

DEFAULT_FRAME_RATE =

Default frame rate used by renderer loops.

60.0

Instance Method Summary collapse

Constructor Details

#initialize(frame_rate: DEFAULT_FRAME_RATE, monotonic_clock: nil, sleeper: nil, error_handler: nil) {|elapsed| ... } ⇒ FrameScheduler

Returns a new instance of FrameScheduler.

Parameters:

  • frame_rate (Float) (defaults to: DEFAULT_FRAME_RATE)
  • monotonic_clock (#call, nil) (defaults to: nil)
  • sleeper (#call, nil) (defaults to: nil)
  • error_handler (#call, nil) (defaults to: nil)

Yield Parameters:

  • elapsed (Float)

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vizcore/renderer/frame_scheduler.rb', line 15

def initialize(frame_rate: DEFAULT_FRAME_RATE, monotonic_clock: nil, sleeper: nil, error_handler: nil, &on_tick)
  @frame_rate = Float(frame_rate)
  raise ArgumentError, "frame_rate must be positive" unless @frame_rate.positive?

  @frame_interval = 1.0 / @frame_rate
  @monotonic_clock = monotonic_clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
  @sleeper = sleeper || ->(seconds) { sleep(seconds) }
  @error_handler = error_handler || ->(error) { raise error }
  @on_tick = on_tick
  @running = false
  @thread = nil
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/vizcore/renderer/frame_scheduler.rb', line 52

def running?
  @running
end

#startvoid

This method returns an undefined value.



29
30
31
32
33
34
35
# File 'lib/vizcore/renderer/frame_scheduler.rb', line 29

def start
  return if running?

  @running = true
  started_at = @monotonic_clock.call
  @thread = Thread.new { run_loop(started_at) }
end

#stop(timeout: 1.0) ⇒ void

This method returns an undefined value.

Parameters:

  • timeout (Float) (defaults to: 1.0)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vizcore/renderer/frame_scheduler.rb', line 39

def stop(timeout: 1.0)
  return unless running?

  @running = false
  thread = @thread
  @thread = nil
  return unless thread
  return if thread == Thread.current

  thread.join(timeout)
end