4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief TimerDispatcher public header */
26 #ifndef HH_TimerDispatcher_
27 #define HH_TimerDispatcher_ 1
33 #include "ClockService.hh"
34 #include "FdManager.hh"
35 #include "FIFORunner.hh"
36 #include "../Utils/Logger/SenfLog.hh"
38 //#include "TimerDispatcher.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
44 /** \brief Scheduler dispatcher managing timers
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.
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.
54 : public FdManager::Event
56 SENF_LOG_CLASS_AREA();
59 ///////////////////////////////////////////////////////////////////////////
62 typedef boost::function<void ()> Callback;
63 typedef unsigned timer_id;
65 ///////////////////////////////////////////////////////////////////////////
66 ///\name Structors and default members
69 TimerDispatcher(FdManager & manager, FIFORunner & runner);
73 ///////////////////////////////////////////////////////////////////////////
75 timer_id add(std::string const & name, ClockService::clock_type timeout,
78 /**< This call adds a new timer expiring at the given point
80 \param[in] name descriptive name
81 \param[in] timeout point in time when the timer is to
83 \param[in] cb callback
84 \returns a \c timer_id which can be used to remove the
86 void remove(timer_id id); ///< Remove timer
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 */
93 bool empty() const; ///< \c true, if no timer is registered.
98 /// Internal: Timer event
100 : public FIFORunner::TaskInfo
102 TimerEvent(timer_id id_, Callback const & cb_, TimerDispatcher & dispatcher_,
103 std::string const & name);
108 TimerDispatcher & dispatcher;
111 virtual void signal(int events);
112 static void sigHandler(int signal, ::siginfo_t * siginfo, void *);
115 FdManager & manager_;
116 FIFORunner & runner_;
118 typedef std::multimap<ClockService::clock_type, TimerEvent> TimerMap;
119 typedef std::map<int, TimerMap::iterator> TimerIdMap;
122 TimerIdMap timerIdIndex_;
130 static unsigned useCount_;
135 ///////////////////////////////hh.e////////////////////////////////////////
136 #include "TimerDispatcher.cci"
137 //#include "TimerDispatcher.ct"
138 //#include "TimerDispatcher.cti"
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"