UCommon
timers.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
27 #ifndef _UCOMMON_TIMERS_H_
28 #define _UCOMMON_TIMERS_H_
29 
30 #ifndef _UCOMMON_LINKED_H_
31 #include <ucommon/linked.h>
32 #endif
33 
34 #ifndef _MSWINDOWS_
35 #include <unistd.h>
36 #include <sys/time.h>
37 #endif
38 
39 #include <time.h>
40 
41 NAMESPACE_UCOMMON
42 
49 class __EXPORT Timer
50 {
51 private:
52  friend class Conditional;
53  friend class Semaphore;
54  friend class Event;
55 
56 #if _POSIX_TIMERS > 0 && defined(POSIX_TIMERS)
57  timespec timer;
58 #else
59 #undef POSIX_TIMERS // make sure not used if no support
60  timeval timer;
61 #endif
62  bool updated;
63 
64 public:
65 #if _MSC_VER > 1400 // windows broken dll linkage issue...
66  static const timeout_t inf = ((timeout_t)(-1));
67  static const time_t reset = ((time_t)(0));
68 #else
69  static const timeout_t inf;
70  static const time_t reset;
71 #endif
72 
73 #ifdef _MSWINDOWS_
74  typedef unsigned __int64 tick_t;
75 #else
76  typedef uint64_t tick_t;
77 #endif
78 
82  Timer();
83 
88  Timer(timeout_t offset);
89 
94  Timer(time_t offset);
95 
100  Timer(const Timer& copy);
101 
106  bool isExpired(void);
107 
112  bool isUpdated(void);
113 
118  void set(timeout_t expire);
119 
124  void set(time_t expire);
125 
129  void set(void);
130 
134  void clear(void);
135 
140  timeout_t get(void) const;
141 
146  inline timeout_t operator*() const
147  {return get();};
148 
153  bool operator!() const;
154 
159  operator bool() const;
160 
165  Timer& operator=(time_t expire);
166 
171  Timer& operator=(timeout_t expire);
172 
177  Timer& operator+=(time_t expire);
178 
183  Timer& operator+=(timeout_t expire);
184 
189  Timer& operator-=(time_t expire);
190 
195  Timer& operator-=(timeout_t expire);
196 
202  timeout_t operator-(const Timer& timer);
203 
209  bool operator==(const Timer& timer);
210 
216  bool operator!=(const Timer& timer);
217 
223  bool operator<(const Timer& timer);
224 
230  bool operator<=(const Timer& timer);
231 
237  bool operator>(const Timer& timer);
238 
244  bool operator>=(const Timer& timer);
245 
250  static void sync(Timer &timer);
251 
256  static tick_t ticks(void);
257 };
258 
269 class __EXPORT TimerQueue : public OrderedIndex
270 {
271 public:
280  class __EXPORT event : protected Timer, public LinkedList
281  {
282  protected:
283  friend class TimerQueue;
284 
289  event(timeout_t expire);
290 
296  event(TimerQueue *queue, timeout_t expire);
297 
301  virtual void expired(void) = 0;
302 
308  virtual timeout_t timeout(void);
309 
310  public:
314  virtual ~event();
315 
321  void attach(TimerQueue *queue);
322 
326  void detach(void);
327 
332  void arm(timeout_t timeout);
333 
337  void disarm(void);
338 
343  inline bool isExpired(void)
344  {return Timer::isExpired();};
345 
350  inline timeout_t get(void) const
351  {return Timer::get();};
352 
356  void update(void);
357 
362  inline TimerQueue *getQueue(void)
363  {return static_cast<TimerQueue*>(root);};
364  };
365 
366 protected:
367  friend class event;
368 
373  virtual void modify(void) = 0;
374 
380  virtual void update(void) = 0;
381 
382 public:
386  TimerQueue();
387 
391  virtual ~TimerQueue();
392 
397  void operator+=(event &timer);
398 
403  void operator-=(event &timer);
404 
412  timeout_t expire();
413 };
414 
419 
423 typedef Timer timer_t;
424 
425 END_NAMESPACE
426 
427 extern "C" {
428 #if defined(WIN32)
429  __EXPORT int gettimeofday(struct timeval *tv, void *tz);
430 #endif
431 }
432 
433 #endif