00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00036 #ifndef _PROTLIB__TIMER_MODULE_H
00037 #define _PROTLIB__TIMER_MODULE_H
00038
00039 #include <boost/unordered_map.hpp>
00040
00041 #include "messages.h"
00042 #include "threads.h"
00043 #include "llhashers.h"
00044 #include "timer.h"
00045
00046 using boost::unordered_map;
00047
00048 namespace protlib {
00049
00054 static const char *const action_t_str[]=
00055 {
00056 "ignore",
00057 "start",
00058 "restart",
00059 "stop",
00060 "stop all",
00061 "elapsed",
00062 "unkown"
00063 };
00064
00066
00070 class TimerMsg : public message
00071 {
00072 public:
00074
00075 enum action_t
00076 {
00077 ac_ignore = 0,
00078 ac_start = 1,
00079 ac_restart = 2,
00080 ac_stop = 3,
00081 ac_stop_all = 4,
00082 ac_elapsed = 5
00083 };
00084
00086
00089 typedef timer_callback_param_t param_t;
00091 TimerMsg(qaddr_t s = qaddr_unknown, bool s_err = false);
00093 virtual ~TimerMsg();
00095 void get_time(int32& sec, int32& msec);
00097 action_t get_action() const;
00099 const char* get_action_str() const;
00101 bool set_ok(bool r);
00103 bool is_ok();
00105 bool start_absolute(int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
00107 bool start_relative(int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
00109 bool start(bool rel, int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
00111 bool restart_absolute(id_t id, int32 sec, int32 msec = 0);
00113 bool restart_relative(id_t id, int32 sec, int32 msec = 0);
00115 bool restart(bool rel, id_t id, int32 sec, int32 msec = 0);
00117 bool stop(id_t id);
00119 bool stop_all();
00121 bool set_elapsed();
00123 bool get_send_error() const;
00125 bool set_send_error(bool f);
00127 param_t get_param1() const;
00129 param_t get_param2() const;
00131 void get_params(param_t& p1, param_t& p2) const;
00133 bool is_absolute() const;
00135 bool is_relative() const;
00136 private:
00137 int32 time_sec;
00138 int32 time_msec;
00139 action_t action;
00140 param_t param1;
00141 param_t param2;
00142 bool ok;
00143 bool send_error;
00144 bool relative;
00145 };
00146
00151 inline
00152 void
00153 TimerMsg::get_time(int32& sec, int32& msec)
00154 {
00155 sec = time_sec;
00156 msec = time_msec;
00157 }
00158
00162 inline
00163 TimerMsg::action_t
00164 TimerMsg::get_action() const
00165 {
00166 return action;
00167 }
00168
00169 inline
00170 const char*
00171 TimerMsg::get_action_str() const
00172 {
00173 return action_t_str[action];
00174 }
00175
00177 bool
00178 inline
00179 TimerMsg::is_ok()
00180 {
00181 return ok;
00182 }
00183
00184
00186
00187 struct TimerModuleParam : public ThreadParam
00188 {
00189 TimerModuleParam(uint32 sleep_time = ThreadParam::default_sleep_time,
00190 bool sua = false, bool ser = true, bool sre = true);
00192 const bool send_until_abort;
00193 const message::qaddr_t source;
00194 const bool send_error_expedited;
00195 const bool send_reply_expedited;
00196 };
00197
00199
00200 class TimerModule : public Thread, public TimerCallback
00201 {
00202
00203 public:
00205
00208 virtual void timer_expired(timer_id_t timer, timer_callback_param_t callback_param);
00209 public:
00211 TimerModule(const TimerModuleParam& p);
00213 virtual ~TimerModule();
00215 virtual void main_loop(uint32 nr);
00216 private:
00218 void process_queue();
00220 void process_elapsed_timers();
00222 bool start_timer(TimerMsg* m);
00224 bool restart_timer(TimerMsg* m);
00226 bool stop_timer(TimerMsg* m);
00228 bool stop_all_timers();
00230 void send_error_or_dispose(TimerMsg* m, bool ok);
00232 TimerManager tm;
00234 const TimerModuleParam timerparam;
00236
00237 class TimerMap
00238 {
00239 public:
00240 bool insert(timer_id_t tid, TimerMsg* m);
00242 message::id_t lookup_mid(timer_id_t tid) const;
00244 timer_id_t lookup_tid(message::id_t mid) const;
00246 TimerMsg* lookup_msg(timer_id_t tid) const;
00248 void erase(timer_id_t tid, message::id_t mid, bool dispose = true);
00250 void clear(bool dispose = true);
00251 private:
00252 typedef unordered_map<message::id_t,timer_id_t> mid2tid_t;
00253 typedef mid2tid_t::const_iterator const_mid2tid_it_t;
00254 typedef unordered_map<timer_id_t,message::id_t> tid2mid_t;
00255 typedef tid2mid_t::const_iterator const_tid2mid_it_t;
00256 typedef unordered_map<timer_id_t,TimerMsg*> tid2msg_t;
00257 typedef tid2msg_t::const_iterator const_tid2msg_it_t;
00258 mid2tid_t mid2tid;
00259 tid2mid_t tid2mid;
00260 tid2msg_t tid2msg;
00261 } tmap;
00262 };
00263
00265
00266 }
00267
00268 #endif