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 protected:
69  bool update(void);
70 
75  bool is_active(void);
76 
77 public:
78 #if _MSC_VER > 1400 // windows broken dll linkage issue...
79  static const timeout_t inf = ((timeout_t)(-1));
80  static const time_t reset = ((time_t)(0));
81 #else
82  static const timeout_t inf;
83  static const time_t reset;
84 #endif
85 
86 #ifdef _MSWINDOWS_
87  typedef unsigned __int64 tick_t;
88 #else
89  typedef uint64_t tick_t;
90 #endif
91 
95  Timer();
96 
101  Timer(timeout_t offset);
102 
107  Timer(time_t offset);
108 
113  Timer(const Timer& copy);
114 
119  void set(timeout_t expire);
120 
125  void set(time_t expire);
126 
130  void set(void);
131 
135  void clear(void);
136 
141  timeout_t get(void) const;
142 
147  inline timeout_t operator*() const
148  {return get();};
149 
154  bool operator!() const;
155 
160  operator bool() const;
161 
166  Timer& operator=(time_t expire);
167 
172  Timer& operator=(timeout_t expire);
173 
178  Timer& operator+=(time_t expire);
179 
184  Timer& operator+=(timeout_t expire);
185 
190  Timer& operator-=(time_t expire);
191 
196  Timer& operator-=(timeout_t expire);
197 
203  timeout_t operator-(const Timer& timer);
204 
210  bool operator==(const Timer& timer) const;
211 
217  bool operator!=(const Timer& timer) const;
218 
224  bool operator<(const Timer& timer) const;
225 
231  bool operator<=(const Timer& timer) const;
232 
238  bool operator>(const Timer& timer) const;
239 
245  bool operator>=(const Timer& timer) const;
246 
251  static void sync(Timer &timer);
252 
257  static tick_t ticks(void);
258 };
259 
270 class __EXPORT TimerQueue : public OrderedIndex
271 {
272 public:
281  class __EXPORT event : protected Timer, public LinkedList
282  {
283  protected:
284  friend class TimerQueue;
285 
290  event(timeout_t expire);
291 
297  event(TimerQueue *queue, timeout_t expire);
298 
302  virtual void expired(void) = 0;
303 
309  virtual timeout_t timeout(void);
310 
311  public:
315  virtual ~event();
316 
322  void attach(TimerQueue *queue);
323 
327  void detach(void);
328 
333  void arm(timeout_t timeout);
334 
338  void disarm(void);
339 
344  inline timeout_t get(void) const
345  {return Timer::get();};
346 
350  void update(void);
351 
356  inline TimerQueue *list(void)
357  {return static_cast<TimerQueue*>(Root);};
358  };
359 
360 protected:
361  friend class event;
362 
367  virtual void modify(void) = 0;
368 
374  virtual void update(void) = 0;
375 
376 public:
380  TimerQueue();
381 
385  virtual ~TimerQueue();
386 
391  void operator+=(event &timer);
392 
397  void operator-=(event &timer);
398 
406  timeout_t expire();
407 };
408 
413 
417 typedef Timer timer_t;
418 
419 END_NAMESPACE
420 
421 extern "C" {
422 #if defined(WIN32)
423  __EXPORT int gettimeofday(struct timeval *tv, void *tz);
424 #endif
425 }
426 
427 #endif
static const timeout_t inf
A value to use for infinite time.
Definition: timers.h:82
bool update(void)
Check if timer has been updated since last check.
Timer timer_t
A convenience type for timers.
Definition: timers.h:417
void operator-=(LinkedList *object)
Insert object in front of our object.
Definition: linked.h:878
TimerQueue * list(void)
Get the timer queue we are attached to.
Definition: timers.h:356
void operator+=(LinkedList *object)
Insert object behind our object.
Definition: linked.h:871
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
A timer event object that lives on a timer queue.
Definition: timers.h:281
timeout_t operator*() const
Get remaining time until timer expires by reference.
Definition: timers.h:147
static const time_t reset
A value to use when resetting.
Definition: timers.h:83
TimerQueue::event TQEvent
A convenience type for timer queue timer events.
Definition: timers.h:412
timeout_t get(void) const
Get remaining time until the timer expires.
A portable counting semaphore class.
Definition: thread.h:853
A timer queue for timer events.
Definition: timers.h:270
void modify(accesslock_t &lock)
Convenience function to exclusively schedule conditional access.
Definition: thread.h:1905
Linked objects, lists, templates, and containers.
A double linked list object.
Definition: linked.h:769
The conditional is a common base for other thread synchronizing classes.
Definition: thread.h:86
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:479
Timer class to use when scheduling realtime events.
Definition: timers.h:49
An index container for maintaining an ordered list of objects.
Definition: linked.h:175