compat.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #if !(defined(WIN32) || defined(unix))
  17. #define unix
  18. #endif
  19. #ifdef WIN32
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <time.h>
  23. #include <pthread.h>
  24. #include <sys/time.h>
  25. #include <windows.h>
  26. #ifndef __maybe_unused
  27. #define __maybe_unused __attribute__((unused))
  28. #endif
  29. #ifndef timersub
  30. #define timersub(a, b, result) \
  31. do { \
  32. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  33. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  34. if ((result)->tv_usec < 0) { \
  35. --(result)->tv_sec; \
  36. (result)->tv_usec += 1000000; \
  37. } \
  38. } while (0)
  39. #endif
  40. #ifndef timeradd
  41. # define timeradd(a, b, result) \
  42. do { \
  43. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  44. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  45. if ((result)->tv_usec >= 1000000) \
  46. { \
  47. ++(result)->tv_sec; \
  48. (result)->tv_usec -= 1000000; \
  49. } \
  50. } while (0)
  51. #endif
  52. // Some versions of MingW define this, but don't handle the timeval.tv_sec case that we use
  53. #ifdef localtime_r
  54. #undef localtime_r
  55. #endif
  56. // localtime is thread-safe on Windows
  57. // We also use this with timeval.tv_sec, which is incorrectly smaller than time_t on Windows
  58. // Need to cast to time_t* to suppress warning - actual problem shouldn't be possible in practice
  59. #define localtime_r(timep, result) ( \
  60. memcpy(result, \
  61. ( \
  62. (sizeof(*timep) == sizeof(time_t)) \
  63. ? localtime((time_t*)timep) \
  64. : localtime_convert(*timep) \
  65. ), \
  66. sizeof(*result) \
  67. ) \
  68. )
  69. static inline
  70. struct tm *localtime_convert(time_t t)
  71. {
  72. return localtime(&t);
  73. }
  74. #endif
  75. #ifndef HAVE_NANOSLEEP
  76. extern void (*timer_set_now)(struct timeval *);
  77. #define cgtime(tvp) timer_set_now(tvp)
  78. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  79. {
  80. struct timeval tstart;
  81. DWORD msecs;
  82. cgtime(&tstart);
  83. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  84. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  85. if (rem) {
  86. struct timeval tdone, tnow, tleft;
  87. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  88. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  89. if (tdone.tv_usec > 1000000) {
  90. tdone.tv_usec -= 1000000;
  91. ++tdone.tv_sec;
  92. }
  93. cgtime(&tnow);
  94. if (timercmp(&tnow, &tdone, >))
  95. return 0;
  96. timersub(&tdone, &tnow, &tleft);
  97. rem->tv_sec = tleft.tv_sec;
  98. rem->tv_nsec = tleft.tv_usec * 1000;
  99. }
  100. errno = EINTR;
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. #undef cgtime
  106. #endif
  107. #ifndef HAVE_SLEEP
  108. static inline int sleep(unsigned int secs)
  109. {
  110. struct timespec req, rem;
  111. req.tv_sec = secs;
  112. req.tv_nsec = 0;
  113. if (!nanosleep(&req, &rem))
  114. return 0;
  115. return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
  116. }
  117. #endif
  118. #ifdef WIN32
  119. enum {
  120. PRIO_PROCESS = 0,
  121. };
  122. static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
  123. {
  124. return -!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  125. }
  126. typedef unsigned long int ulong;
  127. typedef unsigned short int ushort;
  128. typedef unsigned int uint;
  129. #ifndef __SUSECONDS_T_TYPE
  130. typedef long suseconds_t;
  131. #endif
  132. #endif /* WIN32 */
  133. #ifndef HAVE_PTHREAD_CANCEL
  134. // Bionic (Android) is intentionally missing pthread_cancel, so it is implemented using pthread_kill (handled in util.c)
  135. #include <pthread.h>
  136. #include <signal.h>
  137. #define pthread_cancel(pth) pthread_kill(pth, SIGTERM)
  138. extern void pthread_testcancel(void);
  139. #ifndef PTHREAD_CANCEL_ENABLE
  140. #define PTHREAD_CANCEL_ENABLE 0
  141. #define PTHREAD_CANCEL_DISABLE 1
  142. #endif
  143. #ifndef PTHREAD_CANCEL_DEFERRED
  144. #define PTHREAD_CANCEL_DEFERRED 0
  145. #define PTHREAD_CANCEL_ASYNCHRONOUS 1
  146. #endif
  147. #ifndef PTHREAD_CANCELED
  148. #define PTHREAD_CANCELED ((void*)-1)
  149. #endif
  150. #endif
  151. #endif /* __COMPAT_H__ */