compat.h 4.1 KB

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