futex-timeout.c 493 B

123456789101112131415161718192021
  1. #include <errno.h>
  2. #include <linux/futex.h>
  3. #include <stdio.h>
  4. #include <sys/syscall.h>
  5. #include <sys/time.h>
  6. #include <unistd.h>
  7. int main(int argc, const char** argv) {
  8. int myfutex = 0;
  9. int ret;
  10. struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
  11. puts("invoke futex syscall with 1-second timeout");
  12. ret = syscall(SYS_futex, &myfutex, FUTEX_WAIT, 0, &t, NULL, 0);
  13. if (ret == -1 && errno == ETIMEDOUT) {
  14. puts("futex correctly timed out");
  15. }
  16. return 0;
  17. }