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 Timer(const Timer& copy);
00089
00094 bool isExpired(void);
00095
00100 bool isUpdated(void);
00101
00106 void set(timeout_t expire);
00107
00112 void set(time_t expire);
00113
00117 void set(void);
00118
00122 void clear(void);
00123
00128 timeout_t get(void) const;
00129
00134 inline timeout_t operator*() const
00135 {return get();};
00136
00141 bool operator!() const;
00142
00147 operator bool() const;
00148
00153 Timer& operator=(time_t expire);
00154
00159 Timer& operator=(timeout_t expire);
00160
00165 Timer& operator+=(time_t expire);
00166
00171 Timer& operator+=(timeout_t expire);
00172
00177 Timer& operator-=(time_t expire);
00178
00183 Timer& operator-=(timeout_t expire);
00184
00190 timeout_t operator-(const Timer& timer);
00191
00197 bool operator==(const Timer& timer);
00198
00204 bool operator!=(const Timer& timer);
00205
00211 bool operator<(const Timer& timer);
00212
00218 bool operator<=(const Timer& timer);
00219
00225 bool operator>(const Timer& timer);
00226
00232 bool operator>=(const Timer& timer);
00233
00238 static void sync(Timer &timer);
00239 };
00240
00251 class __EXPORT TimerQueue : public OrderedIndex
00252 {
00253 public:
00262 class __EXPORT event : protected Timer, public LinkedList
00263 {
00264 protected:
00265 friend class TimerQueue;
00266
00271 event(timeout_t expire);
00272
00278 event(TimerQueue *queue, timeout_t expire);
00279
00283 virtual void expired(void) = 0;
00284
00290 virtual timeout_t timeout(void);
00291
00292 public:
00296 virtual ~event();
00297
00303 void attach(TimerQueue *queue);
00304
00308 void detach(void);
00309
00314 void arm(timeout_t timeout);
00315
00319 void disarm(void);
00320
00325 inline bool isExpired(void)
00326 {return Timer::isExpired();};
00327
00332 inline timeout_t get(void) const
00333 {return Timer::get();};
00334
00338 void update(void);
00339
00344 inline TimerQueue *getQueue(void)
00345 {return static_cast<TimerQueue*>(root);};
00346 };
00347
00348 protected:
00349 friend class event;
00350
00355 virtual void modify(void) = 0;
00356
00362 virtual void update(void) = 0;
00363
00364 public:
00368 TimerQueue();
00369
00373 virtual ~TimerQueue();
00374
00379 void operator+=(event &timer);
00380
00385 void operator-=(event &timer);
00386
00394 timeout_t expire();
00395 };
00396
00400 typedef TimerQueue::event TQEvent;
00401
00405 typedef Timer timer_t;
00406
00407 END_NAMESPACE
00408
00409 extern "C" {
00410 #if defined(WIN32)
00411 __EXPORT int gettimeofday(struct timeval *tv, void *tz);
00412 #endif
00413 }
00414
00415 #endif