4 * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5 * Copyright (c) 2011 University of Tuebingen
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
42 struct timespec start;
46 static inline void shl_timer_reset(struct shl_timer *timer)
51 clock_gettime(CLOCK_MONOTONIC, &timer->start);
55 static inline int shl_timer_new(struct shl_timer **out)
57 struct shl_timer *timer;
62 timer = malloc(sizeof(*timer));
65 memset(timer, 0, sizeof(*timer));
66 shl_timer_reset(timer);
72 static inline void shl_timer_free(struct shl_timer *timer)
80 static inline void shl_timer_start(struct shl_timer *timer)
85 clock_gettime(CLOCK_MONOTONIC, &timer->start);
88 static inline uint64_t shl_timer_stop(struct shl_timer *timer)
96 clock_gettime(CLOCK_MONOTONIC, &spec);
97 off = spec.tv_sec - timer->start.tv_sec;
98 nsec = spec.tv_nsec - timer->start.tv_nsec;
101 nsec += 1000000000ULL;
106 memcpy(&timer->start, &spec, sizeof(spec));
107 timer->elapsed += off;
108 return timer->elapsed;
111 static inline uint64_t shl_timer_elapsed(struct shl_timer *timer)
113 struct timespec spec;
119 clock_gettime(CLOCK_MONOTONIC, &spec);
120 off = spec.tv_sec - timer->start.tv_sec;
121 nsec = spec.tv_nsec - timer->start.tv_nsec;
124 nsec += 1000000000ULL;
129 return timer->elapsed + off;
132 #endif /* SHL_TIMER_H */