compat.h 6.4 KB

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