compat.h 5.0 KB

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