compat.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef __COMPAT_H__
  2. #define __COMPAT_H__
  3. #include "config.h"
  4. #ifdef WIN32
  5. #include <winsock2.h>
  6. #endif
  7. #include <stdbool.h>
  8. // NOTE: Nested preprocessor checks since the latter isn't defined at all without the former
  9. #ifdef HAVE_LIBUSB
  10. # if ! HAVE_DECL_LIBUSB_ERROR_NAME
  11. static char my_libusb_error_name_buf[0x10];
  12. # define libusb_error_name(x) (sprintf(my_libusb_error_name_buf, "%d", x), my_libusb_error_name_buf)
  13. # endif
  14. #endif
  15. #ifdef WIN32
  16. #include <errno.h>
  17. #include <time.h>
  18. #include <pthread.h>
  19. #include <sys/time.h>
  20. #include <windows.h>
  21. #ifndef __maybe_unused
  22. #define __maybe_unused __attribute__((unused))
  23. #endif
  24. #ifndef timersub
  25. #define timersub(a, b, result) \
  26. do { \
  27. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  28. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  29. if ((result)->tv_usec < 0) { \
  30. --(result)->tv_sec; \
  31. (result)->tv_usec += 1000000; \
  32. } \
  33. } while (0)
  34. #endif
  35. #ifndef timeradd
  36. # define timeradd(a, b, result) \
  37. do { \
  38. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  39. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  40. if ((result)->tv_usec >= 1000000) \
  41. { \
  42. ++(result)->tv_sec; \
  43. (result)->tv_usec -= 1000000; \
  44. } \
  45. } while (0)
  46. #endif
  47. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  48. {
  49. struct timeval tstart;
  50. DWORD msecs;
  51. gettimeofday(&tstart, NULL);
  52. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  53. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  54. if (rem) {
  55. struct timeval tdone, tnow, tleft;
  56. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  57. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  58. if (tdone.tv_usec > 1000000) {
  59. tdone.tv_usec -= 1000000;
  60. ++tdone.tv_sec;
  61. }
  62. gettimeofday(&tnow, NULL);
  63. if (timercmp(&tnow, &tdone, >))
  64. return 0;
  65. timersub(&tdone, &tnow, &tleft);
  66. rem->tv_sec = tleft.tv_sec;
  67. rem->tv_nsec = tleft.tv_usec * 1000;
  68. }
  69. errno = EINTR;
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. static inline int sleep(unsigned int secs)
  75. {
  76. struct timespec req, rem;
  77. req.tv_sec = secs;
  78. req.tv_nsec = 0;
  79. if (!nanosleep(&req, &rem))
  80. return 0;
  81. return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
  82. }
  83. enum {
  84. PRIO_PROCESS = 0,
  85. };
  86. static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
  87. {
  88. return -!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  89. }
  90. typedef unsigned long int ulong;
  91. typedef unsigned short int ushort;
  92. typedef unsigned int uint;
  93. #ifndef __SUSECONDS_T_TYPE
  94. typedef long suseconds_t;
  95. #endif
  96. #define PTH(thr) ((thr)->pth.p)
  97. #else
  98. #define PTH(thr) ((thr)->pth)
  99. #endif /* WIN32 */
  100. #endif /* __COMPAT_H__ */