2c04bc65ff4163225b7f72aa09f8874400a61679
[senf.git] / 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_FIFORunner_
27 #define HH_FIFORunner_ 1
28
29 // Custom includes
30 #include <signal.h>
31 #include <boost/utility.hpp>
32 #include "../boost/intrusive/ilist.hpp"
33 #include "../boost/intrusive/ilist_hook.hpp"
34 #include "../Utils/singleton.hh"
35
36 //#include "FIFORunner.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf { 
40
41     class Scheduler;
42
43 namespace scheduler {
44
45     /** \brief Task execution scheduler
46
47         The FIFORunner implements a simple FIFO scheduler for callback tasks. All tasks are held in
48         a queue. Whenever a task is run, it is moved to the end of the queue. Running the queue will
49         run all tasks which have been marked runnable. 
50
51         When running a task, it's runnable flag is always reset. The flag is set whenever an event
52         is posted for the task.
53       */
54     class FIFORunner
55         : public singleton<FIFORunner>
56     {
57     public:
58         struct TaskInfo;
59
60     private:
61         struct TaskListTag;
62         typedef boost::intrusive::ilist_base_hook<TaskListTag> TaskListBase;
63         typedef boost::intrusive::ilist<TaskListBase::value_traits<TaskInfo>, false> TaskList;
64
65     public:
66         ///////////////////////////////////////////////////////////////////////////
67         // Types
68
69         /** \brief Task structure
70
71             TaskInfo is the base-class for all tasks.
72          */
73         struct TaskInfo 
74             : public TaskListBase
75         {
76             explicit TaskInfo(std::string const & name_);
77             virtual ~TaskInfo();
78
79             bool runnable;              ///< Runnable flag
80                                         /**< This must be set to \c true when the task is
81                                              runnable. It is reset automatically when the task is
82                                              run. */
83
84             std::string name;           ///< Descriptive task name
85 #       ifdef SENF_DEBUG
86             std::string backtrace;
87 #       endif
88             virtual void run() = 0;     ///< Called to run the task
89         };
90
91         ///////////////////////////////////////////////////////////////////////////
92         ///\name Structors and default members
93         ///@{
94
95         using singleton<FIFORunner>::instance;
96         using singleton<FIFORunner>::alive;
97
98         ///@}
99         ///////////////////////////////////////////////////////////////////////////
100
101         void enqueue(TaskInfo * task);  ///< Add task to queue
102         void dequeue(TaskInfo * task);  ///< Remove task from queue
103         
104         void run();                     ///< Run queue
105
106         void taskTimeout(unsigned ms);  ///< Set task timeout to \a ms milliseconds
107         unsigned taskTimeout() const;   ///< Get task timeout in milliseconds
108
109         unsigned hangCount() const;     ///< Number of task expirations
110                                         /**< The FIFORunner manages a watchdog which checks, that a
111                                              single task does not run continuously for a longer time
112                                              or block. If a task runs for more than 1s, a warning is
113                                              printed  and the hangCount is increased. */
114
115     protected:
116
117     private:
118         FIFORunner();
119         ~FIFORunner();
120
121         static void watchdog(int, siginfo_t *, void *);
122
123         TaskList tasks_;
124         TaskList::iterator next_;
125         timer_t watchdogId_;
126         unsigned watchdogMs_;
127         std::string runningName_;
128 #   ifdef SENF_DEBUG
129         std::string runningBacktrace_;
130 #   endif
131         unsigned watchdogCount_;
132         unsigned hangCount_;
133
134         friend class singleton<FIFORunner>;
135         friend class senf::Scheduler;
136     };
137
138
139 }}
140
141 ///////////////////////////////hh.e////////////////////////////////////////
142 #include "FIFORunner.cci"
143 //#include "FIFORunner.ct"
144 //#include "FIFORunner.cti"
145 #endif
146
147 \f
148 // Local Variables:
149 // mode: c++
150 // fill-column: 100
151 // comment-column: 40
152 // c-file-style: "senf"
153 // indent-tabs-mode: nil
154 // ispell-local-dictionary: "american"
155 // compile-command: "scons -u test"
156 // End: