00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00027 #ifndef _UCOMMON_TIMERS_H_
00028 #define _UCOMMON_TIMERS_H_
00029
00030 #ifndef _UCOMMON_LINKED_H_
00031 #include <ucommon/linked.h>
00032 #endif
00033
00034 #ifndef _MSWINDOWS_
00035 #include <unistd.h>
00036 #include <sys/time.h>
00037 #endif
00038
00039 #include <time.h>
00040
00041 NAMESPACE_UCOMMON
00042
00049 class __EXPORT Timer
00050 {
00051 private:
00052 friend class Conditional;
00053 friend class Semaphore;
00054 friend class Event;
00055
00056 #if _POSIX_TIMERS > 0
00057 timespec timer;
00058 #else
00059 timeval timer;
00060 #endif
00061 bool updated;
00062
00063 public:
00064 static const timeout_t inf;
00065 static const time_t reset;
00070 Timer();
00071
00076 Timer(timeout_t offset);
00077
00082 Timer(time_t offset);
00083
00088 bool isExpired(void);
00089
00094 bool isUpdated(void);
00095
00100 void set(timeout_t expire);
00101
00106 void set(time_t expire);
00107
00111 void set(void);
00112
00116 void clear(void);
00117
00122 timeout_t get(void);
00123
00128 inline timeout_t operator*()
00129 {return get();};
00130
00135 bool operator!();
00136
00141 void operator=(time_t expire);
00142
00147 void operator=(timeout_t expire);
00148
00153 void operator+=(time_t expire);
00154
00159 void operator+=(timeout_t expire);
00160
00165 void operator-=(time_t expire);
00166
00171 void operator-=(timeout_t expire);
00172
00177 static void sync(Timer &timer);
00178 };
00179
00190 class __EXPORT TimerQueue : public OrderedIndex
00191 {
00192 public:
00201 class __EXPORT event : protected Timer, public LinkedList
00202 {
00203 protected:
00204 friend class TimerQueue;
00205
00210 event(timeout_t expire);
00211
00217 event(TimerQueue *queue, timeout_t expire);
00218
00222 virtual void expired(void) = 0;
00223
00229 virtual timeout_t timeout(void);
00230
00231 public:
00235 virtual ~event();
00236
00242 void attach(TimerQueue *queue);
00243
00247 void detach(void);
00248
00253 void arm(timeout_t timeout);
00254
00258 void disarm(void);
00259
00264 inline bool isExpired(void)
00265 {return Timer::isExpired();};
00266
00271 inline timeout_t get(void)
00272 {return Timer::get();};
00273
00277 void update(void);
00278
00283 inline TimerQueue *getQueue(void)
00284 {return static_cast<TimerQueue*>(root);};
00285 };
00286
00287 protected:
00288 friend class event;
00289
00294 virtual void modify(void) = 0;
00295
00301 virtual void update(void) = 0;
00302
00303 public:
00307 TimerQueue();
00308
00312 virtual ~TimerQueue();
00313
00318 void operator+=(event &timer);
00319
00324 void operator-=(event &timer);
00325
00333 timeout_t expire();
00334 };
00335
00339 typedef TimerQueue::event TQEvent;
00340
00344 typedef Timer timer_t;
00345
00346 END_NAMESPACE
00347
00348 extern "C" {
00349 #if defined(WIN32)
00350 __EXPORT int gettimeofday(struct timeval *tv, void *tz);
00351 #endif
00352 }
00353
00354 #endif