xref: /freebsd/sys/contrib/zstd/programs/timefn.h (revision da5069e1f7daaef1e7157876d6044de6f3a08ce2)
1 /*
2  * Copyright (c) 2016-present, 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 <sys/types.h>    /* utime */
23 #if defined(_MSC_VER)
24 #  include <sys/utime.h>  /* utime */
25 #else
26 #  include <utime.h>      /* utime */
27 #endif
28 #include <time.h>         /* clock_t, clock, CLOCKS_PER_SEC */
29 
30 
31 
32 /*-****************************************
33 *  Local Types
34 ******************************************/
35 
36 #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
37 # include <stdint.h>
38   typedef uint64_t           PTime;  /* Precise Time */
39 #else
40   typedef unsigned long long PTime;  /* does not support compilers without long long support */
41 #endif
42 
43 
44 
45 /*-****************************************
46 *  Time functions
47 ******************************************/
48 #if defined(_WIN32)   /* Windows */
49 
50     #include <Windows.h>   /* LARGE_INTEGER */
51     typedef LARGE_INTEGER UTIL_time_t;
52     #define UTIL_TIME_INITIALIZER { { 0, 0 } }
53 
54 #elif defined(__APPLE__) && defined(__MACH__)
55 
56     #include <mach/mach_time.h>
57     typedef PTime UTIL_time_t;
58     #define UTIL_TIME_INITIALIZER 0
59 
60 #elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \
61     && defined(TIME_UTC) /* C11 requires timespec_get, but FreeBSD 11 lacks it, while still claiming C11 compliance */
62 
63     typedef struct timespec UTIL_time_t;
64     #define UTIL_TIME_INITIALIZER { 0, 0 }
65 
66 #else   /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
67 
68     typedef clock_t UTIL_time_t;
69     #define UTIL_TIME_INITIALIZER 0
70 
71 #endif
72 
73 
74 UTIL_time_t UTIL_getTime(void);
75 PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
76 PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd);
77 
78 #define SEC_TO_MICRO ((PTime)1000000)
79 PTime UTIL_clockSpanMicro(UTIL_time_t clockStart);
80 PTime UTIL_clockSpanNano(UTIL_time_t clockStart);
81 
82 void UTIL_waitForNextTick(void);
83 
84 
85 #if defined (__cplusplus)
86 }
87 #endif
88 
89 #endif /* TIME_FN_H_MODULE_287987 */
90