3265fc1d062be8effd2b69746e2a9ebcf9ddb6a4
[senf.git] / senf / Scheduler / FIFORunner.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief FIFORunner public header */
30
31 #ifndef HH_SENF_Scheduler_FIFORunner_
32 #define HH_SENF_Scheduler_FIFORunner_ 1
33
34 // Custom includes
35 #include <signal.h>
36 #include <boost/utility.hpp>
37 #include <senf/boost_intrusive/ilist.hpp>
38 #include <senf/boost_intrusive/ilist_hook.hpp>
39 #include <senf/Utils/singleton.hh>
40 #include "EventManager.hh"
41
42 //#include "FIFORunner.mpp"
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 namespace senf {
46 namespace scheduler {
47
48     void restart();
49
50 namespace detail {
51
52     class FIFORunner
53         : public singleton<FIFORunner>
54     {
55     public:
56         struct TaskInfo;
57
58     private:
59         struct TaskListTag;
60         typedef boost::intrusive::ilist_base_hook<TaskListTag> TaskListBase;
61         typedef boost::intrusive::ilist<TaskListBase::value_traits<TaskInfo>, false> TaskList;
62
63     public:
64         class TaskInfo
65             : public Event,
66               public TaskListBase
67         {
68         public:
69             enum Priority { PRIORITY_LOW = 0, PRIORITY_NORMAL = 1, PRIORITY_HIGH = 2 };
70
71             explicit TaskInfo(std::string const & name, Priority priority=PRIORITY_NORMAL);
72             virtual ~TaskInfo();
73
74             void run();
75
76             bool runnable() const;
77
78         protected:
79             void setRunnable();
80
81         private:
82             virtual void v_run() = 0;
83             virtual bool v_enabled() const;
84
85             bool runnable_;
86             Priority priority_;
87             std::string backtrace_; // only used if SENF_BACKTRACE is defined.
88
89             friend class FIFORunner;
90         };
91
92         typedef boost::filter_iterator<
93             EventManager::IteratorFilter, TaskList::const_iterator> iterator;
94
95         using singleton<FIFORunner>::instance;
96         using singleton<FIFORunner>::alive;
97
98         void enqueue(TaskInfo * task);
99         void dequeue(TaskInfo * task);
100
101         void run();
102
103         void taskTimeout(unsigned ms);
104         unsigned taskTimeout() const;
105         void abortOnTimeout(bool flag);
106         bool abortOnTimeout() const;
107
108         void startWatchdog();
109         void stopWatchdog();
110
111         unsigned hangCount();
112
113         iterator begin() const;
114         iterator end() const;
115
116         void yield();
117
118     protected:
119
120     private:
121         FIFORunner();
122         ~FIFORunner();
123
124         static void watchdog(int, siginfo_t *, void *);
125         void watchdogError();
126
127         TaskList::iterator priorityEnd(TaskInfo::Priority p);
128         void run(TaskList::iterator f, TaskList::iterator l);
129
130         struct NullTask : public TaskInfo
131         {
132             NullTask();
133             ~NullTask();
134             virtual void v_run();;
135             virtual char const * v_type() const;
136             virtual std::string v_info() const;
137         };
138
139         TaskList tasks_;
140         TaskList::iterator next_;
141
142         NullTask normalPriorityEnd_;
143         NullTask highPriorityEnd_;
144
145         timer_t watchdogId_;
146         bool watchdogRunning_;
147         unsigned watchdogMs_;
148         bool watchdogAbort_;
149         std::string runningName_;
150 #   ifdef SENF_DEBUG
151         std::string runningBacktrace_;
152 #   endif
153         unsigned watchdogCount_;
154         unsigned hangCount_;
155         bool yield_;
156
157         friend void senf::scheduler::restart();
158         friend class singleton<FIFORunner>;
159     };
160
161 }}}
162
163 //-/////////////////////////////////////////////////////////////////////////////////////////////////
164 #include "FIFORunner.cci"
165 //#include "FIFORunner.ct"
166 //#include "FIFORunner.cti"
167 #endif
168
169 \f
170 // Local Variables:
171 // mode: c++
172 // fill-column: 100
173 // comment-column: 40
174 // c-file-style: "senf"
175 // indent-tabs-mode: nil
176 // ispell-local-dictionary: "american"
177 // compile-command: "scons -u test"
178 // End: