STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sequencer::ScriptRunner Class Reference

Runs one dynamic state's script on its own thread. More...

#include <ScriptRunner.hpp>

Collaboration diagram for sequencer::ScriptRunner:
[legend]

Public Types

using Clock = std::chrono::steady_clock

Public Member Functions

 ScriptRunner ()=default
 ~ScriptRunner ()
 ScriptRunner (const ScriptRunner &)=delete
ScriptRunner & operator= (const ScriptRunner &)=delete
void start (const DynamicState &ds, ScriptEnv env)
 Begin running ds.
void stop ()
 Cancel and reap.
bool isActive () const
double elapsedSeconds () const
 Seconds since start(), which is what elapsed() reports.

Private Member Functions

void run (DynamicState ds, ScriptEnv env)

Private Attributes

std::mutex mtx_
std::condition_variable cv_
std::thread thread_
std::atomic< bool > active_ {false}
std::atomic< bool > stop_ {false}
Clock::time_point started_at_ {}

Detailed Description

Runs one dynamic state's script on its own thread.

Modelled on HoldTimer, and for its reasons rather than by analogy:

  • Its own thread. It must not run on the command worker. That worker serialises every command through one queue, so a 30 s script executing there would block every TRANSITION, ACTUATOR and abort for its whole runtime, and the command timeout would start firing on ordinary operator input.
  • wait_until on a steady_clock deadline, never sleep_for. HoldTimer records why: its predecessor counted sleep calls and overshot in one direction every iteration. A loop body with a delay(0.2) compounds exactly that error, a hundred times over.
  • The program is passed by value into start(), never stored on the runner. A duration that lives behind a setter is one a reload can forget to refresh, which is how a burn once ran the previous config's window.
  • stop() is idempotent, reaps the thread, and fires no completion callback. Cancellation is not an ending the script gets to react to.

The stop flag is checked at every statement boundary, inside every wait, and at the top of every loop iteration, so a transition or an abort preempts a running script in well under a millisecond rather than at the next statement.

Because it owns a thread, the interpreter is a plain recursive tree walk — the thread IS the continuation. No resumable VM, no stepped scheduler.

Definition at line 79 of file ScriptRunner.hpp.

Member Typedef Documentation

◆ Clock

using sequencer::ScriptRunner::Clock = std::chrono::steady_clock

Definition at line 81 of file ScriptRunner.hpp.

Constructor & Destructor Documentation

◆ ScriptRunner() [1/2]

sequencer::ScriptRunner::ScriptRunner ( )
default

Referenced by operator=(), and ScriptRunner().

◆ ~ScriptRunner()

sequencer::ScriptRunner::~ScriptRunner ( )

Definition at line 61 of file ScriptRunner.cpp.

References stop().

◆ ScriptRunner() [2/2]

sequencer::ScriptRunner::ScriptRunner ( const ScriptRunner & )
delete

References ScriptRunner().

Member Function Documentation

◆ elapsedSeconds()

double sequencer::ScriptRunner::elapsedSeconds ( ) const

Seconds since start(), which is what elapsed() reports.

0 when not running.

Definition at line 65 of file ScriptRunner.cpp.

References active_, lk, mtx_, and started_at_.

◆ isActive()

bool sequencer::ScriptRunner::isActive ( ) const
inline

Definition at line 99 of file ScriptRunner.hpp.

References active_.

◆ operator=()

ScriptRunner & sequencer::ScriptRunner::operator= ( const ScriptRunner & )
delete

References ScriptRunner().

◆ run()

◆ start()

void sequencer::ScriptRunner::start ( const DynamicState & ds,
ScriptEnv env )

Begin running ds.

Supersedes anything already running (which is stopped first).

Definition at line 72 of file ScriptRunner.cpp.

References active_, lk, mtx_, sequencer::DynamicState::name, run(), started_at_, stop(), stop_, and thread_.

◆ stop()

void sequencer::ScriptRunner::stop ( )

Cancel and reap.

Safe when nothing is running, and safe to call from the transition the script itself requested — that path detaches rather than self-joining, which would be a std::terminate.

Definition at line 101 of file ScriptRunner.cpp.

References active_, cv_, lk, mtx_, stop_, and thread_.

Referenced by start(), and ~ScriptRunner().

Member Data Documentation

◆ active_

std::atomic<bool> sequencer::ScriptRunner::active_ {false}
private

Definition at line 112 of file ScriptRunner.hpp.

Referenced by elapsedSeconds(), isActive(), run(), start(), and stop().

◆ cv_

std::condition_variable sequencer::ScriptRunner::cv_
private

Definition at line 110 of file ScriptRunner.hpp.

Referenced by run(), and stop().

◆ mtx_

std::mutex sequencer::ScriptRunner::mtx_
mutableprivate

Definition at line 109 of file ScriptRunner.hpp.

Referenced by elapsedSeconds(), run(), start(), and stop().

◆ started_at_

Clock::time_point sequencer::ScriptRunner::started_at_ {}
private

Definition at line 114 of file ScriptRunner.hpp.

Referenced by elapsedSeconds(), and start().

◆ stop_

std::atomic<bool> sequencer::ScriptRunner::stop_ {false}
private

Definition at line 113 of file ScriptRunner.hpp.

Referenced by run(), start(), and stop().

◆ thread_

std::thread sequencer::ScriptRunner::thread_
private

Definition at line 111 of file ScriptRunner.hpp.

Referenced by start(), and stop().


The documentation for this class was generated from the following files: