compat.h 1.9 KB

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