compat.h 4.2 KB

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