1
0

__timedwait.c 1.1 KB

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