futex_requeue.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #define _GNU_SOURCE
  2. #include <errno.h>
  3. #include <limits.h>
  4. #include <linux/futex.h>
  5. #include <pthread.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/syscall.h>
  10. #include <unistd.h>
  11. static int futex(int* uaddr, int futex_op, int val, const struct timespec* timeout, int* uaddr2, int val3) {
  12. return syscall(SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3);
  13. }
  14. static int futex_wait(int* uaddr, int val, const struct timespec* timeout) {
  15. return futex(uaddr, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, val, timeout, NULL, 0);
  16. }
  17. static int futex_wake(int* uaddr, int to_wake) {
  18. return futex(uaddr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, to_wake, NULL, NULL, 0);
  19. }
  20. static int futex_cmp_requeue(int* uaddr, int val, int to_wake, int* uaddr2, unsigned int max_requeue) {
  21. return futex(uaddr, FUTEX_CMP_REQUEUE | FUTEX_PRIVATE_FLAG, to_wake, (struct timespec*)(unsigned long)max_requeue, uaddr2, val);
  22. }
  23. static void fail(const char* msg, int x) {
  24. printf("%s failed with %d (%s)\n", msg, x, strerror(x));
  25. exit(1);
  26. }
  27. static void check(int x) {
  28. if (x) {
  29. fail("pthread", x);
  30. }
  31. }
  32. static void store(int* ptr, int val) {
  33. __atomic_store_n(ptr, val, __ATOMIC_SEQ_CST);
  34. }
  35. static int load(int* ptr) {
  36. return __atomic_load_n(ptr, __ATOMIC_SEQ_CST);
  37. }
  38. static int futex1 = 0;
  39. static int futex2 = 0;
  40. #define THREADS 9
  41. #define THREADS_WAKE 2
  42. #define THREADS_REQUEUE 3
  43. static int thread_state[THREADS] = { 0 };
  44. static void* thread_func(void* arg) {
  45. unsigned long i = (unsigned long)arg;
  46. store(&thread_state[i], 1);
  47. int ret = futex_wait(&futex1, futex1, NULL);
  48. if (ret) {
  49. printf("futex_wait in thread %lu returned %d (%s)\n", i, ret, strerror(ret));
  50. // skip setting state below
  51. return arg;
  52. }
  53. store(&thread_state[i], 2);
  54. return arg;
  55. }
  56. int main(void) {
  57. pthread_t th[THREADS];
  58. unsigned long i;
  59. int ret;
  60. for (i = 0; i < THREADS; i++) {
  61. check(pthread_create(&th[i], NULL, thread_func, (void*)i));
  62. }
  63. // wait for all threads
  64. for (i = 0; i < THREADS; i++) {
  65. while (load(&thread_state[i]) != 1) {
  66. usleep(1000u);
  67. }
  68. }
  69. // and let them sleep on futex
  70. usleep(100000u);
  71. ret = futex_cmp_requeue(&futex1, futex1, THREADS_WAKE, &futex2, THREADS_REQUEUE);
  72. if (ret < 0) {
  73. fail("futex_cmp_requeue", errno);
  74. }
  75. if (ret != THREADS_WAKE + THREADS_REQUEUE) {
  76. printf("futex_cmp_requeue returned %d instead of %d!\n", ret, THREADS_WAKE + THREADS_REQUEUE);
  77. return 1;
  78. }
  79. // let the woken thread(s) end
  80. usleep(100000u);
  81. ret = 0;
  82. for (i = 0; i < THREADS; i++) {
  83. if (load(&thread_state[i]) == 2) {
  84. ret++;
  85. check(pthread_join(th[i], NULL));
  86. store(&thread_state[i], 3);
  87. }
  88. }
  89. if (ret != THREADS_WAKE) {
  90. printf("futex_cmp_requeue woke-up %d threads instead of %d!\n", ret, THREADS_WAKE);
  91. return 1;
  92. }
  93. ret = futex_wake(&futex1, INT_MAX);
  94. if (ret < 0) {
  95. fail("futex_wake(&futex1)", errno);
  96. }
  97. if (ret != (THREADS - THREADS_WAKE - THREADS_REQUEUE)) {
  98. printf("futex_wake on futex1 woke-up %d threads instead of %d!\n", ret, THREADS - THREADS_WAKE - THREADS_REQUEUE);
  99. return 1;
  100. }
  101. ret = futex_wake(&futex2, INT_MAX);
  102. if (ret < 0) {
  103. fail("futex_wake(&futex2)", errno);
  104. }
  105. if (ret != THREADS_REQUEUE) {
  106. printf("futex_wake on futex2 woke-up %d threads instead of %d!\n", ret, THREADS_REQUEUE);
  107. return 1;
  108. }
  109. for (i = 0; i < THREADS; i++) {
  110. if (load(&thread_state[i]) != 3) {
  111. check(pthread_join(th[i], NULL));
  112. }
  113. }
  114. puts("Test successful!");
  115. return 0;
  116. }