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 <ext/hash_map>
00040
00041 #include "messages.h"
00042 #include "threads.h"
00043 #include "llhashers.h"
00044 #include "timer.h"
00045
00046 namespace protlib {
00047
00052 static const char *const action_t_str[]=
00053 {
00054 "ignore",
00055 "start",
00056 "restart",
00057 "stop",
00058 "stop all",
00059 "elapsed",
00060 "unkown"
00061 };
00062
00064
00068 class TimerMsg : public message
00069 {
00070 public:
00072
00073 enum action_t
00074 {
00075 ac_ignore = 0,
00076 ac_start = 1,
00077 ac_restart = 2,
00078 ac_stop = 3,
00079 ac_stop_all = 4,
00080 ac_elapsed = 5
00081 };
00082
00084
00087 typedef timer_callback_param_t param_t;
00089 TimerMsg(qaddr_t s = qaddr_unknown, bool s_err = false);
00091 virtual ~TimerMsg();
00093 void get_time(int32& sec, int32& msec);
00095 action_t get_action() const;
00097 const char* get_action_str() const;
00099 bool set_ok(bool r);
00101 bool is_ok();
00103 bool start_absolute(int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
00105 bool start_relative(int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
00107 bool start(bool rel, int32 sec, int32 msec = 0, param_t p1 = NULL, param_t p2 = NULL);
00109 bool restart_absolute(id_t id, int32 sec, int32 msec = 0);
00111 bool restart_relative(id_t id, int32 sec, int32 msec = 0);
00113 bool restart(bool rel, id_t id, int32 sec, int32 msec = 0);
00115 bool stop(id_t id);
00117 bool stop_all();
00119 bool set_elapsed();
00121 bool get_send_error() const;
00123 bool set_send_error(bool f);
00125 param_t get_param1() const;
00127 param_t get_param2() const;
00129 void get_params(param_t& p1, param_t& p2) const;
00131 bool is_absolute() const;
00133 bool is_relative() const;
00134 private:
00135 int32 time_sec;
00136 int32 time_msec;
00137 action_t action;
00138 param_t param1;
00139 param_t param2;
00140 bool ok;
00141 bool send_error;
00142 bool relative;
00143 };
00144
00149 inline
00150 void
00151 TimerMsg::get_time(int32& sec, int32& msec)
00152 {
00153 sec = time_sec;
00154 msec = time_msec;
00155 }
00156
00160 inline
00161 TimerMsg::action_t
00162 TimerMsg::get_action() const
00163 {
00164 return action;
00165 }
00166
00167 inline
00168 const char*
00169 TimerMsg::get_action_str() const
00170 {
00171 return action_t_str[action];
00172 }
00173
00175 bool
00176 inline
00177 TimerMsg::is_ok()
00178 {
00179 return ok;
00180 }
00181
00182
00184
00185 struct TimerModuleParam : public ThreadParam
00186 {
00187 TimerModuleParam(uint32 sleep_time = ThreadParam::default_sleep_time,
00188 bool sua = false, bool ser = true, bool sre = true);
00190 const bool send_until_abort;
00191 const message::qaddr_t source;
00192 const bool send_error_expedited;
00193 const bool send_reply_expedited;
00194 };
00195
00197
00198 class TimerModule : public Thread, public TimerCallback
00199 {
00200
00201 public:
00203
00206 virtual void timer_expired(timer_id_t timer, timer_callback_param_t callback_param);
00207 public:
00209 TimerModule(const TimerModuleParam& p);
00211 virtual ~TimerModule();
00213 virtual void main_loop(uint32 nr);
00214 private:
00216 void process_queue();
00218 void process_elapsed_timers();
00220 bool start_timer(TimerMsg* m);
00222 bool restart_timer(TimerMsg* m);
00224 bool stop_timer(TimerMsg* m);
00226 bool stop_all_timers();
00228 void send_error_or_dispose(TimerMsg* m, bool ok);
00230 TimerManager tm;
00232 const TimerModuleParam timerparam;
00234
00235 class TimerMap
00236 {
00237 public:
00238 bool insert(timer_id_t tid, TimerMsg* m);
00240 message::id_t lookup_mid(timer_id_t tid) const;
00242 timer_id_t lookup_tid(message::id_t mid) const;
00244 TimerMsg* lookup_msg(timer_id_t tid) const;
00246 void erase(timer_id_t tid, message::id_t mid, bool dispose = true);
00248 void clear(bool dispose = true);
00249 private:
00250 typedef hash_map<message::id_t,timer_id_t> mid2tid_t;
00251 typedef mid2tid_t::const_iterator const_mid2tid_it_t;
00252 typedef hash_map<timer_id_t,message::id_t> tid2mid_t;
00253 typedef tid2mid_t::const_iterator const_tid2mid_it_t;
00254 typedef hash_map<timer_id_t,TimerMsg*> tid2msg_t;
00255 typedef tid2msg_t::const_iterator const_tid2msg_it_t;
00256 mid2tid_t mid2tid;
00257 tid2mid_t tid2mid;
00258 tid2msg_t tid2msg;
00259 } tmap;
00260 };
00261
00263
00264 }
00265
00266 #endif