1a90ed5ba892e5d54b32e21b785b08c6f6ea2284
[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
37 //#include "TimerDispatcher.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41 namespace scheduler {
42
43     /** \brief
44       */
45     class TimerDispatcher
46         : public FdManager::Event
47     {
48     public:
49         ///////////////////////////////////////////////////////////////////////////
50         // Types
51
52         typedef boost::function<void ()> Callback;
53         typedef unsigned timer_id;
54
55         ///////////////////////////////////////////////////////////////////////////
56         ///\name Structors and default members
57         ///@{
58
59         TimerDispatcher(FdManager & manager, FIFORunner & runner);
60         ~TimerDispatcher();
61
62         ///@}
63         ///////////////////////////////////////////////////////////////////////////
64
65         timer_id add(ClockService::clock_type timeout, Callback const & cb);
66         void remove(timer_id id);
67
68         void blockSignals();
69         void unblockSignals();
70
71     protected:
72
73     private:
74         struct TimerEvent
75             : public FIFORunner::TaskInfo
76         {
77             TimerEvent(timer_id id_, Callback const & cb_, TimerDispatcher & dispatcher_);
78             virtual void run();
79
80             timer_id id;
81             Callback cb;
82             TimerDispatcher & dispatcher;
83         };
84
85         virtual void signal(int events);
86         static void sigHandler(int signal, ::siginfo_t * siginfo, void *);
87         void reschedule();
88
89         FdManager & manager_;
90         FIFORunner & runner_;
91
92         typedef std::multimap<ClockService::clock_type, TimerEvent> TimerMap;
93         typedef std::map<int, TimerMap::iterator> TimerIdMap;
94
95         TimerMap timers_;
96         TimerIdMap timerIdIndex_;
97         timer_id lastId_;
98
99         int timerPipe_[2];
100         sigset_t sigSet_;
101         bool blocked_;
102         timer_t timerId_;
103
104         static unsigned useCount_;
105     };
106
107 }}
108
109 ///////////////////////////////hh.e////////////////////////////////////////
110 #include "TimerDispatcher.cci"
111 //#include "TimerDispatcher.ct"
112 //#include "TimerDispatcher.cti"
113 #endif
114
115 \f
116 // Local Variables:
117 // mode: c++
118 // fill-column: 100
119 // comment-column: 40
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // End: