FIFORunner.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Scheduler_FIFORunner_
18 #define HH_SENF_Scheduler_FIFORunner_ 1
19 
20 // Custom includes
21 #include <signal.h>
22 #include <boost/function.hpp>
23 #include <boost/intrusive/list.hpp>
24 #include <boost/intrusive/list_hook.hpp>
25 #include <senf/Utils/singleton.hh>
26 #include "EventManager.hh"
27 
28 //#include "FIFORunner.mpp"
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 namespace senf {
32 namespace scheduler {
33 
34  void restart();
35 
36 namespace detail {
37 
38  class FIFORunner
39  : public singleton<FIFORunner>
40  {
41  public:
42  class TaskInfo;
43 
44  typedef boost::function<void(std::string const &, std::string const &, std::string const &, unsigned, unsigned)> WatchdogCallback;
45 
46  private:
47  struct TaskListTag;
48  typedef boost::intrusive::list_base_hook< boost::intrusive::tag<TaskListTag> > TaskListBase;
49  typedef boost::intrusive::list< TaskInfo,
50  boost::intrusive::constant_time_size<false>,
51  boost::intrusive::base_hook<TaskListBase> > TaskList;
52 
53  public:
54  class TaskInfo
55  : public Event,
56  public TaskListBase
57  {
58  public:
60 
61  explicit TaskInfo(std::string const & name, Priority priority=PRIORITY_NORMAL);
62  virtual ~TaskInfo();
63 
64  void run();
65 
66  bool runnable() const;
67 
68  protected:
69  void setRunnable();
70 
71  private:
72  virtual void v_run() = 0;
73  virtual bool v_enabled() const;
74 
75  bool runnable_;
76  Priority priority_;
77  std::string backtrace_; // only used if SENF_BACKTRACE is defined.
78 
79  friend class FIFORunner;
80  };
81 
82  typedef boost::filter_iterator<
83  EventManager::IteratorFilter, TaskList::const_iterator> iterator;
84 
87 
88  void enqueue(TaskInfo * task);
89  void dequeue(TaskInfo * task);
90 
91  void run();
92 
93  void taskTimeout(unsigned ms);
94  unsigned taskTimeout() const;
95  void abortOnTimeout(bool flag);
96  bool abortOnTimeout() const;
97 
98  void watchdogCallback(WatchdogCallback const & cb);
99  void startWatchdog();
100  void stopWatchdog();
101 
102  void watchdogCheckpoint(const char * checkpoint);
103 
104  unsigned hangCount();
105 
106  iterator begin() const;
107  iterator end() const;
108 
109  void yield();
110 
111  protected:
112 
113  private:
114  FIFORunner();
115  ~FIFORunner();
116 
117  static void watchdog(int, siginfo_t *, void *);
118  void watchdogError();
119 
120  TaskList::iterator priorityEnd(TaskInfo::Priority p);
121  void run(TaskList::iterator f, TaskList::iterator l);
122 
123  struct NullTask : public TaskInfo
124  {
125  NullTask();
126  ~NullTask();
127  virtual void v_run();;
128  virtual char const * v_type() const;
129  virtual std::string v_info() const;
130  };
131 
132  TaskList tasks_;
133  TaskList::iterator next_;
134 
135  NullTask queueEnd_;
136  NullTask normalPriorityEnd_;
137  NullTask highPriorityEnd_;
138 
139  timer_t watchdogId_;
140  bool watchdogRunning_;
141  unsigned watchdogMs_;
142  bool watchdogAbort_;
143  const char * watchdogCheckpoint_;
144 
145  TaskInfo * runningTask_;
146  std::string runningName_;
147  std::string runningBacktrace_;
148 
149  WatchdogCallback watchdogCallback_;
150  unsigned watchdogCount_;
151  unsigned hangCount_;
152  bool yield_;
153 
154  friend void senf::scheduler::restart();
155  friend class singleton<FIFORunner>;
156  };
157 
158 }}}
159 
160 //-/////////////////////////////////////////////////////////////////////////////////////////////////
161 #include "FIFORunner.cci"
162 //#include "FIFORunner.ct"
163 //#include "FIFORunner.cti"
164 #endif
165 
166 
167 // Local Variables:
168 // mode: c++
169 // fill-column: 100
170 // comment-column: 40
171 // c-file-style: "senf"
172 // indent-tabs-mode: nil
173 // ispell-local-dictionary: "american"
174 // compile-command: "scons -u test"
175 // End:
boost::function< void(std::string const &, std::string const &, std::string const &, unsigned, unsigned)> WatchdogCallback
Definition: FIFORunner.hh:42
EventManager public header.
boost::filter_iterator< EventManager::IteratorFilter, TaskList::const_iterator > iterator
Definition: FIFORunner.hh:83
std::string const & name() const
Get event name.
TaskInfo(std::string const &name, Priority priority=PRIORITY_NORMAL)
void restart()
Restart scheduler.
Definition: Scheduler.cc:96
void watchdogCallback(WatchdogCallback const &cb)
void watchdogCheckpoint(const char *checkpoint)