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