xref: /freebsd/sys/contrib/zstd/programs/timefn.h (revision e17f5b1d)
1 /*
2  * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10 
11 #ifndef TIME_FN_H_MODULE_287987
12 #define TIME_FN_H_MODULE_287987
13 
14 #if defined (__cplusplus)
15 extern "C" {
16 #endif
17 
18 
19 /*-****************************************
20 *  Dependencies
21 ******************************************/
22 #include <time.h>         /* clock_t, clock, CLOCKS_PER_SEC */
23 
24 
25 
26 /*-****************************************
27 *  Local Types
28 ******************************************/
29 
30 #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
31 # include <stdint.h>
32   typedef uint64_t           PTime;  /* Precise Time */
33 #else
34   typedef unsigned long long PTime;  /* does not support compilers without long long support */
35 #endif
36 
37 
38 
39 /*-****************************************
40 *  Time functions
41 ******************************************/
42 #if defined(_WIN32)   /* Windows */
43 
44     #include <windows.h>   /* LARGE_INTEGER */
45     typedef LARGE_INTEGER UTIL_time_t;
46     #define UTIL_TIME_INITIALIZER { { 0, 0 } }
47 
48 #elif defined(__APPLE__) && defined(__MACH__)
49 
50     #include <mach/mach_time.h>
51     typedef PTime UTIL_time_t;
52     #define UTIL_TIME_INITIALIZER 0
53 
54 /* C11 requires timespec_get, but FreeBSD 11 lacks it, while still claiming C11 compliance.
55    Android also lacks it but does define TIME_UTC. */
56 #elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \
57     && defined(TIME_UTC) && !defined(__ANDROID__)
58 
59     typedef struct timespec UTIL_time_t;
60     #define UTIL_TIME_INITIALIZER { 0, 0 }
61 
62 #else   /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
63 
64     typedef clock_t UTIL_time_t;
65     #define UTIL_TIME_INITIALIZER 0
66 
67 #endif
68 
69 
70 UTIL_time_t UTIL_getTime(void);
71 PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
72 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd);
73 
74 #define SEC_TO_MICRO ((PTime)1000000)
75 PTime UTIL_clockSpanMicro(UTIL_time_t clockStart);
76 PTime UTIL_clockSpanNano(UTIL_time_t clockStart);
77 
78 void UTIL_waitForNextTick(void);
79 
80 
81 #if defined (__cplusplus)
82 }
83 #endif
84 
85 #endif /* TIME_FN_H_MODULE_287987 */
86