JobScheduler  1.1.0
CMJob.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <atomic>
11 #include <chrono>
12 #include <functional>
13 #include <memory>
14 #include <vector>
15 
16 namespace CMJob {
17 
30 enum class JobType {
31  Cyclic,
32  Trigger
33 };
34 
42 enum class JobState { New, Active, Ready, Idle, Suspended, Timeout, Disabled };
43 
52 enum class CallbackHook {
53  Manual = -1,
54  PreIn,
55  In,
56  DrivMan,
57  Traffic,
58  VehicleControl,
59  Brake,
60  Calc,
61  Out,
62  User_1 = 32,
63  User_2,
64  User_3,
65  User_4,
66  User_5
67 };
74 class AbstractJob {
75  public:
82  AbstractJob(const std::string& name, JobType type, bool sync);
83  ~AbstractJob();
84 
85  /* Configuration */
86  /* Can only be set, when the job is in state JobState::New */
91  void setCycleTime(unsigned long cycle);
96  void setCycleOffset(unsigned long cycle);
101  void setTimeoutTime(double time_s);
106  void setExecutionCounter(int count);
111  void skipFirstCycles(unsigned count);
116  void setCallbackHook(CallbackHook hook);
117 
118  /* State machine */
119 
130  virtual void init();
131 
142  virtual void activate();
143 
156  virtual void prepare();
157 
167  virtual void execute() = 0;
168 
178  virtual void suspend();
179 
186  virtual void timeout();
187 
197  virtual void disable();
198 
208  virtual void reset();
209 
210  /* Job functions */
211 
217  void info() const;
218 
225  bool isJobScheduled(unsigned long cycle) const;
226 
232  bool isCallbackScheduled(unsigned long cycle) const;
233 
238  bool isJobTimeout(void) const;
239 
240  /* Getter */
241  std::string getJobName() const;
242  unsigned long getCycleTime() const;
243  unsigned long getCycleOffset() const;
244  bool isSynchronized() const;
245  unsigned long getCycle() const;
246 
247  JobType getJobType() const;
248  std::string getJobTypeName() const;
249 
250  CallbackHook getCallbackHook() const;
251  std::string getCallbackHookName() const;
252 
253  JobState getJobState() const;
254  std::string getJobStateName() const;
255 
256  /* Setter */
257  void setCycle(unsigned long cycle);
258 
265  void setJobName(const std::string& name);
266 
267  protected:
268  /* State setters */
269  void setReady();
270  void setIdle();
271  void setActive();
272  void setSuspended();
273  void setTimeout();
274  void setDisabled();
275 
276  private:
277  struct Impl;
278  std::unique_ptr<Impl> impl_;
279 };
280 
286  public:
291  static JobScheduler& instance() {
292  static JobScheduler instance_;
293  return instance_;
294  }
295 
301  void setCycleStep(unsigned short cycle_step);
302 
310  void lock(unsigned long cycle, const std::function<void(void)>& fp);
311 
317  void addJob(const std::shared_ptr<AbstractJob>& job);
318 
324  bool areJobsReady(unsigned long cycle) const;
325 
330  bool areJobsTimeout(void) const;
331 
338  void executeJobs(CallbackHook hook, unsigned long cycle);
339 
345  void updateJobs(unsigned long cycle_current);
346 
350  void resetJobs(void);
351 
355  void disableJobs(void);
356 
362  std::shared_ptr<CMJob::AbstractJob> getJob(const std::string& name) const;
363 
367  void deleteJobs(void);
368 
373  void deleteJob(const std::string& name);
374 
378  void printJobs(void) const;
379 
380  private:
381  JobScheduler() = default;
382  JobScheduler(const JobScheduler&);
383  JobScheduler& operator=(const JobScheduler&);
384 
386  std::vector<std::shared_ptr<AbstractJob>> Jobs_;
387 
388  unsigned short cycle_step_ = 1;
389 };
390 
398 class Log {
399  public:
400  /* set CM Log */
401  static void setLog(void (*pLog)(const char* format, ...));
402  static void setLogWarn(void (*pLogWarn)(unsigned ECId, const char* format,
403  ...));
404  static void setLogErr(void (*pLogErr)(unsigned ECId, const char* format,
405  ...));
406 
407  /* Use these, if you want to print output depending on the registered output
408  * stream */
409  static void printLog(const std::string& out);
410  static void printWarn(unsigned ECId, const std::string& out);
411  static void printError(unsigned ECId, const std::string& out);
412 
413  private:
414  static void (*cm_log)(const char* format, ...);
415  static void (*cm_log_warn_f)(unsigned ECId, const char* format,
416  ...);
417  static void (*cm_log_err_f)(unsigned ECId, const char* format,
418  ...);
419 };
420 
421 } // namespace CMJob
called periodically at the end of the job
The Log class.
Definition: CMJob.h:398
static JobScheduler & instance()
instance singleton
Definition: CMJob.h:291
Definition: CMJob.h:16
JobState
Definition: CMJob.h:42
JobType
Definition: CMJob.h:30
The AbstractJob class provides an interface for jobs.
Definition: CMJob.h:74
CallbackHook
Definition: CMJob.h:52
called immediately
The singleton JobScheduler class handles AbstractJobs.
Definition: CMJob.h:285