wlt: fix shl_hook API changes
[platform/upstream/kmscon.git] / src / shl_timer.h
1 /*
2  * shl - Timers
3  *
4  * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5  * Copyright (c) 2011 University of Tuebingen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files
9  * (the "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 /*
28  * Timers
29  */
30
31 #ifndef SHL_TIMER_H
32 #define SHL_TIMER_H
33
34 #include <errno.h>
35 #include <stdbool.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <time.h>
40
41 struct shl_timer {
42         struct timespec start;
43         uint64_t elapsed;
44 };
45
46 static inline void shl_timer_reset(struct shl_timer *timer)
47 {
48         if (!timer)
49                 return;
50
51         clock_gettime(CLOCK_MONOTONIC, &timer->start);
52         timer->elapsed = 0;
53 }
54
55 static inline int shl_timer_new(struct shl_timer **out)
56 {
57         struct shl_timer *timer;
58
59         if (!out)
60                 return -EINVAL;
61
62         timer = malloc(sizeof(*timer));
63         if (!timer)
64                 return -ENOMEM;
65         memset(timer, 0, sizeof(*timer));
66         shl_timer_reset(timer);
67
68         *out = timer;
69         return 0;
70 }
71
72 static inline void shl_timer_free(struct shl_timer *timer)
73 {
74         if (!timer)
75                 return;
76
77         free(timer);
78 }
79
80 static inline void shl_timer_start(struct shl_timer *timer)
81 {
82         if (!timer)
83                 return;
84
85         clock_gettime(CLOCK_MONOTONIC, &timer->start);
86 }
87
88 static inline uint64_t shl_timer_stop(struct shl_timer *timer)
89 {
90         struct timespec spec;
91         int64_t off, nsec;
92
93         if (!timer)
94                 return 0;
95
96         clock_gettime(CLOCK_MONOTONIC, &spec);
97         off = spec.tv_sec - timer->start.tv_sec;
98         nsec = spec.tv_nsec - timer->start.tv_nsec;
99         if (nsec < 0) {
100                 --off;
101                 nsec += 1000000000ULL;
102         }
103         off *= 1000000;
104         off += nsec / 1000;
105
106         memcpy(&timer->start, &spec, sizeof(spec));
107         timer->elapsed += off;
108         return timer->elapsed;
109 }
110
111 static inline uint64_t shl_timer_elapsed(struct shl_timer *timer)
112 {
113         struct timespec spec;
114         int64_t off, nsec;
115
116         if (!timer)
117                 return 0;
118
119         clock_gettime(CLOCK_MONOTONIC, &spec);
120         off = spec.tv_sec - timer->start.tv_sec;
121         nsec = spec.tv_nsec - timer->start.tv_nsec;
122         if (nsec < 0) {
123                 --off;
124                 nsec += 1000000000ULL;
125         }
126         off *= 1000000;
127         off += nsec / 1000;
128
129         return timer->elapsed + off;
130 }
131
132 #endif /* SHL_TIMER_H */