compat.h 5.0 KB

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