liftof_lib/
thread_control.rs

1//! Thread control structures
2
3use std::collections::HashMap;
4use std::fmt;
5
6#[cfg(feature = "database")]
7use tof_dataclasses::database::ReadoutBoard;
8
9use tof_dataclasses::status::TofDetectorStatus;
10use tof_dataclasses::calibrations::RBCalibrations;
11
12use crate::settings::LiftofSettings;
13
14/// Send runtime information 
15/// to threads via shared memory
16/// (Arc(Mutex)
17#[derive(Default, Debug)]
18pub struct ThreadControl {
19  /// Stop ALL threads
20  pub stop_flag                  : bool,
21  /// Received INT signal        
22  pub sigint_recvd               : bool,
23  /// signal to end all rb thread activity
24  pub end_all_rb_threads         : bool,
25  /// Trigger calibration thread
26  pub calibration_active         : bool,
27  /// Keep track on how many calibration 
28  /// packets we have received
29  pub finished_calibrations      : HashMap<u8,bool>,
30  /// Hold the actual calibration data
31  pub calibrations               : HashMap<u8, RBCalibrations>,
32  /// Hold off the master trigger thread, until everything else
33  /// is ready
34  pub holdoff_mtb_thread         : bool,
35  /// alive indicator for cmd dispatch thread
36  pub thread_cmd_dispatch_active : bool,
37  /// alive indicator for data sink thread
38  pub thread_data_sink_active    : bool,
39  /// alive indicator for runner thread
40  pub thread_runner_active       : bool,
41  /// alive indicator for event builder thread
42  pub thread_event_bldr_active   : bool,
43  /// alive indicator for master trigger thread
44  pub thread_master_trg_active   : bool,
45  /// alive indicator for monitoring thread
46  pub thread_monitoring_active   : bool,
47  /// Running readoutboard communicator threads - the key is associated rb id
48  pub thread_rbcomm_active       : HashMap<u8, bool>,
49  /// Manage CTRL+C (or CMD+C/Mac) input
50  pub thread_signal_hdlr_active  : bool,
51  /// The current run id
52  pub run_id                     : u32,
53  /// The number of boards available
54  pub n_rbs                      : u32,
55  #[cfg(feature = "database")]
56  /// The active readoutboards in the Tof
57  pub rb_list                    : Vec<ReadoutBoard>,
58  /// Verification run currently active
59  pub verification_active        : bool,
60  /// TOF Detector status - which channels are active?
61  pub detector_status            : TofDetectorStatus,
62  /// Decide if data is actually written to disk
63  pub write_data_to_disk         : bool,
64  /// indicator that a new 
65  /// run has started
66  /// (data sinks need to know)
67  pub new_run_start_flag         : bool,
68  /// initiate a MTB DAQ reset (if the queue is behind)
69  pub reset_mtb_daq              : bool,
70  pub liftof_settings            : LiftofSettings,
71}
72
73impl ThreadControl {
74  pub fn new() -> Self {
75    Self {
76      stop_flag                  : false,
77      calibration_active         : false,
78      finished_calibrations      : HashMap::<u8,bool>::new(),
79      calibrations               : HashMap::<u8, RBCalibrations>::new(),
80      sigint_recvd               : false,
81      end_all_rb_threads         : false,
82      holdoff_mtb_thread         : false,
83      thread_cmd_dispatch_active : false,
84      thread_data_sink_active    : false,
85      thread_runner_active       : false,
86      thread_event_bldr_active   : false,
87      thread_master_trg_active   : false,
88      thread_monitoring_active   : false,
89      thread_rbcomm_active       : HashMap::<u8,bool>::new(),
90      // in principle this should always be active
91      thread_signal_hdlr_active  : true,
92      run_id                     : 0,
93      n_rbs                      : 0,
94      #[cfg(feature = "database")]
95      rb_list                    : Vec::<ReadoutBoard>::new(),
96      verification_active        : false,
97      detector_status            : TofDetectorStatus::new(),
98      write_data_to_disk         : false,
99      new_run_start_flag         : false,
100      reset_mtb_daq              : false,
101      liftof_settings            : LiftofSettings::new(),
102    }
103  }
104}
105
106impl fmt::Display for ThreadControl {
107  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108    let mut repr = String::from("<ThreadControl:");
109    repr        += &(format!("\n  Run ID         : {}", self.run_id));
110    repr        += &(format!("\n  N RBs          : {}", self.n_rbs));
111    repr        += &(format!("\n  wr to disk     : {}", self.write_data_to_disk));
112    repr        += "\n    -- reported RB calibration activity:";
113    repr        += &(format!("\n  RB cali active : {}", self.calibration_active));
114    for k in self.finished_calibrations.keys() {
115      repr        += &(format!("\n  -- finished  {}  : {}", k, self.finished_calibrations.get(k).unwrap()));       
116    }
117    repr        += &(format!("\n    -- verification run: {}", self.verification_active));
118    repr        += "\n    -- program status:";
119    repr        += &(format!("\n  stop flag        : {}", self.stop_flag));
120    repr        += "\n    -- reported thread activity:";
121    repr        += &(format!("\n  holdoff mtb thr. : {}", self.holdoff_mtb_thread));
122    repr        += &(format!("\n  cmd dispatcher   : {}", self.thread_cmd_dispatch_active));
123    repr        += &(format!("\n  runner           : {}", self.thread_runner_active));
124    repr        += &(format!("\n  data sink        : {}", self.thread_data_sink_active));
125    repr        += &(format!("\n  monitoring       : {}", self.thread_monitoring_active));
126    repr        += &(format!("\n  evt builder      : {}", self.thread_event_bldr_active));
127    repr        += &(format!("\n  master_trigger   : {}", self.thread_master_trg_active));
128    if self.thread_rbcomm_active.len() > 0 {
129      repr        += "\n -- active RB threads";
130      for k in self.thread_rbcomm_active.keys() {
131        repr      += &(format!("\n -- -- {} : {}", k, self.thread_rbcomm_active.get(k).unwrap()));
132      }
133    }
134    repr        += &(format!("\n  master trig    : {}>", self.thread_master_trg_active));
135    write!(f, "{}", repr)
136  }
137}
138