00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #ifndef HH_SENF_Scheduler_FIFORunner_
00027 #define HH_SENF_Scheduler_FIFORunner_ 1
00028
00029
00030 #include <signal.h>
00031 #include <boost/utility.hpp>
00032 #include <senf/boost_intrusive/ilist.hpp>
00033 #include <senf/boost_intrusive/ilist_hook.hpp>
00034 #include <senf/Utils/singleton.hh>
00035 #include "EventManager.hh"
00036
00037
00038
00039
00040 namespace senf {
00041 namespace scheduler {
00042
00043 void restart();
00044
00045 namespace detail {
00046
00047 class FIFORunner
00048 : public singleton<FIFORunner>
00049 {
00050 public:
00051 struct TaskInfo;
00052
00053 private:
00054 struct TaskListTag;
00055 typedef boost::intrusive::ilist_base_hook<TaskListTag> TaskListBase;
00056 typedef boost::intrusive::ilist<TaskListBase::value_traits<TaskInfo>, false> TaskList;
00057
00058 public:
00059 class TaskInfo
00060 : public Event,
00061 public TaskListBase
00062 {
00063 public:
00064 enum Priority { PRIORITY_LOW = 0, PRIORITY_NORMAL = 1, PRIORITY_HIGH = 2 };
00065
00066 explicit TaskInfo(std::string const & name, Priority priority=PRIORITY_NORMAL);
00067 virtual ~TaskInfo();
00068
00069 void run();
00070
00071 bool runnable() const;
00072
00073 protected:
00074 void setRunnable();
00075
00076 private:
00077 virtual void v_run() = 0;
00078 virtual bool v_enabled() const;
00079
00080 bool runnable_;
00081 Priority priority_;
00082 std::string backtrace_;
00083
00084 friend class FIFORunner;
00085 };
00086
00087 typedef boost::filter_iterator<
00088 EventManager::IteratorFilter, TaskList::const_iterator> iterator;
00089
00090 using singleton<FIFORunner>::instance;
00091 using singleton<FIFORunner>::alive;
00092
00093 void enqueue(TaskInfo * task);
00094 void dequeue(TaskInfo * task);
00095
00096 void run();
00097
00098 void taskTimeout(unsigned ms);
00099 unsigned taskTimeout() const;
00100 void abortOnTimeout(bool flag);
00101 bool abortOnTimeout() const;
00102
00103 void startWatchdog();
00104 void stopWatchdog();
00105
00106 unsigned hangCount();
00107
00108 iterator begin() const;
00109 iterator end() const;
00110
00111 void yield();
00112
00113 protected:
00114
00115 private:
00116 FIFORunner();
00117 ~FIFORunner();
00118
00119 static void watchdog(int, siginfo_t *, void *);
00120 void watchdogError();
00121
00122 TaskList::iterator priorityEnd(TaskInfo::Priority p);
00123 void run(TaskList::iterator f, TaskList::iterator l);
00124
00125 struct NullTask : public TaskInfo
00126 {
00127 NullTask();
00128 ~NullTask();
00129 virtual void v_run();;
00130 virtual char const * v_type() const;
00131 virtual std::string v_info() const;
00132 };
00133
00134 TaskList tasks_;
00135 TaskList::iterator next_;
00136
00137 NullTask normalPriorityEnd_;
00138 NullTask highPriorityEnd_;
00139
00140 timer_t watchdogId_;
00141 bool watchdogRunning_;
00142 unsigned watchdogMs_;
00143 bool watchdogAbort_;
00144 std::string runningName_;
00145 # ifdef SENF_DEBUG
00146 std::string runningBacktrace_;
00147 # endif
00148 unsigned watchdogCount_;
00149 unsigned hangCount_;
00150 bool yield_;
00151
00152 friend void senf::scheduler::restart();
00153 friend class singleton<FIFORunner>;
00154 };
00155
00156 }}}
00157
00158
00159 #include "FIFORunner.cci"
00160
00161
00162 #endif
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173