compat.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // NOTE: Nested preprocessor checks since the latter isn't defined at all without the former
  17. #ifdef HAVE_LIBUSB
  18. # if ! HAVE_DECL_LIBUSB_ERROR_NAME
  19. static char my_libusb_error_name_buf[0x10];
  20. # define libusb_error_name(x) (sprintf(my_libusb_error_name_buf, "%d", x), my_libusb_error_name_buf)
  21. # endif
  22. #endif
  23. #ifdef WIN32
  24. #include <errno.h>
  25. #include <time.h>
  26. #include <pthread.h>
  27. #include <sys/time.h>
  28. #include <windows.h>
  29. #ifndef __maybe_unused
  30. #define __maybe_unused __attribute__((unused))
  31. #endif
  32. #ifndef timersub
  33. #define timersub(a, b, result) \
  34. do { \
  35. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  36. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  37. if ((result)->tv_usec < 0) { \
  38. --(result)->tv_sec; \
  39. (result)->tv_usec += 1000000; \
  40. } \
  41. } while (0)
  42. #endif
  43. #ifndef timeradd
  44. # define timeradd(a, b, result) \
  45. do { \
  46. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  47. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  48. if ((result)->tv_usec >= 1000000) \
  49. { \
  50. ++(result)->tv_sec; \
  51. (result)->tv_usec -= 1000000; \
  52. } \
  53. } while (0)
  54. #endif
  55. // Some versions of MingW define this, but don't handle the timeval.tv_sec case that we use
  56. #ifdef localtime_r
  57. #undef localtime_r
  58. #endif
  59. // localtime is thread-safe on Windows
  60. // We also use this with timeval.tv_sec, which is incorrectly smaller than time_t on Windows
  61. // Need to cast to time_t* to suppress warning - actual problem shouldn't be possible in practice
  62. #define localtime_r(timep, result) ( \
  63. memcpy(result, \
  64. ( \
  65. (sizeof(*timep) == sizeof(time_t)) \
  66. ? localtime((time_t*)timep) \
  67. : localtime_convert(*timep) \
  68. ), \
  69. sizeof(*result) \
  70. ) \
  71. )
  72. static inline
  73. struct tm *localtime_convert(time_t t)
  74. {
  75. return localtime(&t);
  76. }
  77. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  78. {
  79. struct timeval tstart;
  80. DWORD msecs;
  81. gettimeofday(&tstart, NULL);
  82. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  83. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  84. if (rem) {
  85. struct timeval tdone, tnow, tleft;
  86. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  87. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  88. if (tdone.tv_usec > 1000000) {
  89. tdone.tv_usec -= 1000000;
  90. ++tdone.tv_sec;
  91. }
  92. gettimeofday(&tnow, NULL);
  93. if (timercmp(&tnow, &tdone, >))
  94. return 0;
  95. timersub(&tdone, &tnow, &tleft);
  96. rem->tv_sec = tleft.tv_sec;
  97. rem->tv_nsec = tleft.tv_usec * 1000;
  98. }
  99. errno = EINTR;
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. static inline int sleep(unsigned int secs)
  105. {
  106. struct timespec req, rem;
  107. req.tv_sec = secs;
  108. req.tv_nsec = 0;
  109. if (!nanosleep(&req, &rem))
  110. return 0;
  111. return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
  112. }
  113. enum {
  114. PRIO_PROCESS = 0,
  115. };
  116. static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
  117. {
  118. return -!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  119. }
  120. typedef unsigned long int ulong;
  121. typedef unsigned short int ushort;
  122. typedef unsigned int uint;
  123. #ifndef __SUSECONDS_T_TYPE
  124. typedef long suseconds_t;
  125. #endif
  126. #define PTH(thr) ((thr)->pth.p)
  127. #else
  128. #define PTH(thr) ((thr)->pth)
  129. #endif /* WIN32 */
  130. #endif /* __COMPAT_H__ */