__timedwait.c 617 B

1234567891011121314151617181920212223
  1. #include <time.h>
  2. #include <errno.h>
  3. #include "futex.h"
  4. #include "syscall.h"
  5. #include <stdio.h>
  6. int __timedwait(volatile int *addr, int val, clockid_t clk, const struct timespec *at, int priv)
  7. {
  8. int r;
  9. struct timespec to;
  10. if (at) {
  11. clock_gettime(clk, &to);
  12. to.tv_sec = at->tv_sec - to.tv_sec;
  13. if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
  14. to.tv_sec--;
  15. to.tv_nsec += 1000000000;
  16. }
  17. if (to.tv_sec < 0) return ETIMEDOUT;
  18. }
  19. if (priv) priv = 128; priv=0;
  20. r = -__syscall(SYS_futex, (long)addr, FUTEX_WAIT | priv, val, at ? (long)&to : 0);
  21. if (r == ETIMEDOUT || r == EINTR) return r;
  22. return 0;
  23. }