Pico Headers
Loading...
Searching...
No Matches
pico_time.h
Go to the documentation of this file.
1
44#ifndef _POSIX_C_SOURCE
45#define _POSIX_C_SOURCE 199309L
46#endif
47
48#ifndef PICO_TIME_H
49#define PICO_TIME_H
50
51#include <stdint.h>
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
60typedef uint64_t ptime_t;
61
66
73void pt_sleep(ptime_t duration);
74
78int64_t pt_to_usec(ptime_t time);
79
83int32_t pt_to_msec(ptime_t time);
84
88double pt_to_sec(ptime_t time);
89
93ptime_t pt_from_usec(int64_t usec);
94
98ptime_t pt_from_msec(int32_t msec);
99
104
105#ifdef __cplusplus
106}
107#endif
108
109#endif // PICO_TIME_H
110
111#ifdef PICO_TIME_IMPLEMENTATION
112
113#define PT_WINDOWS 1
114#define PT_APPLE 2
115#define PT_UNIX 3
116
117#if defined(_WIN32) || defined(_WIN64) || defined (__CYGWIN__)
118 #define PT_PLATFORM PT_WINDOWS
119#elif defined(__APPLE__) && defined(__MACH__)
120 #define PT_PLATFORM PT_APPLE
121#elif defined(__unix__)
122 #define PT_PLATFORM PT_UNIX
123#else
124 #error "Unsupported platform"
125#endif
126
127/*==============================================================================
128 * Windows (pt_now/pt_sleep)
129 *============================================================================*/
130
131#if PT_PLATFORM == PT_WINDOWS
132
133#include <windows.h>
134
135ptime_t pt_now(void)
136{
137 static LARGE_INTEGER freq = { 0 };
138
139 if (freq.QuadPart == 0)
140 QueryPerformanceFrequency(&freq);
141
142 LARGE_INTEGER ticks;
143 QueryPerformanceCounter(&ticks);
144
145 return (1000000UL * ticks.QuadPart) / freq.QuadPart;
146}
147
148void pt_sleep(ptime_t duration)
149{
150 TIMECAPS tc;
151 timeGetDevCaps(&tc, sizeof(TIMECAPS));
152
153 timeBeginPeriod(tc.wPeriodMin);
154 Sleep(pt_to_msec(duration));
155 timeEndPeriod(tc.wPeriodMin);
156}
157
158/*==============================================================================
159 * Apple (pt_now)
160 *============================================================================*/
161
162#elif PT_PLATFORM == PT_APPLE
163
164#include <mach/mach_time.h>
165
166ptime_t pt_now(void)
167{
168 static mach_timebase_info_data_t freq = { 0, 0 };
169
170 if (freq.denom == 0)
171 mach_timebase_info(&freq);
172
173 uint64_t nsec = (mach_absolute_time() * freq.numer) / freq.denom;
174 return nsec / 1000;
175}
176
177/*==============================================================================
178 * Unix (pt_now)
179 *============================================================================*/
180
181#elif PT_PLATFORM == PT_UNIX
182
183#include <errno.h>
184#include <time.h>
185
186ptime_t pt_now(void)
187{
188 struct timespec ti;
189 clock_gettime(CLOCK_MONOTONIC, &ti);
190 return ti.tv_sec * 1000000UL + ti.tv_nsec / 1000;
191}
192
193#endif // PT_PLATFORM
194
195/*==============================================================================
196 * Unix and Apple (pt_sleep)
197 *============================================================================*/
198
199#if PT_PLATFORM == PT_UNIX || PT_PLATFORM == PT_APPLE
200
201#include <errno.h>
202#include <time.h>
203
204void pt_sleep(ptime_t duration)
205{
206 struct timespec ti;
207 ti.tv_sec = duration / 1000000;
208 ti.tv_nsec = (duration % 1000000) * 1000;
209
210 while ((nanosleep(&ti, &ti) == -1) && (errno == EINTR));
211}
212
213#endif // PT_PLATFORM
214
215int64_t pt_to_usec(ptime_t time)
216{
217 return time;
218}
219
220int32_t pt_to_msec(ptime_t time)
221{
222 return time / 1000;
223}
224
225double pt_to_sec(ptime_t time)
226{
227 return time / 1000000.0;
228}
229
230ptime_t pt_from_usec(int64_t usec)
231{
232 return usec;
233}
234
235ptime_t pt_from_msec(int32_t msec)
236{
237 return msec * 1000;
238}
239
240ptime_t pt_from_sec(double sec)
241{
242 return (ptime_t)(sec * 1000000.0 + 0.5);
243}
244
245#endif // PICO_TIME_IMPLEMENTATION
246
247/*
248 ----------------------------------------------------------------------------
249 This software is available under two licenses (A) or (B). You may choose
250 either one as you wish:
251 ----------------------------------------------------------------------------
252
253 (A) The zlib License
254
255 Copyright (c) 2021 James McLean
256
257 This software is provided 'as-is', without any express or implied warranty.
258 In no event will the authors be held liable for any damages arising from the
259 use of this software.
260
261 Permission is granted to anyone to use this software for any purpose,
262 including commercial applications, and to alter it and redistribute it
263 freely, subject to the following restrictions:
264
265 1. The origin of this software must not be misrepresented; you must not
266 claim that you wrote the original software. If you use this software in a
267 product, an acknowledgment in the product documentation would be appreciated
268 but is not required.
269
270 2. Altered source versions must be plainly marked as such, and must not be
271 misrepresented as being the original software.
272
273 3. This notice may not be removed or altered from any source distribution.
274
275 ----------------------------------------------------------------------------
276
277 (B) Public Domain (www.unlicense.org)
278
279 This is free and unencumbered software released into the public domain.
280
281 Anyone is free to copy, modify, publish, use, compile, sell, or distribute
282 this software, either in source code form or as a compiled binary, for any
283 purpose, commercial or non-commercial, and by any means.
284
285 In jurisdictions that recognize copyright laws, the author or authors of
286 this software dedicate any and all copyright interest in the software to the
287 public domain. We make this dedication for the benefit of the public at
288 large and to the detriment of our heirs and successors. We intend this
289 dedication to be an overt act of relinquishment in perpetuity of all present
290 and future rights to this software under copyright law.
291
292 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
293 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
294 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
295 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
296 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
297 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
298*/
299
ptime_t pt_now(void)
Returns the present high-res clock time.
int32_t pt_to_msec(ptime_t time)
Converts time to milliseconds.
int64_t pt_to_usec(ptime_t time)
Converts time to microseconds.
ptime_t pt_from_usec(int64_t usec)
Make time from microseconds.
double pt_to_sec(ptime_t time)
Converts time to seconds.
uint64_t ptime_t
Time value expressed in microseconds.
Definition pico_time.h:60
ptime_t pt_from_msec(int32_t msec)
Make time from miliseconds.
ptime_t pt_from_sec(double sec)
Make time from seconds.
void pt_sleep(ptime_t duration)
Sleeps for at least the specified duration.