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:
71  bool update(void);
72 
77  bool is_active(void) const;
78 
79 public:
80  static const timeout_t inf = ((timeout_t)(-1));
81  static const time_t reset = ((time_t)(0));
82 
83 #ifdef _MSWINDOWS_
84  typedef unsigned __int64 tick_t;
85 #else
86  typedef uint64_t tick_t;
87 #endif
88 
92  Timer();
93 
98  Timer(timeout_t offset);
99 
104  Timer(time_t offset);
105 
110  Timer(const Timer& copy);
111 
116  void set(timeout_t expire);
117 
122  void set(time_t expire);
123 
127  void set(void);
128 
132  void clear(void);
133 
138  timeout_t get(void) const;
139 
144  inline timeout_t operator*() const {
145  return get();
146  }
147 
152  bool operator!() const;
153 
158  operator bool() const;
159 
164  Timer& operator=(time_t expire);
165 
170  Timer& operator=(timeout_t expire);
171 
176  Timer& operator+=(time_t expire);
177 
182  Timer& operator+=(timeout_t expire);
183 
188  Timer& operator-=(time_t expire);
189 
194  Timer& operator-=(timeout_t expire);
195 
201  timeout_t operator-(const Timer& timer);
202 
208  bool operator==(const Timer& timer) const;
209 
215  bool operator!=(const Timer& timer) const;
216 
222  bool operator<(const Timer& timer) const;
223 
229  bool operator<=(const Timer& timer) const;
230 
236  bool operator>(const Timer& timer) const;
237 
243  bool operator>=(const Timer& timer) const;
244 
249  static void sync(Timer &timer);
250 
255  static tick_t ticks(void);
256 };
257 
268 class __EXPORT TimerQueue : public OrderedIndex
269 {
270 private:
271  __DELETE_COPY(TimerQueue);
272 
273 public:
282  class __EXPORT event : protected Timer, public DLinkedObject
283  {
284  private:
285  __DELETE_DEFAULTS(event);
286 
287  protected:
288  friend class TimerQueue;
289 
294  event(timeout_t expire);
295 
301  event(TimerQueue *queue, timeout_t expire);
302 
306  virtual void expired(void) = 0;
307 
313  virtual timeout_t timeout(void);
314 
315  public:
319  virtual ~event();
320 
326  void attach(TimerQueue *queue);
327 
331  void detach(void);
332 
337  void arm(timeout_t timeout);
338 
342  void disarm(void);
343 
348  inline timeout_t get(void) const {
349  return Timer::get();
350  }
351 
352  inline timeout_t operator*() const {
353  return Timer::get();
354  }
355 
359  void update(void);
360 
365  inline TimerQueue *list(void) const {
366  return static_cast<TimerQueue*>(Root);
367  }
368  };
369 
370 protected:
371  friend class event;
372 
377  virtual void modify(void) = 0;
378 
384  virtual void update(void) = 0;
385 
386 public:
390  TimerQueue();
391 
395  virtual ~TimerQueue();
396 
401  void operator+=(event &timer);
402 
407  void operator-=(event &timer);
408 
416  timeout_t expire();
417 };
418 
423 
427 typedef Timer timer_t;
428 
429 } // namespace ucommon
430 
431 #endif
The conditional is a common base for other thread synchronizing classes.
Definition: condition.h:227
Linked objects, lists, templates, and containers.
Timer class to use when scheduling realtime events.
Definition: timers.h:50
Timer timer_t
A convenience type for timers.
Definition: timers.h:427
A timer event object that lives on a timer queue.
Definition: timers.h:282
TimerQueue::event TQEvent
A convenience type for timer queue timer events.
Definition: timers.h:422
TimerQueue * list(void) const
Get the timer queue we are attached to.
Definition: timers.h:365
timeout_t get(void) const
Time remaining until expired.
Definition: timers.h:348
timeout_t get(void) const
Get remaining time until the timer expires.
Common namespace for all ucommon objects.
Definition: access.h:47
A double linked list object.
Definition: linked.h:768
T copy(const T &src)
Convenience function to copy objects.
Definition: generics.h:395
A timer queue for timer events.
Definition: timers.h:268
A portable counting semaphore class.
Definition: condition.h:655
An index container for maintaining an ordered list of objects.
Definition: linked.h:176