futex_timeout.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #define _GNU_SOURCE
  2. #include <errno.h>
  3. #include <linux/futex.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/syscall.h>
  7. #include <sys/time.h>
  8. #include <unistd.h>
  9. #define SLEEP_SEC 1
  10. int main(int argc, const char** argv) {
  11. int myfutex = 0;
  12. int ret;
  13. int futex_errno = 0;
  14. struct timespec t = {.tv_sec = SLEEP_SEC, .tv_nsec = 0};
  15. struct timeval tv1 = { 0 };
  16. struct timeval tv2 = { 0 };
  17. printf("invoke futex syscall with a %d second timeout\n", SLEEP_SEC);
  18. if (gettimeofday(&tv1, NULL)) {
  19. printf("Cannot get time 1: %m\n");
  20. return 1;
  21. }
  22. ret = syscall(SYS_futex, &myfutex, FUTEX_WAIT, 0, &t, NULL, 0);
  23. futex_errno = errno;
  24. if (gettimeofday(&tv2, NULL)) {
  25. printf("Cannot get time 2: %m\n");
  26. return 1;
  27. }
  28. if (ret != -1 || futex_errno != ETIMEDOUT) {
  29. printf("futex syscall returned: %d with errno: %d (%s)\n", ret, futex_errno, strerror(futex_errno));
  30. return 1;
  31. }
  32. long long diff = (tv2.tv_sec - tv1.tv_sec) * 1000000ll;
  33. diff += tv2.tv_usec - tv1.tv_usec;
  34. if (diff < 0) {
  35. printf("Just moved back in time (%lld), better call Ghostbusters!\n", diff);
  36. return 1;
  37. }
  38. if (diff < 1000000ll * SLEEP_SEC) {
  39. printf("Slept for %lld microseconds, which is less than %d seconds\n", diff, SLEEP_SEC);
  40. return 1;
  41. }
  42. puts("futex correctly timed out");
  43. return 0;
  44. }