source: trash/old-modules/transport/protlib/timer.h@ 12764

Last change on this file since 12764 was 5641, checked in by Christoph Mayer, 15 years ago
File size: 5.2 KB
RevLine 
[5641]1/// ----------------------------------------*- mode: C++; -*--
2/// @file timer.h
3/// Basic TimerManager class
4/// ----------------------------------------------------------
5/// $Id: timer.h 2549 2007-04-02 22:17:37Z bless $
6/// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/timer.h $
7// ===========================================================
8//
9// Copyright (C) 2005-2007, all rights reserved by
10// - Institute of Telematics, Universitaet Karlsruhe (TH)
11//
12// More information and contact:
13// https://projekte.tm.uka.de/trac/NSIS
14//
15// This program is free software; you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation; version 2 of the License
18//
19// This program is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License along
25// with this program; if not, write to the Free Software Foundation, Inc.,
26// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27//
28// ===========================================================
29// ----------------------------------------*- mode: C++; -*--
30// timer.h - Software Timer interface
31// ----------------------------------------------------------
32// $Id: timer.h 2549 2007-04-02 22:17:37Z bless $
33// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/timer.h $
34// ==========================================================
35//
36// Institute of Telematics, University of Karlsruhe (TH)
37// ==========================================================
38/** @ingroup timer
39 * @file
40 * Software Timer interface
41 *
42 * You can create a software timer and attach a callback object to it.
43 * Timers are only accessed through their timer manager and thtimer ID.
44 * timer managers are thread-safe.
45 */
46
47#ifndef _PROTLIB__TIMER_H_
48#define _PROTLIB__TIMER_H_
49
50#include <pthread.h>
51#include <sys/time.h>
52#include <list>
53#include <ext/hash_map>
54
55#include "protlib_types.h"
56#include "llhashers.h"
57
58namespace protlib {
59
60/** @addtogroup timer Timer
61 * @{
62 */
63
64/// timer ID
65/** Each timer of a timer manager has a unique timer ID. */
66typedef uint64 timer_id_t;
67
68/// timer callback parameter
69typedef void* timer_callback_param_t;
70
71/// Timer Callback
72/** When a timer goes off, a callback is called. Each class that wants to
73 * receive timer events must inherit from TimerCallback.
74 */
75class TimerCallback {
76public:
77 virtual ~TimerCallback() { }
78
79 /// callback member function
80 /** @param id timer ID
81 * @param param additional callback parameter.
82 */
83 virtual void timer_expired(timer_id_t id, timer_callback_param_t param) = 0;
84};
85
86/// Timer Manager
87/** Creates, sets, resets, stops, checks and deletes timers. */
88class TimerManager {
89public:
90 /// constructor
91 TimerManager();
92 /// destructor
93 ~TimerManager();
94 /// start relative timer
95 timer_id_t start_relative(TimerCallback* tc, int32 sec, int32 msec = 0, timer_callback_param_t tcp = NULL);
96 /// start absolute timer
97 timer_id_t start_absolute(TimerCallback* tc, int32 sec, int32 msec = 0, timer_callback_param_t tcp = NULL);
98 /// restart relative timer
99 bool restart_relative(timer_id_t id, int32 sec, int32 msec = 0);
100 /// restart absolute timer
101 bool restart_absolute(timer_id_t id, int32 sec, int32 msec = 0);
102 /// stop timer
103 bool stop(timer_id_t id);
104 /// check for elapsed timers
105 uint32 check_timers();
106 /// check for elapsed timers or wait for next timer event.
107 uint32 check_timers_wait(int32 msec);
108 /// stop all timers
109 uint32 stop_all();
110private:
111 /// timer item
112 struct timer {
113 timer_id_t id;
114 struct timespec time;
115 TimerCallback* callback;
116 timer_callback_param_t param;
117 timer(struct timespec& ts, TimerCallback* tc, timer_callback_param_t tcp, bool get_id = true);
118 // compare two timers
119 bool operator<=(const timer& t);
120 /// execute callback
121 void do_callback();
122 /// timer ID counter
123 static timer_id_t next_id;
124 }; // end struct timer
125 /// timer manager hashmap
126 typedef hash_map<timer_id_t,timer*> timer_hashmap_t;
127 /// hashmap iterator
128 typedef timer_hashmap_t::iterator timer_hashmap_it_t;
129 /// timer list type
130 typedef list<timer*> timerlist_t;
131
132 /// cleanup hashmap and list
133 uint32 cleanup();
134 /// insert into list
135 void insert_into_list(timer* t);
136 /// delete timer from list
137 void delete_from_list(timer *t);
138 /// collect elapsed timers
139 timer* collect_elapsed();
140 /// process elapsed timers
141 uint32 process_elapsed();
142 /// timer manager mutex
143 pthread_mutex_t mutex;
144 /// timer manager mutex attributes
145 pthread_mutexattr_t mutex_attr;
146
147 /// timer manager condition
148 pthread_cond_t cond;
149 /// sorted timer list (should be replaced by a heap for performance reasons...)
150 timerlist_t active_timerlist;
151 /// elapsed timer list
152 timerlist_t elapsed_timerlist;
153 /// return first element of active timer list
154 timer* first() { return active_timerlist.empty() ? 0 : active_timerlist.front(); }
155 timer_hashmap_t hashmap;
156};
157
158//@}
159
160} // end namespace protlib
161
162#endif // _PROTLIB__TIMER_H_
Note: See TracBrowser for help on using the repository browser.