AERA
time_job.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #ifndef time_job_h
86 #define time_job_h
87 
88 #include "../r_code/utils.h"
89 #include "init.h"
90 #include "group.h"
91 #include "pgm_overlay.h"
92 
93 
94 namespace r_exec {
95 
96 class r_exec_dll TimeJob :
97  public _Object {
98 protected:
99  TimeJob(Timestamp target_time);
100 public:
101  Timestamp target_time_; // absolute deadline; 0 means ASAP.
102  virtual bool update(Timestamp &next_target) = 0; // next_target: absolute deadline; 0 means no more waiting; return false to shutdown the time core.
103  virtual bool is_alive() const;
104  virtual void report(std::chrono::microseconds lag) const;
105  uint32 get_job_id() const { return job_id_; }
106 
110  class Compare {
111  public:
112  bool
113  operator()
114  (const P<TimeJob>& x, const P<TimeJob>& y) const
115  {
116  return x->target_time_ < y->target_time_;
117  }
118  };
119 
120 private:
121  static uint32 job_count_;
122  int job_id_;
123 };
124 
125 class r_exec_dll UpdateJob :
126  public TimeJob {
127 public:
128  P<Group> group_;
129  UpdateJob(Group *g, Timestamp ijt);
130  bool update(Timestamp &next_target) override;
131  void report(int64 lag) const;
132 };
133 
134 class r_exec_dll SignalingJob :
135  public TimeJob {
136 protected:
137  SignalingJob(View *v, Timestamp ijt);
138 public:
139  P<View> view_;
140  bool is_alive() const override;
141 };
142 
143 class r_exec_dll AntiPGMSignalingJob :
144  public SignalingJob {
145 public:
146  AntiPGMSignalingJob(View *v, Timestamp ijt);
147  bool update(Timestamp &next_target) override;
148  void report(int64 lag) const;
149 };
150 
151 class r_exec_dll InputLessPGMSignalingJob :
152  public SignalingJob {
153 public:
154  InputLessPGMSignalingJob(View *v, Timestamp ijt);
155  bool update(Timestamp &next_target) override;
156  void report(int64 lag) const;
157 };
158 
162 class r_exec_dll InjectionJob :
163  public TimeJob {
164 public:
165  P<View> view_;
173  InjectionJob(View *v, Timestamp target_time, bool is_from_io_device);
174  bool update(Timestamp &next_target) override;
175  void report(int64 lag) const;
176 
177  bool is_from_io_device_;
178 };
179 
180 class r_exec_dll EInjectionJob :
181  public TimeJob {
182 public:
183  P<View> view_;
184  EInjectionJob(View *v, Timestamp ijt);
185  bool update(Timestamp &next_target) override;
186  void report(int64 lag) const;
187 };
188 
189 class r_exec_dll SaliencyPropagationJob :
190  public TimeJob {
191 public:
192  P<r_code::Code> object_;
193  float32 sln_change_;
194  float32 source_sln_thr_;
195  SaliencyPropagationJob(r_code::Code *o, float32 sln_change, float32 source_sln_thr, Timestamp ijt);
196  bool update(Timestamp &next_target) override;
197  void report(int64 lag) const;
198 };
199 
200 class r_exec_dll ShutdownTimeCore :
201  public TimeJob {
202 public:
204  bool update(Timestamp &next_target) override;
205 };
206 
207 template<class M> class MonitoringJob :
208  public TimeJob {
209 public:
210  P<M> monitor_;
211  MonitoringJob(M *monitor, Timestamp deadline) : TimeJob(deadline), monitor_(monitor) {
212 #ifdef WITH_DETAIL_OID
213  OUTPUT_LINE((TraceLevel)0, " make MonitoringJob::TimeJob " << get_job_id() <<
214  "(" << get_detail_oid() << ") for monitor(" << monitor_->get_detail_oid() << "), deadline " <<
215  r_code::Utils::RelativeTime(deadline));
216 #endif
217  }
218  bool update(Timestamp &next_target) override {
219 
220 #ifdef WITH_DETAIL_OID
221  OUTPUT_LINE((TraceLevel)0, r_code::Utils::RelativeTime(r_exec::Now()) << " MonitoringJob::TimeJob " << get_job_id() <<
222  ": monitor(" << monitor_->get_detail_oid() << ")->update()");
223 #endif
224  monitor_->update(next_target);
225  return true;
226  }
227  bool is_alive() const override {
228 
229  return monitor_->is_alive();
230  }
231  void report(std::chrono::microseconds lag) const override {
232 
233  std::cout << "> late monitoring: " << lag.count() << " us behind." << std::endl;
234  }
235 };
236 
237 class r_exec_dll PerfSamplingJob :
238  public TimeJob {
239 public:
240  std::chrono::microseconds period_;
241  PerfSamplingJob(Timestamp start, std::chrono::microseconds period);
242  bool is_alive() const override;
243  bool update(Timestamp &next_target) override;
244 };
245 }
246 
247 
248 #endif
r_exec::TimeJob
Definition: time_job.h:97
r_exec::PerfSamplingJob
Definition: time_job.h:238
r_exec::UpdateJob
Definition: time_job.h:126
r_exec::MonitoringJob
Definition: time_job.h:208
core::P
Definition: base.h:103
r_exec::InjectionJob
Definition: time_job.h:163
r_code::Code
Definition: r_code/object.h:224
r_exec::SaliencyPropagationJob
Definition: time_job.h:190
r_exec::AntiPGMSignalingJob
Definition: time_job.h:144
r_exec::ShutdownTimeCore
Definition: time_job.h:201
r_exec::EInjectionJob
Definition: time_job.h:181
core::_Object
Definition: base.h:131
r_exec::View
Definition: view.h:102
r_exec::Group
Definition: group.h:108
r_exec::InputLessPGMSignalingJob
Definition: time_job.h:152
r_exec::TimeJob::Compare
Definition: time_job.h:110
r_exec::SignalingJob
Definition: time_job.h:135