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 && defined(POSIX_TIMERS)
00057 timespec timer;
00058 #else
00059 #undef POSIX_TIMERS // make sure not used if no support
00060 timeval timer;
00061 #endif
00062 bool updated;
00063
00064 public:
00065 #if _MSC_VER > 1400 // windows broken dll linkage issue...
00066 static const timeout_t inf = ((timeout_t)(-1));
00067 static const time_t reset = ((time_t)(0));
00068 #else
00069 static const timeout_t inf;
00070 static const time_t reset;
00071 #endif
00072
00073 #ifdef _MSWINDOWS_
00074 typedef unsigned __int64 tick_t;
00075 #else
00076 typedef uint64_t tick_t;
00077 #endif
00078
00082 Timer();
00083
00088 Timer(timeout_t offset);
00089
00094 Timer(time_t offset);
00095
00100 Timer(const Timer& copy);
00101
00106 bool isExpired(void);
00107
00112 bool isUpdated(void);
00113
00118 void set(timeout_t expire);
00119
00124 void set(time_t expire);
00125
00129 void set(void);
00130
00134 void clear(void);
00135
00140 timeout_t get(void) const;
00141
00146 inline timeout_t operator*() const
00147 {return get();};
00148
00153 bool operator!() const;
00154
00159 operator bool() const;
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
00189 Timer& operator-=(time_t expire);
00190
00195 Timer& operator-=(timeout_t expire);
00196
00202 timeout_t operator-(const Timer& timer);
00203
00209 bool operator==(const Timer& timer);
00210
00216 bool operator!=(const Timer& timer);
00217
00223 bool operator<(const Timer& timer);
00224
00230 bool operator<=(const Timer& timer);
00231
00237 bool operator>(const Timer& timer);
00238
00244 bool operator>=(const Timer& timer);
00245
00250 static void sync(Timer &timer);
00251
00256 static tick_t ticks(void);
00257 };
00258
00269 class __EXPORT TimerQueue : public OrderedIndex
00270 {
00271 public:
00280 class __EXPORT event : protected Timer, public LinkedList
00281 {
00282 protected:
00283 friend class TimerQueue;
00284
00289 event(timeout_t expire);
00290
00296 event(TimerQueue *queue, timeout_t expire);
00297
00301 virtual void expired(void) = 0;
00302
00308 virtual timeout_t timeout(void);
00309
00310 public:
00314 virtual ~event();
00315
00321 void attach(TimerQueue *queue);
00322
00326 void detach(void);
00327
00332 void arm(timeout_t timeout);
00333
00337 void disarm(void);
00338
00343 inline bool isExpired(void)
00344 {return Timer::isExpired();};
00345
00350 inline timeout_t get(void) const
00351 {return Timer::get();};
00352
00356 void update(void);
00357
00362 inline TimerQueue *getQueue(void)
00363 {return static_cast<TimerQueue*>(root);};
00364 };
00365
00366 protected:
00367 friend class event;
00368
00373 virtual void modify(void) = 0;
00374
00380 virtual void update(void) = 0;
00381
00382 public:
00386 TimerQueue();
00387
00391 virtual ~TimerQueue();
00392
00397 void operator+=(event &timer);
00398
00403 void operator-=(event &timer);
00404
00412 timeout_t expire();
00413 };
00414
00418 typedef TimerQueue::event TQEvent;
00419
00423 typedef Timer timer_t;
00424
00425 END_NAMESPACE
00426
00427 extern "C" {
00428 #if defined(WIN32)
00429 __EXPORT int gettimeofday(struct timeval *tv, void *tz);
00430 #endif
00431 }
00432
00433 #endif