libtrbase  1.0.2
Web server and task management solution.
timer.h
Go to the documentation of this file.
1 /**
2  * \file
3  * A timer with second, millisec, microsec or nanosec granularity.
4  *
5  * \author Georg Hopp
6  *
7  * \copyright
8  * Copyright © 2014 Georg Hopp
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef __TR_TIMER_H__
25 #define __TR_TIMER_H__
26 
27 #include <time.h>
28 
29 #include "trbase.h"
30 
31 #ifdef _WIN32
32 struct timespec {
33  time_t tv_sec;
34  long tv_nsec;
35 };
36 
37 #define CLOCK_REALTIME 1
38 
39 int clock_gettime(int, struct timespec *tv);
40 #endif
41 
42 typedef enum {
48 
49 TR_CLASS(TR_Timer) {
50  int sec;
51  int nsec;
52  TR_eTimeoutBase base;
53  unsigned long timeout;
54 };
55 
56 TR_INSTANCE_INIT(TR_Timer);
57 TR_CLASSVARS_DECL(TR_Timer) {};
58 
59 void TR_timerSet(TR_Timer, TR_eTimeoutBase, unsigned long);
60 unsigned long TR_timerGet(TR_Timer, unsigned long *); // 2nd arg gets amount of
61  // missed since last check.
62 
63 #define TR_timerSetNan(timer, val) (TR_timerSet((timer), TR_TBASE_NAN, (val)))
64 #define TR_timerSetMic(timer, val) (TR_timerSet((timer), TR_TBASE_MIC, (val)))
65 #define TR_timerSetMil(timer, val) (TR_timerSet((timer), TR_TBASE_MIL, (val)))
66 #define TR_timerSetSec(timer, val) (TR_timerSet((timer), TR_TBASE_SEC, (val)))
67 
68 #endif // __TR_TIMER_H__
69 
70 // vim: set ts=4 sw=4:
unsigned long TR_timerGet(TR_Timer, unsigned long *)
Definition: timer_get.c:31
TR_CLASSVARS_DECL(TR_Timer)
Definition: timer.h:57
TR_INSTANCE_INIT(TR_Timer)
void TR_timerSet(TR_Timer, TR_eTimeoutBase, unsigned long)
Definition: timer_set.c:30
TR_eTimeoutBase
Definition: timer.h:42
TR_CLASS(TR_Timer)
Definition: timer.h:49