compat.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef __COMPAT_H__
  2. #define __COMPAT_H__
  3. #include "config.h"
  4. #ifdef WIN32
  5. #include <winsock2.h>
  6. #endif
  7. #include <stdbool.h>
  8. // NOTE: Nested preprocessor checks since the latter isn't defined at all without the former
  9. #ifdef HAVE_LIBUSB
  10. # if ! HAVE_DECL_LIBUSB_ERROR_NAME
  11. static char my_libusb_error_name_buf[0x10];
  12. # define libusb_error_name(x) (sprintf(my_libusb_error_name_buf, "%d", x), my_libusb_error_name_buf)
  13. # endif
  14. #endif
  15. #ifdef WIN32
  16. #include <errno.h>
  17. #include <time.h>
  18. #include <pthread.h>
  19. #include <sys/time.h>
  20. #include <windows.h>
  21. #ifndef __maybe_unused
  22. #define __maybe_unused __attribute__((unused))
  23. #endif
  24. #ifndef timersub
  25. #define timersub(a, b, result) \
  26. do { \
  27. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  28. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  29. if ((result)->tv_usec < 0) { \
  30. --(result)->tv_sec; \
  31. (result)->tv_usec += 1000000; \
  32. } \
  33. } while (0)
  34. #endif
  35. #ifndef timeradd
  36. # define timeradd(a, b, result) \
  37. do { \
  38. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  39. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  40. if ((result)->tv_usec >= 1000000) \
  41. { \
  42. ++(result)->tv_sec; \
  43. (result)->tv_usec -= 1000000; \
  44. } \
  45. } while (0)
  46. #endif
  47. // localtime is thread-safe on Windows
  48. // We also use this with timeval.tv_sec, which is incorrectly smaller than time_t on Windows
  49. // Need to cast to time_t* to suppress warning - actual problem shouldn't be possible in practice
  50. #define localtime_r(timep, result) ( \
  51. memcpy(result, \
  52. ( \
  53. (sizeof(*timep) == sizeof(time_t)) \
  54. ? localtime((time_t*)timep) \
  55. : localtime_convert(*timep) \
  56. ), \
  57. sizeof(*result) \
  58. ) \
  59. )
  60. static inline
  61. struct tm *localtime_convert(time_t t)
  62. {
  63. return localtime(&t);
  64. }
  65. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  66. {
  67. struct timeval tstart;
  68. DWORD msecs;
  69. gettimeofday(&tstart, NULL);
  70. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  71. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  72. if (rem) {
  73. struct timeval tdone, tnow, tleft;
  74. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  75. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  76. if (tdone.tv_usec > 1000000) {
  77. tdone.tv_usec -= 1000000;
  78. ++tdone.tv_sec;
  79. }
  80. gettimeofday(&tnow, NULL);
  81. if (timercmp(&tnow, &tdone, >))
  82. return 0;
  83. timersub(&tdone, &tnow, &tleft);
  84. rem->tv_sec = tleft.tv_sec;
  85. rem->tv_nsec = tleft.tv_usec * 1000;
  86. }
  87. errno = EINTR;
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. static inline int sleep(unsigned int secs)
  93. {
  94. struct timespec req, rem;
  95. req.tv_sec = secs;
  96. req.tv_nsec = 0;
  97. if (!nanosleep(&req, &rem))
  98. return 0;
  99. return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
  100. }
  101. enum {
  102. PRIO_PROCESS = 0,
  103. };
  104. static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
  105. {
  106. return -!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  107. }
  108. typedef unsigned long int ulong;
  109. typedef unsigned short int ushort;
  110. typedef unsigned int uint;
  111. #ifndef __SUSECONDS_T_TYPE
  112. typedef long suseconds_t;
  113. #endif
  114. #define PTH(thr) ((thr)->pth.p)
  115. #else
  116. #define PTH(thr) ((thr)->pth)
  117. #endif /* WIN32 */
  118. #endif /* __COMPAT_H__ */