compat.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // NOTE: Windows strtok uses a thread-local static buffer, so this is safe
  10. #define SETUP_STRTOK_TS /*nothing needed*/
  11. #define strtok_ts strtok
  12. #include "miner.h" // for timersub
  13. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  14. {
  15. struct timeval tstart;
  16. DWORD msecs;
  17. gettimeofday(&tstart, NULL);
  18. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  19. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  20. if (rem) {
  21. struct timeval tdone, tnow, tleft;
  22. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  23. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  24. if (tdone.tv_usec > 1000000) {
  25. tdone.tv_usec -= 1000000;
  26. ++tdone.tv_sec;
  27. }
  28. gettimeofday(&tnow, NULL);
  29. if (timercmp(&tnow, &tdone, >))
  30. return 0;
  31. timersub(&tdone, &tnow, &tleft);
  32. rem->tv_sec = tleft.tv_sec;
  33. rem->tv_nsec = tleft.tv_usec * 1000;
  34. }
  35. errno = EINTR;
  36. return -1;
  37. }
  38. return 0;
  39. }
  40. static inline int sleep(unsigned int secs)
  41. {
  42. struct timespec req, rem;
  43. req.tv_sec = secs;
  44. req.tv_nsec = 0;
  45. if (!nanosleep(&req, &rem))
  46. return 0;
  47. return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
  48. }
  49. enum {
  50. PRIO_PROCESS = 0,
  51. };
  52. static inline int setpriority(int which, int who, int prio)
  53. {
  54. return -!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
  55. }
  56. typedef unsigned long int ulong;
  57. typedef unsigned short int ushort;
  58. typedef unsigned int uint;
  59. #ifndef __SUSECONDS_T_TYPE
  60. typedef long suseconds_t;
  61. #endif
  62. #define PTH(thr) ((thr)->pth.p)
  63. #else /* ! WIN32 */
  64. #define PTH(thr) ((thr)->pth)
  65. #define SETUP_STRTOK_TS char*_strtok_ts_saveptr
  66. #define strtok_ts(str, delim) strtok_r(str, delim, &_strtok_ts_saveptr)
  67. #endif /* WIN32 */
  68. #endif /* __COMPAT_H__ */