compat.h 2.9 KB

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