futex_wait.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright Collabora Ltd., 2021
  4. *
  5. * futex cmp requeue test by André Almeida <andrealmeid@collabora.com>
  6. */
  7. #include <pthread.h>
  8. #include <sys/shm.h>
  9. #include <sys/mman.h>
  10. #include <fcntl.h>
  11. #include "futextest.h"
  12. #include "kselftest_harness.h"
  13. #define timeout_ns 30000000
  14. #define WAKE_WAIT_US 10000
  15. #define SHM_PATH "futex_shm_file"
  16. void *futex;
  17. static void *waiterfn(void *arg)
  18. {
  19. struct timespec to;
  20. unsigned int flags = 0;
  21. if (arg)
  22. flags = *((unsigned int *) arg);
  23. to.tv_sec = 0;
  24. to.tv_nsec = timeout_ns;
  25. if (futex_wait(futex, 0, &to, flags))
  26. printf("waiter failed errno %d\n", errno);
  27. return NULL;
  28. }
  29. TEST(private_futex)
  30. {
  31. unsigned int flags = FUTEX_PRIVATE_FLAG;
  32. u_int32_t f_private = 0;
  33. pthread_t waiter;
  34. int res;
  35. futex = &f_private;
  36. /* Testing a private futex */
  37. ksft_print_dbg_msg("Calling private futex_wait on futex: %p\n", futex);
  38. if (pthread_create(&waiter, NULL, waiterfn, (void *) &flags))
  39. ksft_exit_fail_msg("pthread_create failed\n");
  40. usleep(WAKE_WAIT_US);
  41. ksft_print_dbg_msg("Calling private futex_wake on futex: %p\n", futex);
  42. res = futex_wake(futex, 1, FUTEX_PRIVATE_FLAG);
  43. if (res != 1) {
  44. ksft_test_result_fail("futex_wake private returned: %d %s\n",
  45. errno, strerror(errno));
  46. } else {
  47. ksft_test_result_pass("futex_wake private succeeds\n");
  48. }
  49. }
  50. TEST(anon_page)
  51. {
  52. u_int32_t *shared_data;
  53. pthread_t waiter;
  54. int res, shm_id;
  55. /* Testing an anon page shared memory */
  56. shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
  57. if (shm_id < 0) {
  58. if (errno == ENOSYS)
  59. ksft_exit_skip("shmget syscall not supported\n");
  60. perror("shmget");
  61. exit(1);
  62. }
  63. shared_data = shmat(shm_id, NULL, 0);
  64. *shared_data = 0;
  65. futex = shared_data;
  66. ksft_print_dbg_msg("Calling shared (page anon) futex_wait on futex: %p\n", futex);
  67. if (pthread_create(&waiter, NULL, waiterfn, NULL))
  68. ksft_exit_fail_msg("pthread_create failed\n");
  69. usleep(WAKE_WAIT_US);
  70. ksft_print_dbg_msg("Calling shared (page anon) futex_wake on futex: %p\n", futex);
  71. res = futex_wake(futex, 1, 0);
  72. if (res != 1) {
  73. ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n",
  74. errno, strerror(errno));
  75. } else {
  76. ksft_test_result_pass("futex_wake shared (page anon) succeeds\n");
  77. }
  78. shmdt(shared_data);
  79. }
  80. TEST(file_backed)
  81. {
  82. u_int32_t f_private = 0;
  83. pthread_t waiter;
  84. int res, fd;
  85. void *shm;
  86. /* Testing a file backed shared memory */
  87. fd = open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  88. if (fd < 0)
  89. ksft_exit_fail_msg("open\n");
  90. if (ftruncate(fd, sizeof(f_private)))
  91. ksft_exit_fail_msg("ftruncate\n");
  92. shm = mmap(NULL, sizeof(f_private), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  93. if (shm == MAP_FAILED)
  94. ksft_exit_fail_msg("mmap\n");
  95. memcpy(shm, &f_private, sizeof(f_private));
  96. futex = shm;
  97. ksft_print_dbg_msg("Calling shared (file backed) futex_wait on futex: %p\n", futex);
  98. if (pthread_create(&waiter, NULL, waiterfn, NULL))
  99. ksft_exit_fail_msg("pthread_create failed\n");
  100. usleep(WAKE_WAIT_US);
  101. ksft_print_dbg_msg("Calling shared (file backed) futex_wake on futex: %p\n", futex);
  102. res = futex_wake(shm, 1, 0);
  103. if (res != 1) {
  104. ksft_test_result_fail("futex_wake shared (file backed) returned: %d %s\n",
  105. errno, strerror(errno));
  106. } else {
  107. ksft_test_result_pass("futex_wake shared (file backed) succeeds\n");
  108. }
  109. munmap(shm, sizeof(f_private));
  110. remove(SHM_PATH);
  111. close(fd);
  112. }
  113. TEST_HARNESS_MAIN