compat.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 cgtime(struct timeval *);
  77. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  78. {
  79. struct timeval tstart;
  80. DWORD msecs;
  81. cgtime(&tstart);
  82. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  83. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  84. if (rem) {
  85. struct timeval tdone, tnow, tleft;
  86. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  87. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  88. if (tdone.tv_usec > 1000000) {
  89. tdone.tv_usec -= 1000000;
  90. ++tdone.tv_sec;
  91. }
  92. cgtime(&tnow);
  93. if (timercmp(&tnow, &tdone, >))
  94. return 0;
  95. timersub(&tdone, &tnow, &tleft);
  96. rem->tv_sec = tleft.tv_sec;
  97. rem->tv_nsec = tleft.tv_usec * 1000;
  98. }
  99. errno = EINTR;
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. #endif
  105. #ifdef WIN32
  106. static inline int sleep(unsigned int secs)
  107. {
  108. struct timespec req, rem;
  109. req.tv_sec = secs;
  110. req.tv_nsec = 0;
  111. if (!nanosleep(&req, &rem))
  112. return 0;
  113. return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
  114. }
  115. enum {
  116. PRIO_PROCESS = 0,
  117. };
  118. static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
  119. {
  120. return -!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  121. }
  122. typedef unsigned long int ulong;
  123. typedef unsigned short int ushort;
  124. typedef unsigned int uint;
  125. #ifndef __SUSECONDS_T_TYPE
  126. typedef long suseconds_t;
  127. #endif
  128. #endif /* WIN32 */
  129. #ifndef HAVE_PTHREAD_CANCEL
  130. // Bionic (Android) is intentionally missing pthread_cancel, so it is implemented using pthread_kill (handled in util.c)
  131. #include <pthread.h>
  132. #include <signal.h>
  133. #define pthread_cancel(pth) pthread_kill(pth, SIGTERM)
  134. extern void pthread_testcancel(void);
  135. #ifndef PTHREAD_CANCEL_ENABLE
  136. #define PTHREAD_CANCEL_ENABLE 0
  137. #define PTHREAD_CANCEL_DISABLE 1
  138. #endif
  139. #ifndef PTHREAD_CANCEL_DEFERRED
  140. #define PTHREAD_CANCEL_DEFERRED 0
  141. #define PTHREAD_CANCEL_ASYNCHRONOUS 1
  142. #endif
  143. #ifndef PTHREAD_CANCELED
  144. #define PTHREAD_CANCELED ((void*)-1)
  145. #endif
  146. #endif
  147. #endif /* __COMPAT_H__ */