UCommon
|
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 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; 00067 #ifdef _MSWINDOWS_ 00068 typedef unsigned __int64 tick_t; 00069 #else 00070 typedef uint64_t tick_t; 00071 #endif 00072 00076 Timer(); 00077 00082 Timer(timeout_t offset); 00083 00088 Timer(time_t offset); 00089 00094 Timer(const Timer& copy); 00095 00100 bool isExpired(void); 00101 00106 bool isUpdated(void); 00107 00112 void set(timeout_t expire); 00113 00118 void set(time_t expire); 00119 00123 void set(void); 00124 00128 void clear(void); 00129 00134 timeout_t get(void) const; 00135 00140 inline timeout_t operator*() const 00141 {return get();}; 00142 00147 bool operator!() const; 00148 00153 operator bool() const; 00154 00159 Timer& operator=(time_t expire); 00160 00165 Timer& operator=(timeout_t expire); 00166 00171 Timer& operator+=(time_t expire); 00172 00177 Timer& operator+=(timeout_t expire); 00178 00183 Timer& operator-=(time_t expire); 00184 00189 Timer& operator-=(timeout_t expire); 00190 00196 timeout_t operator-(const Timer& timer); 00197 00203 bool operator==(const Timer& timer); 00204 00210 bool operator!=(const Timer& timer); 00211 00217 bool operator<(const Timer& timer); 00218 00224 bool operator<=(const Timer& timer); 00225 00231 bool operator>(const Timer& timer); 00232 00238 bool operator>=(const Timer& timer); 00239 00244 static void sync(Timer &timer); 00245 00250 static tick_t ticks(void); 00251 }; 00252 00263 class __EXPORT TimerQueue : public OrderedIndex 00264 { 00265 public: 00274 class __EXPORT event : protected Timer, public LinkedList 00275 { 00276 protected: 00277 friend class TimerQueue; 00278 00283 event(timeout_t expire); 00284 00290 event(TimerQueue *queue, timeout_t expire); 00291 00295 virtual void expired(void) = 0; 00296 00302 virtual timeout_t timeout(void); 00303 00304 public: 00308 virtual ~event(); 00309 00315 void attach(TimerQueue *queue); 00316 00320 void detach(void); 00321 00326 void arm(timeout_t timeout); 00327 00331 void disarm(void); 00332 00337 inline bool isExpired(void) 00338 {return Timer::isExpired();}; 00339 00344 inline timeout_t get(void) const 00345 {return Timer::get();}; 00346 00350 void update(void); 00351 00356 inline TimerQueue *getQueue(void) 00357 {return static_cast<TimerQueue*>(root);}; 00358 }; 00359 00360 protected: 00361 friend class event; 00362 00367 virtual void modify(void) = 0; 00368 00374 virtual void update(void) = 0; 00375 00376 public: 00380 TimerQueue(); 00381 00385 virtual ~TimerQueue(); 00386 00391 void operator+=(event &timer); 00392 00397 void operator-=(event &timer); 00398 00406 timeout_t expire(); 00407 }; 00408 00412 typedef TimerQueue::event TQEvent; 00413 00417 typedef Timer timer_t; 00418 00419 END_NAMESPACE 00420 00421 extern "C" { 00422 #if defined(WIN32) 00423 __EXPORT int gettimeofday(struct timeval *tv, void *tz); 00424 #endif 00425 } 00426 00427 #endif