compat.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // Some versions of MingW define this, but don't handle the timeval.tv_sec case that we use
  48. #ifdef localtime_r
  49. #undef localtime_r
  50. #endif
  51. // localtime is thread-safe on Windows
  52. // We also use this with timeval.tv_sec, which is incorrectly smaller than time_t on Windows
  53. // Need to cast to time_t* to suppress warning - actual problem shouldn't be possible in practice
  54. #define localtime_r(timep, result) ( \
  55. memcpy(result, \
  56. ( \
  57. (sizeof(*timep) == sizeof(time_t)) \
  58. ? localtime((time_t*)timep) \
  59. : localtime_convert(*timep) \
  60. ), \
  61. sizeof(*result) \
  62. ) \
  63. )
  64. static inline
  65. struct tm *localtime_convert(time_t t)
  66. {
  67. return localtime(&t);
  68. }
  69. #endif
  70. #ifndef HAVE_NANOSLEEP
  71. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  72. {
  73. struct timeval tstart;
  74. DWORD msecs;
  75. gettimeofday(&tstart, NULL);
  76. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  77. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  78. if (rem) {
  79. struct timeval tdone, tnow, tleft;
  80. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  81. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  82. if (tdone.tv_usec > 1000000) {
  83. tdone.tv_usec -= 1000000;
  84. ++tdone.tv_sec;
  85. }
  86. gettimeofday(&tnow, NULL);
  87. if (timercmp(&tnow, &tdone, >))
  88. return 0;
  89. timersub(&tdone, &tnow, &tleft);
  90. rem->tv_sec = tleft.tv_sec;
  91. rem->tv_nsec = tleft.tv_usec * 1000;
  92. }
  93. errno = EINTR;
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. #endif
  99. #ifdef WIN32
  100. static inline int sleep(unsigned int secs)
  101. {
  102. struct timespec req, rem;
  103. req.tv_sec = secs;
  104. req.tv_nsec = 0;
  105. if (!nanosleep(&req, &rem))
  106. return 0;
  107. return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
  108. }
  109. enum {
  110. PRIO_PROCESS = 0,
  111. };
  112. static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
  113. {
  114. return -!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  115. }
  116. typedef unsigned long int ulong;
  117. typedef unsigned short int ushort;
  118. typedef unsigned int uint;
  119. #ifndef __SUSECONDS_T_TYPE
  120. typedef long suseconds_t;
  121. #endif
  122. #endif /* WIN32 */
  123. #ifndef HAVE_PTHREAD_CANCEL
  124. // Bionic (Android) is intentionally missing pthread_cancel, so it is implemented using pthread_kill (handled in util.c)
  125. #include <pthread.h>
  126. #include <signal.h>
  127. #define pthread_cancel(pth) pthread_kill(pth, SIGTERM)
  128. #ifndef PTHREAD_CANCEL_ENABLE
  129. #define PTHREAD_CANCEL_ENABLE 0
  130. #define PTHREAD_CANCEL_DISABLE 1
  131. #endif
  132. #ifndef PTHREAD_CANCEL_DEFERRED
  133. #define PTHREAD_CANCEL_DEFERRED 0
  134. #define PTHREAD_CANCEL_ASYNCHRONOUS 1
  135. #endif
  136. #ifndef PTHREAD_CANCELED
  137. #define PTHREAD_CANCELED ((void*)-1)
  138. #endif
  139. #endif
  140. #endif /* __COMPAT_H__ */