compat.h 6.4 KB

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