__timedwait.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <pthread.h>
  2. #include <time.h>
  3. #include <errno.h>
  4. #include "futex.h"
  5. #include "syscall.h"
  6. static int do_wait(volatile int *addr, int val,
  7. clockid_t clk, const struct timespec *at, int priv)
  8. {
  9. int r;
  10. struct timespec to, *top=0;
  11. if (at) {
  12. if (at->tv_nsec >= 1000000000UL) return EINVAL;
  13. if (clock_gettime(clk, &to)) return EINVAL;
  14. to.tv_sec = at->tv_sec - to.tv_sec;
  15. if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
  16. to.tv_sec--;
  17. to.tv_nsec += 1000000000;
  18. }
  19. if (to.tv_sec < 0) return ETIMEDOUT;
  20. top = &to;
  21. }
  22. r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT, val, top);
  23. if (r == EINTR || r == EINVAL || r == ETIMEDOUT) return r;
  24. return 0;
  25. }
  26. int __timedwait(volatile int *addr, int val,
  27. clockid_t clk, const struct timespec *at,
  28. void (*cleanup)(void *), void *arg, int priv)
  29. {
  30. int r, cs;
  31. if (!cleanup) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  32. pthread_cleanup_push(cleanup, arg);
  33. r = do_wait(addr, val, clk, at, priv);
  34. pthread_cleanup_pop(0);
  35. if (!cleanup) pthread_setcancelstate(cs, 0);
  36. return r;
  37. }