-
Notifications
You must be signed in to change notification settings - Fork 0
/
ail_time.h
111 lines (93 loc) · 2.97 KB
/
ail_time.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Time-related functions
//
// LICENSE
/*
Copyright (c) 2024 Lily Val Richter
Permission is hereby granted, free_one of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef AIL_TIME_H_
#define AIL_TIME_H_
#include "ail.h"
#include <time.h> // For clock
#ifndef AIL_TIME_DEF
#ifdef AIL_DEF
#define AIL_TIME_DEF AIL_DEF
#else
#define AIL_TIME_DEF
#endif // AIL_DEF
#endif // AIL_TIME_DEF
#ifndef AIL_TIME_DEF_INLINE
#ifdef AIL_DEF_INLINE
#define AIL_TIME_DEF_INLINE AIL_DEF_INLINE
#else
#define AIL_TIME_DEF_INLINE static inline
#endif // AIL_DEF_INLINE
#endif // AIL_TIME_DEF_INLINE
// Sleep `msecs` milliseconds
AIL_TIME_DEF void ail_time_sleep(u64 msecs);
// Time in seconds here
AIL_TIME_DEF_INLINE f64 ail_time_clock_start(void);
AIL_TIME_DEF_INLINE f64 ail_time_clock_elapsed(f64 start);
#endif // AIL_TIME_H_
#if !defined(AIL_NO_TIME_IMPL) && !defined(AIL_NO_IMPL)
#ifndef _AIL_TIME_GUARD_
#define _AIL_TIME_GUARD_
#if AIL_OS_WIN
# include <windows.h> // For Sleep
# define AIL_TIME_FLAG_WINSLEEP
#elif _POSIX_C_SOURCE >= 199309L
# define AIL_TIME_FLAG_NANOSLEEP
# include <time.h> // for nanosleep
#else
# define AIL_TIME_FLAG_USLEEP
# include <unistd.h> // for usleep
#endif
void ail_time_sleep(u64 msecs)
{
#if defined(AIL_TIME_FLAG_NANOSLEEP)
struct timespec ts;
ts.tv_sec = msecs / 1000;
ts.tv_nsec = (msecs % 1000) * 1000000;
nanosleep(&ts, NULL);
#elif defined(AIL_TIME_FLAG_WINSLEEP)
Sleep(msecs);
#elif defined(AIL_TIME_FLAG_USLEEP)
if (msecs >= 1000) sleep(msecs / 1000);
usleep((msecs % 1000) * 1000);
#else
# error "Missing feature flag not implemented yet"
#endif
}
f64 ail_time_clock_start(void)
{
#if AIL_OS_WIN
return (f64)timeGetTime() / 1000;
#elif AIL_OS_POSIX
struct timespec ts = {0};
int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
AIL_ASSERT(ret == 0);
return (f64)ts.tv_sec + ts.tv_nsec*1e-9;
#else
return (f64)clock() / CLOCKS_PER_SEC;
#endif
}
f64 ail_time_clock_elapsed(f64 start)
{
return ail_time_clock_start() - start;
}
#endif // _AIL_TIME_GUARD_
#endif // AIL_NO_TIME_IMPL