compat.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #ifndef __COMPAT_H__
  10. #define __COMPAT_H__
  11. #include "config.h"
  12. #ifdef WIN32
  13. #include <winsock2.h>
  14. #endif
  15. #include <stdbool.h>
  16. // NOTE: Nested preprocessor checks since the latter isn't defined at all without the former
  17. #ifdef HAVE_LIBUSB
  18. # if ! HAVE_DECL_LIBUSB_ERROR_NAME
  19. static char my_libusb_error_name_buf[0x10];
  20. # define libusb_error_name(x) (sprintf(my_libusb_error_name_buf, "%d", x), my_libusb_error_name_buf)
  21. # endif
  22. #endif
  23. #ifdef WIN32
  24. #include <errno.h>
  25. #include <time.h>
  26. #include <pthread.h>
  27. #include <sys/time.h>
  28. #include <windows.h>
  29. #ifndef __maybe_unused
  30. #define __maybe_unused __attribute__((unused))
  31. #endif
  32. #ifndef timersub
  33. #define timersub(a, b, result) \
  34. do { \
  35. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  36. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  37. if ((result)->tv_usec < 0) { \
  38. --(result)->tv_sec; \
  39. (result)->tv_usec += 1000000; \
  40. } \
  41. } while (0)
  42. #endif
  43. #ifndef timeradd
  44. # define timeradd(a, b, result) \
  45. do { \
  46. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  47. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  48. if ((result)->tv_usec >= 1000000) \
  49. { \
  50. ++(result)->tv_sec; \
  51. (result)->tv_usec -= 1000000; \
  52. } \
  53. } while (0)
  54. #endif
  55. // Some versions of MingW define this, but don't handle the timeval.tv_sec case that we use
  56. #ifdef localtime_r
  57. #undef localtime_r
  58. #endif
  59. // localtime is thread-safe on Windows
  60. // We also use this with timeval.tv_sec, which is incorrectly smaller than time_t on Windows
  61. // Need to cast to time_t* to suppress warning - actual problem shouldn't be possible in practice
  62. #define localtime_r(timep, result) ( \
  63. memcpy(result, \
  64. ( \
  65. (sizeof(*timep) == sizeof(time_t)) \
  66. ? localtime((time_t*)timep) \
  67. : localtime_convert(*timep) \
  68. ), \
  69. sizeof(*result) \
  70. ) \
  71. )
  72. static inline
  73. struct tm *localtime_convert(time_t t)
  74. {
  75. return localtime(&t);
  76. }
  77. #endif
  78. #ifndef HAVE_NANOSLEEP
  79. extern void cgtime(struct timeval *);
  80. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  81. {
  82. struct timeval tstart;
  83. DWORD msecs;
  84. cgtime(&tstart);
  85. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  86. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  87. if (rem) {
  88. struct timeval tdone, tnow, tleft;
  89. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  90. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  91. if (tdone.tv_usec > 1000000) {
  92. tdone.tv_usec -= 1000000;
  93. ++tdone.tv_sec;
  94. }
  95. cgtime(&tnow);
  96. if (timercmp(&tnow, &tdone, >))
  97. return 0;
  98. timersub(&tdone, &tnow, &tleft);
  99. rem->tv_sec = tleft.tv_sec;
  100. rem->tv_nsec = tleft.tv_usec * 1000;
  101. }
  102. errno = EINTR;
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. #endif
  108. #ifdef WIN32
  109. static inline int sleep(unsigned int secs)
  110. {
  111. struct timespec req, rem;
  112. req.tv_sec = secs;
  113. req.tv_nsec = 0;
  114. if (!nanosleep(&req, &rem))
  115. return 0;
  116. return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
  117. }
  118. enum {
  119. PRIO_PROCESS = 0,
  120. };
  121. static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
  122. {
  123. return -!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  124. }
  125. typedef unsigned long int ulong;
  126. typedef unsigned short int ushort;
  127. typedef unsigned int uint;
  128. #ifndef __SUSECONDS_T_TYPE
  129. typedef long suseconds_t;
  130. #endif
  131. #endif /* WIN32 */
  132. #ifndef HAVE_PTHREAD_CANCEL
  133. // Bionic (Android) is intentionally missing pthread_cancel, so it is implemented using pthread_kill (handled in util.c)
  134. #include <pthread.h>
  135. #include <signal.h>
  136. #define pthread_cancel(pth) pthread_kill(pth, SIGTERM)
  137. extern void pthread_testcancel(void);
  138. #ifndef PTHREAD_CANCEL_ENABLE
  139. #define PTHREAD_CANCEL_ENABLE 0
  140. #define PTHREAD_CANCEL_DISABLE 1
  141. #endif
  142. #ifndef PTHREAD_CANCEL_DEFERRED
  143. #define PTHREAD_CANCEL_DEFERRED 0
  144. #define PTHREAD_CANCEL_ASYNCHRONOUS 1
  145. #endif
  146. #ifndef PTHREAD_CANCELED
  147. #define PTHREAD_CANCELED ((void*)-1)
  148. #endif
  149. #endif
  150. #endif /* __COMPAT_H__ */