compat.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef __COMPAT_H__
  2. #define __COMPAT_H__
  3. #ifdef WIN32
  4. #include "config.h"
  5. #include <errno.h>
  6. #include <time.h>
  7. #include <pthread.h>
  8. #include <sys/time.h>
  9. #include "miner.h" // for timersub
  10. #include <windows.h>
  11. #ifndef HAVE_LIBWINPTHREAD
  12. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  13. {
  14. struct timeval tstart;
  15. DWORD msecs;
  16. gettimeofday(&tstart, NULL);
  17. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  18. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  19. if (rem) {
  20. struct timeval tdone, tnow, tleft;
  21. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  22. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  23. if (tdone.tv_usec > 1000000) {
  24. tdone.tv_usec -= 1000000;
  25. ++tdone.tv_sec;
  26. }
  27. gettimeofday(&tnow, NULL);
  28. if (timercmp(&tnow, &tdone, >))
  29. return 0;
  30. timersub(&tdone, &tnow, &tleft);
  31. rem->tv_sec = tleft.tv_sec;
  32. rem->tv_nsec = tleft.tv_usec * 1000;
  33. }
  34. errno = EINTR;
  35. return -1;
  36. }
  37. return 0;
  38. }
  39. #endif
  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(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
  53. {
  54. /* FIXME - actually do something */
  55. return 0;
  56. }
  57. typedef unsigned long int ulong;
  58. typedef unsigned short int ushort;
  59. typedef unsigned int uint;
  60. #ifndef __SUSECONDS_T_TYPE
  61. typedef long suseconds_t;
  62. #endif
  63. #ifdef HAVE_LIBWINPTHREAD
  64. #define PTH(thr) ((thr)->pth)
  65. #else
  66. #define PTH(thr) ((thr)->pth.p)
  67. #endif
  68. #else
  69. #define PTH(thr) ((thr)->pth)
  70. #endif /* WIN32 */
  71. #endif /* __COMPAT_H__ */