compat.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "util.h"
  11. #include <windows.h>
  12. #if !(__MINGW32__)
  13. #ifndef HAVE_LIBWINPTHREAD
  14. static inline int nanosleep(const struct timespec *req, struct timespec *rem)
  15. {
  16. struct timeval tstart;
  17. DWORD msecs;
  18. cgtime(&tstart);
  19. msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
  20. if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
  21. if (rem) {
  22. struct timeval tdone, tnow, tleft;
  23. tdone.tv_sec = tstart.tv_sec + req->tv_sec;
  24. tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
  25. if (tdone.tv_usec > 1000000) {
  26. tdone.tv_usec -= 1000000;
  27. ++tdone.tv_sec;
  28. }
  29. cgtime(&tnow);
  30. if (timercmp(&tnow, &tdone, >))
  31. return 0;
  32. timersub(&tdone, &tnow, &tleft);
  33. rem->tv_sec = tleft.tv_sec;
  34. rem->tv_nsec = tleft.tv_usec * 1000;
  35. }
  36. errno = EINTR;
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. #endif
  42. static inline int sleep(unsigned int secs)
  43. {
  44. struct timespec req, rem;
  45. req.tv_sec = secs;
  46. req.tv_nsec = 0;
  47. if (!nanosleep(&req, &rem))
  48. return 0;
  49. return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
  50. }
  51. #endif
  52. enum {
  53. PRIO_PROCESS = 0,
  54. };
  55. static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
  56. {
  57. /* FIXME - actually do something */
  58. return 0;
  59. }
  60. typedef unsigned long int ulong;
  61. typedef unsigned short int ushort;
  62. typedef unsigned int uint;
  63. #ifndef __SUSECONDS_T_TYPE
  64. typedef long suseconds_t;
  65. #endif
  66. #ifdef HAVE_LIBWINPTHREAD
  67. #define PTH(thr) ((thr)->pth)
  68. #else
  69. #define PTH(thr) ((thr)->pth.p)
  70. #endif
  71. #else
  72. #define PTH(thr) ((thr)->pth)
  73. #endif /* WIN32 */
  74. #endif /* __COMPAT_H__ */