Scheduler: Remove obsolete 'Scheduler' class
[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 namespace scheduler {
41
42     void restart();
43
44 namespace detail {
45
46     class FIFORunner
47         : public singleton<FIFORunner>
48     {
49     public:
50         struct TaskInfo;
51
52     private:
53         struct TaskListTag;
54         typedef boost::intrusive::ilist_base_hook<TaskListTag> TaskListBase;
55         typedef boost::intrusive::ilist<TaskListBase::value_traits<TaskInfo>, false> TaskList;
56
57     public:
58         class TaskInfo 
59             : public TaskListBase
60         {
61         public:
62             explicit TaskInfo(std::string const & name);
63             virtual ~TaskInfo();
64
65         protected:
66             void setRunnable();
67             
68         private:
69             virtual void run() = 0;
70
71             bool runnable_;
72             std::string name_;
73 #       ifdef SENF_DEBUG
74             std::string backtrace_;
75 #       endif
76
77             friend class FIFORunner;
78         };
79         using singleton<FIFORunner>::instance;
80         using singleton<FIFORunner>::alive;
81
82         void enqueue(TaskInfo * task);
83         void dequeue(TaskInfo * task);
84         
85         void run();
86
87         void taskTimeout(unsigned ms);
88         unsigned taskTimeout() const;
89
90         unsigned hangCount() const;     ///< Number of task expirations
91                                         /**< The FIFORunner manages a watchdog which checks, that a
92                                              single task does not run continuously for a longer time
93                                              or block. If a task runs for more than 1s, a warning is
94                                              printed  and the hangCount is increased. */
95
96     protected:
97
98     private:
99         FIFORunner();
100         ~FIFORunner();
101
102         static void watchdog(int, siginfo_t *, void *);
103
104         TaskList tasks_;
105         TaskList::iterator next_;
106         timer_t watchdogId_;
107         unsigned watchdogMs_;
108         std::string runningName_;
109 #   ifdef SENF_DEBUG
110         std::string runningBacktrace_;
111 #   endif
112         unsigned watchdogCount_;
113         unsigned hangCount_;
114
115         friend void senf::scheduler::restart();
116         friend class singleton<FIFORunner>;
117     };
118
119 }}}
120
121 ///////////////////////////////hh.e////////////////////////////////////////
122 #include "FIFORunner.cci"
123 //#include "FIFORunner.ct"
124 //#include "FIFORunner.cti"
125 #endif
126
127 \f
128 // Local Variables:
129 // mode: c++
130 // fill-column: 100
131 // comment-column: 40
132 // c-file-style: "senf"
133 // indent-tabs-mode: nil
134 // ispell-local-dictionary: "american"
135 // compile-command: "scons -u test"
136 // End: