UCommon
timers.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
28 #ifndef _UCOMMON_TIMERS_H_
29 #define _UCOMMON_TIMERS_H_
30 
31 #ifndef _UCOMMON_LINKED_H_
32 #include <ucommon/linked.h>
33 #endif
34 
35 #ifndef _MSWINDOWS_
36 #include <unistd.h>
37 #include <sys/time.h>
38 #endif
39 
40 #include <time.h>
41 
42 namespace ucommon {
43 
50 class __EXPORT Timer
51 {
52 private:
53  friend class Conditional;
54  friend class Semaphore;
55  friend class Event;
56 
57 #if _POSIX_TIMERS > 0 && defined(POSIX_TIMERS)
58  timespec timer;
59 #else
60 #undef POSIX_TIMERS // make sure not used if no support
61  timeval timer;
62 #endif
63  bool updated;
64 
65 protected:
70  bool update(void);
71 
76  bool is_active(void) const;
77 
78 public:
79 #if _MSC_VER > 1400 // windows broken dll linkage issue...
80  static const timeout_t inf = ((timeout_t)(-1));
81  static const time_t reset = ((time_t)(0));
82 #else
83  static const timeout_t inf;
84  static const time_t reset;
85 #endif
86 
87 #ifdef _MSWINDOWS_
88  typedef unsigned __int64 tick_t;
89 #else
90  typedef uint64_t tick_t;
91 #endif
92 
96  Timer();
97 
102  Timer(timeout_t offset);
103 
108  Timer(time_t offset);
109 
114  Timer(const Timer& copy);
115 
120  void set(timeout_t expire);
121 
126  void set(time_t expire);
127 
131  void set(void);
132 
136  void clear(void);
137 
142  timeout_t get(void) const;
143 
148  inline timeout_t operator*() const
149  {return get();}
150 
155  bool operator!() const;
156 
161  operator bool() const;
162 
167  Timer& operator=(time_t expire);
168 
173  Timer& operator=(timeout_t expire);
174 
179  Timer& operator+=(time_t expire);
180 
185  Timer& operator+=(timeout_t expire);
186 
191  Timer& operator-=(time_t expire);
192 
197  Timer& operator-=(timeout_t expire);
198 
204  timeout_t operator-(const Timer& timer);
205 
211  bool operator==(const Timer& timer) const;
212 
218  bool operator!=(const Timer& timer) const;
219 
225  bool operator<(const Timer& timer) const;
226 
232  bool operator<=(const Timer& timer) const;
233 
239  bool operator>(const Timer& timer) const;
240 
246  bool operator>=(const Timer& timer) const;
247 
252  static void sync(Timer &timer);
253 
258  static tick_t ticks(void);
259 };
260 
271 class __EXPORT TimerQueue : public OrderedIndex
272 {
273 public:
282  class __EXPORT event : protected Timer, public LinkedList
283  {
284  protected:
285  friend class TimerQueue;
286 
291  event(timeout_t expire);
292 
298  event(TimerQueue *queue, timeout_t expire);
299 
303  virtual void expired(void) = 0;
304 
310  virtual timeout_t timeout(void);
311 
312  public:
316  virtual ~event();
317 
323  void attach(TimerQueue *queue);
324 
328  void detach(void);
329 
334  void arm(timeout_t timeout);
335 
339  void disarm(void);
340 
345  inline timeout_t get(void) const
346  {return Timer::get();}
347 
351  void update(void);
352 
357  inline TimerQueue *list(void) const
358  {return static_cast<TimerQueue*>(Root);}
359  };
360 
361 protected:
362  friend class event;
363 
368  virtual void modify(void) = 0;
369 
375  virtual void update(void) = 0;
376 
377 public:
381  TimerQueue();
382 
386  virtual ~TimerQueue();
387 
392  void operator+=(event &timer);
393 
398  void operator-=(event &timer);
399 
407  timeout_t expire();
408 };
409 
414 
418 typedef Timer timer_t;
419 
420 } // namespace ucommon
421 
422 extern "C" {
423 #if defined(WIN32)
424  __EXPORT int gettimeofday(struct timeval *tv, void *tz);
425 #endif
426 }
427 
428 #endif
The conditional is a common base for other thread synchronizing classes.
Definition: thread.h:87
Timer timer_t
A convenience type for timers.
Definition: timers.h:418
TimerQueue::event TQEvent
A convenience type for timer queue timer events.
Definition: timers.h:413
A timer event object that lives on a timer queue.
Definition: timers.h:282
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
Linked objects, lists, templates, and containers.
void modify(accesslock_t &lock)
Convenience function to exclusively schedule conditional access.
Definition: thread.h:1957
timeout_t get(void) const
Get remaining time until the timer expires.
A double linked list object.
Definition: linked.h:798
timeout_t operator*() const
Get remaining time until timer expires by reference.
Definition: timers.h:148
An index container for maintaining an ordered list of objects.
Definition: linked.h:182
A timer queue for timer events.
Definition: timers.h:271
Common namespace for all ucommon objects.
Definition: access.h:47
TimerQueue * list(void) const
Get the timer queue we are attached to.
Definition: timers.h:357
Timer class to use when scheduling realtime events.
Definition: timers.h:50
static const time_t reset
A value to use when resetting.
Definition: timers.h:84
static const timeout_t inf
A value to use for infinite time.
Definition: timers.h:83
A portable counting semaphore class.
Definition: thread.h:879