2bd919dbb072772d6ff2a5f78ff47c68fe3e911a
[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 <boost/utility.hpp>
32 #include <senf/boost_intrusive/ilist.hpp>
33 #include <senf/boost_intrusive/ilist_hook.hpp>
34 #include <senf/Utils/singleton.hh>
35 #include "EventManager.hh"
36
37 //#include "FIFORunner.mpp"
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 namespace senf {
41 namespace scheduler {
42
43     void restart();
44
45 namespace detail {
46
47     class FIFORunner
48         : public singleton<FIFORunner>
49     {
50     public:
51         struct TaskInfo;
52
53     private:
54         struct TaskListTag;
55         typedef boost::intrusive::ilist_base_hook<TaskListTag> TaskListBase;
56         typedef boost::intrusive::ilist<TaskListBase::value_traits<TaskInfo>, false> TaskList;
57
58     public:
59         class TaskInfo
60             : public Event,
61               public TaskListBase
62         {
63         public:
64             enum Priority { PRIORITY_LOW = 0, PRIORITY_NORMAL = 1, PRIORITY_HIGH = 2 };
65
66             explicit TaskInfo(std::string const & name, Priority priority=PRIORITY_NORMAL);
67             virtual ~TaskInfo();
68
69             void run();
70
71             bool runnable() const;
72
73         protected:
74             void setRunnable();
75
76         private:
77             virtual void v_run() = 0;
78             virtual bool v_enabled() const;
79
80             bool runnable_;
81             Priority priority_;
82             std::string backtrace_; // only used if SENF_BACKTRACE is defined.
83
84             friend class FIFORunner;
85         };
86
87         typedef boost::filter_iterator<
88             EventManager::IteratorFilter, TaskList::const_iterator> iterator;
89
90         using singleton<FIFORunner>::instance;
91         using singleton<FIFORunner>::alive;
92
93         void enqueue(TaskInfo * task);
94         void dequeue(TaskInfo * task);
95
96         void run();
97
98         void taskTimeout(unsigned ms);
99         unsigned taskTimeout() const;
100         void abortOnTimeout(bool flag);
101         bool abortOnTimeout() const;
102
103         void startWatchdog();
104         void stopWatchdog();
105
106         unsigned hangCount();
107
108         iterator begin() const;
109         iterator end() const;
110
111         void yield();
112
113     protected:
114
115     private:
116         FIFORunner();
117         ~FIFORunner();
118
119         static void watchdog(int, siginfo_t *, void *);
120         void watchdogError();
121
122         TaskList::iterator priorityEnd(TaskInfo::Priority p);
123         void run(TaskList::iterator f, TaskList::iterator l);
124
125         struct NullTask : public TaskInfo
126         {
127             NullTask();
128             ~NullTask();
129             virtual void v_run();;
130             virtual char const * v_type() const;
131             virtual std::string v_info() const;
132         };
133
134         TaskList tasks_;
135         TaskList::iterator next_;
136
137         NullTask normalPriorityEnd_;
138         NullTask highPriorityEnd_;
139
140         timer_t watchdogId_;
141         bool watchdogRunning_;
142         unsigned watchdogMs_;
143         bool watchdogAbort_;
144         std::string runningName_;
145 #   ifdef SENF_DEBUG
146         std::string runningBacktrace_;
147 #   endif
148         unsigned watchdogCount_;
149         unsigned hangCount_;
150         bool yield_;
151
152         friend void senf::scheduler::restart();
153         friend class singleton<FIFORunner>;
154     };
155
156 }}}
157
158 //-/////////////////////////////////////////////////////////////////////////////////////////////////
159 #include "FIFORunner.cci"
160 //#include "FIFORunner.ct"
161 //#include "FIFORunner.cti"
162 #endif
163
164 \f
165 // Local Variables:
166 // mode: c++
167 // fill-column: 100
168 // comment-column: 40
169 // c-file-style: "senf"
170 // indent-tabs-mode: nil
171 // ispell-local-dictionary: "american"
172 // compile-command: "scons -u test"
173 // End: