fix: missing include added
[senf.git] / senf / Scheduler / FIFORunner.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief FIFORunner public header */
25
26 #ifndef HH_SENF_Scheduler_FIFORunner_
27 #define HH_SENF_Scheduler_FIFORunner_ 1
28
29 // Custom includes
30 #include <signal.h>
31 #include <senf/config.hh>
32 #include <boost/utility.hpp>
33 #include <senf/boost_intrusive/ilist.hpp>
34 #include <senf/boost_intrusive/ilist_hook.hpp>
35 #include <senf/Utils/singleton.hh>
36 #include "EventManager.hh"
37
38 //#include "FIFORunner.mpp"
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 namespace senf {
42 namespace scheduler {
43
44     void restart();
45
46 namespace detail {
47
48     class FIFORunner
49         : public singleton<FIFORunner>
50     {
51     public:
52         struct TaskInfo;
53
54     private:
55         struct TaskListTag;
56         typedef boost::intrusive::ilist_base_hook<TaskListTag> TaskListBase;
57         typedef boost::intrusive::ilist<TaskListBase::value_traits<TaskInfo>, false> TaskList;
58
59     public:
60         class TaskInfo
61             : public Event,
62               public TaskListBase
63         {
64         public:
65             enum Priority { PRIORITY_LOW = 0, PRIORITY_NORMAL = 1, PRIORITY_HIGH = 2 };
66
67             explicit TaskInfo(std::string const & name, Priority priority=PRIORITY_NORMAL);
68             virtual ~TaskInfo();
69
70             void run();
71
72             bool runnable() const;
73
74         protected:
75             void setRunnable();
76
77         private:
78             virtual void v_run() = 0;
79             virtual bool v_enabled() const;
80
81             bool runnable_;
82             Priority priority_;
83 #       ifdef SENF_BACKTRACE
84             std::string backtrace_;
85 #       endif
86
87             friend class FIFORunner;
88         };
89
90         typedef boost::filter_iterator<
91             EventManager::IteratorFilter, TaskList::const_iterator> iterator;
92
93         using singleton<FIFORunner>::instance;
94         using singleton<FIFORunner>::alive;
95
96         void enqueue(TaskInfo * task);
97         void dequeue(TaskInfo * task);
98
99         void run();
100
101         void taskTimeout(unsigned ms);
102         unsigned taskTimeout() const;
103         void abortOnTimeout(bool flag);
104         bool abortOnTimeout() const;
105
106         void startWatchdog();
107         void stopWatchdog();
108
109         unsigned hangCount();
110
111         iterator begin() const;
112         iterator end() const;
113
114         void yield();
115
116     protected:
117
118     private:
119         FIFORunner();
120         ~FIFORunner();
121
122         static void watchdog(int, siginfo_t *, void *);
123         void watchdogError();
124
125         TaskList::iterator priorityEnd(TaskInfo::Priority p);
126         void run(TaskList::iterator f, TaskList::iterator l);
127
128         struct NullTask : public TaskInfo
129         {
130             NullTask();
131             ~NullTask();
132             virtual void v_run();;
133             virtual char const * v_type() const;
134             virtual std::string v_info() const;
135         };
136
137         TaskList tasks_;
138         TaskList::iterator next_;
139
140         NullTask normalPriorityEnd_;
141         NullTask highPriorityEnd_;
142
143         timer_t watchdogId_;
144         bool watchdogRunning_;
145         unsigned watchdogMs_;
146         bool watchdogAbort_;
147         std::string runningName_;
148 #   ifdef SENF_DEBUG
149         std::string runningBacktrace_;
150 #   endif
151         unsigned watchdogCount_;
152         unsigned hangCount_;
153         bool yield_;
154
155         friend void senf::scheduler::restart();
156         friend class singleton<FIFORunner>;
157     };
158
159 }}}
160
161 //-/////////////////////////////////////////////////////////////////////////////////////////////////
162 #include "FIFORunner.cci"
163 //#include "FIFORunner.ct"
164 //#include "FIFORunner.cti"
165 #endif
166
167 \f
168 // Local Variables:
169 // mode: c++
170 // fill-column: 100
171 // comment-column: 40
172 // c-file-style: "senf"
173 // indent-tabs-mode: nil
174 // ispell-local-dictionary: "american"
175 // compile-command: "scons -u test"
176 // End: