UCommon
|
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00002 // Copyright (C) 2015 Cherokees of Idaho. 00003 // 00004 // This file is part of GNU uCommon C++. 00005 // 00006 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00007 // it under the terms of the GNU Lesser General Public License as published 00008 // by the Free Software Foundation, either version 3 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // GNU uCommon C++ is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public License 00017 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00018 00028 #ifndef _UCOMMON_TIMERS_H_ 00029 #define _UCOMMON_TIMERS_H_ 00030 00031 #ifndef _UCOMMON_LINKED_H_ 00032 #include <ucommon/linked.h> 00033 #endif 00034 00035 #ifndef _MSWINDOWS_ 00036 #include <unistd.h> 00037 #include <sys/time.h> 00038 #endif 00039 00040 #include <time.h> 00041 00042 namespace ucommon { 00043 00050 class __EXPORT Timer 00051 { 00052 private: 00053 friend class Conditional; 00054 friend class Semaphore; 00055 friend class Event; 00056 00057 #if _POSIX_TIMERS > 0 && defined(POSIX_TIMERS) 00058 timespec timer; 00059 #else 00060 #undef POSIX_TIMERS // make sure not used if no support 00061 timeval timer; 00062 #endif 00063 bool updated; 00064 00065 protected: 00070 bool update(void); 00071 00076 bool is_active(void) const; 00077 00078 public: 00079 #if _MSC_VER > 1400 // windows broken dll linkage issue... 00080 static const timeout_t inf = ((timeout_t)(-1)); 00081 static const time_t reset = ((time_t)(0)); 00082 #else 00083 static const timeout_t inf; 00084 static const time_t reset; 00085 #endif 00086 00087 #ifdef _MSWINDOWS_ 00088 typedef unsigned __int64 tick_t; 00089 #else 00090 typedef uint64_t tick_t; 00091 #endif 00092 00096 Timer(); 00097 00102 Timer(timeout_t offset); 00103 00108 Timer(time_t offset); 00109 00114 Timer(const Timer& copy); 00115 00120 void set(timeout_t expire); 00121 00126 void set(time_t expire); 00127 00131 void set(void); 00132 00136 void clear(void); 00137 00142 timeout_t get(void) const; 00143 00148 inline timeout_t operator*() const 00149 {return get();} 00150 00155 bool operator!() const; 00156 00161 operator bool() const; 00162 00167 Timer& operator=(time_t expire); 00168 00173 Timer& operator=(timeout_t expire); 00174 00179 Timer& operator+=(time_t expire); 00180 00185 Timer& operator+=(timeout_t expire); 00186 00191 Timer& operator-=(time_t expire); 00192 00197 Timer& operator-=(timeout_t expire); 00198 00204 timeout_t operator-(const Timer& timer); 00205 00211 bool operator==(const Timer& timer) const; 00212 00218 bool operator!=(const Timer& timer) const; 00219 00225 bool operator<(const Timer& timer) const; 00226 00232 bool operator<=(const Timer& timer) const; 00233 00239 bool operator>(const Timer& timer) const; 00240 00246 bool operator>=(const Timer& timer) const; 00247 00252 static void sync(Timer &timer); 00253 00258 static tick_t ticks(void); 00259 }; 00260 00271 class __EXPORT TimerQueue : public OrderedIndex 00272 { 00273 public: 00282 class __EXPORT event : protected Timer, public LinkedList 00283 { 00284 protected: 00285 friend class TimerQueue; 00286 00291 event(timeout_t expire); 00292 00298 event(TimerQueue *queue, timeout_t expire); 00299 00303 virtual void expired(void) = 0; 00304 00310 virtual timeout_t timeout(void); 00311 00312 public: 00316 virtual ~event(); 00317 00323 void attach(TimerQueue *queue); 00324 00328 void detach(void); 00329 00334 void arm(timeout_t timeout); 00335 00339 void disarm(void); 00340 00345 inline timeout_t get(void) const 00346 {return Timer::get();} 00347 00351 void update(void); 00352 00357 inline TimerQueue *list(void) const 00358 {return static_cast<TimerQueue*>(Root);} 00359 }; 00360 00361 protected: 00362 friend class event; 00363 00368 virtual void modify(void) = 0; 00369 00375 virtual void update(void) = 0; 00376 00377 public: 00381 TimerQueue(); 00382 00386 virtual ~TimerQueue(); 00387 00392 void operator+=(event &timer); 00393 00398 void operator-=(event &timer); 00399 00407 timeout_t expire(); 00408 }; 00409 00413 typedef TimerQueue::event TQEvent; 00414 00418 typedef Timer timer_t; 00419 00420 } // namespace ucommon 00421 00422 extern "C" { 00423 #if defined(WIN32) 00424 __EXPORT int gettimeofday(struct timeval *tv, void *tz); 00425 #endif 00426 } 00427 00428 #endif