compat.h 1.6 KB

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