timefn.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 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. #ifndef TIME_FN_H_MODULE_287987
  11. #define TIME_FN_H_MODULE_287987
  12. #if defined (__cplusplus)
  13. extern "C" {
  14. #endif
  15. /*-****************************************
  16. * Dependencies
  17. ******************************************/
  18. #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
  19. /*-****************************************
  20. * Local Types
  21. ******************************************/
  22. #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  23. # if defined(_AIX)
  24. # include <inttypes.h>
  25. # else
  26. # include <stdint.h> /* intptr_t */
  27. # endif
  28. typedef uint64_t PTime; /* Precise Time */
  29. #else
  30. typedef unsigned long long PTime; /* does not support compilers without long long support */
  31. #endif
  32. /*-****************************************
  33. * Time functions
  34. ******************************************/
  35. #if defined(_WIN32) /* Windows */
  36. #include <windows.h> /* LARGE_INTEGER */
  37. typedef LARGE_INTEGER UTIL_time_t;
  38. #define UTIL_TIME_INITIALIZER { { 0, 0 } }
  39. #elif defined(__APPLE__) && defined(__MACH__)
  40. #include <mach/mach_time.h>
  41. typedef PTime UTIL_time_t;
  42. #define UTIL_TIME_INITIALIZER 0
  43. /* C11 requires timespec_get, but FreeBSD 11 lacks it, while still claiming C11 compliance.
  44. Android also lacks it but does define TIME_UTC. */
  45. #elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \
  46. && defined(TIME_UTC) && !defined(__ANDROID__)
  47. typedef struct timespec UTIL_time_t;
  48. #define UTIL_TIME_INITIALIZER { 0, 0 }
  49. #else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
  50. typedef clock_t UTIL_time_t;
  51. #define UTIL_TIME_INITIALIZER 0
  52. #endif
  53. UTIL_time_t UTIL_getTime(void);
  54. PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
  55. PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd);
  56. #define SEC_TO_MICRO ((PTime)1000000)
  57. PTime UTIL_clockSpanMicro(UTIL_time_t clockStart);
  58. PTime UTIL_clockSpanNano(UTIL_time_t clockStart);
  59. void UTIL_waitForNextTick(void);
  60. #if defined (__cplusplus)
  61. }
  62. #endif
  63. #endif /* TIME_FN_H_MODULE_287987 */