Utils/Console: Fix singleton instantiation order (ServerManager / Scheduler)
[senf.git] / Scheduler / TimerDispatcher.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 TimerDispatcher public header */
25
26 #ifndef HH_TimerDispatcher_
27 #define HH_TimerDispatcher_ 1
28
29 // Custom includes
30 #include <signal.h>
31 #include <set>
32 #include <map>
33 #include "ClockService.hh"
34 #include "FdManager.hh"
35 #include "FIFORunner.hh"
36 #include "../Utils/Logger/SenfLog.hh"
37
38 //#include "TimerDispatcher.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
40
41 namespace senf {
42 namespace scheduler {
43
44     /** \brief Scheduler dispatcher managing timers
45
46         Timers are implemented using high-precision POSIX real-time timers. As such, the timer
47         granularity is given by clock_getres(CLOCK_MONOTONIC) which is 1ns on current linux kernels.
48
49         \implementation TimerDispatcher manages a single POSIX timer which is always programmed to
50             expire when the next scheduled timer needs to fire. The timer sends a signal (SIGALRM is
51             used). The handler writes data into a pipe which is has been added to the FdManager.
52       */
53     class TimerDispatcher
54         : public FdManager::Event
55     {
56         SENF_LOG_CLASS_AREA();
57
58     public:
59         ///////////////////////////////////////////////////////////////////////////
60         // Types
61
62         typedef boost::function<void ()> Callback;
63         typedef unsigned timer_id;
64
65         ///////////////////////////////////////////////////////////////////////////
66         ///\name Structors and default members
67         ///@{
68
69         TimerDispatcher(FdManager & manager, FIFORunner & runner);
70         ~TimerDispatcher();
71
72         ///@}
73         ///////////////////////////////////////////////////////////////////////////
74
75         timer_id add(std::string const & name, ClockService::clock_type timeout, 
76                      Callback const & cb);
77                                         ///< Add timer event
78                                         /**< This call adds a new timer expiring at the given point
79                                              in time.
80                                              \param[in] name descriptive name
81                                              \param[in] timeout point in time when the timer is to
82                                                  expire 
83                                              \param[in] cb callback 
84                                              \returns a \c timer_id which can be used to remove the
85                                                  timer. */
86         void remove(timer_id id);       ///< Remove timer
87
88         void unblockSignals();          ///< Unblock internal signals
89                                         /**< Must be called before waiting for an event */
90         void blockSignals();            ///< Block internal signals
91                                         /**< Must be called directly after the FdManager returns */
92         
93         bool empty() const;             ///< \c true, if no timer is registered.
94
95     protected:
96
97     private:
98         /// Internal: Timer event
99         struct TimerEvent
100             : public FIFORunner::TaskInfo
101         {
102             TimerEvent(timer_id id_, Callback const & cb_, TimerDispatcher & dispatcher_,
103                        std::string const & name);
104             virtual void run();
105
106             timer_id id;
107             Callback cb;
108             TimerDispatcher & dispatcher;
109         };
110
111         virtual void signal(int events);
112         static void sigHandler(int signal, ::siginfo_t * siginfo, void *);
113         void reschedule();
114
115         FdManager & manager_;
116         FIFORunner & runner_;
117
118         typedef std::multimap<ClockService::clock_type, TimerEvent> TimerMap;
119         typedef std::map<int, TimerMap::iterator> TimerIdMap;
120
121         TimerMap timers_;
122         TimerIdMap timerIdIndex_;
123         timer_id lastId_;
124
125         int timerPipe_[2];
126         sigset_t sigSet_;
127         bool blocked_;
128         timer_t timerId_;
129
130         static unsigned useCount_;
131     };
132
133 }}
134
135 ///////////////////////////////hh.e////////////////////////////////////////
136 #include "TimerDispatcher.cci"
137 //#include "TimerDispatcher.ct"
138 //#include "TimerDispatcher.cti"
139 #endif
140
141 \f
142 // Local Variables:
143 // mode: c++
144 // fill-column: 100
145 // comment-column: 40
146 // c-file-style: "senf"
147 // indent-tabs-mode: nil
148 // ispell-local-dictionary: "american"
149 // compile-command: "scons -u test"
150 // End: