compat.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /* FIXME - actually do something */
  52. return 0;
  53. }
  54. typedef unsigned long int ulong;
  55. typedef unsigned short int ushort;
  56. typedef unsigned int uint;
  57. #ifndef __SUSECONDS_T_TYPE
  58. typedef long suseconds_t;
  59. #endif
  60. #define PTH(thr) ((thr)->pth.p)
  61. #else
  62. #define PTH(thr) ((thr)->pth)
  63. #endif /* WIN32 */
  64. #endif /* __COMPAT_H__ */