futex_wait_wouldblock.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /******************************************************************************
  3. *
  4. * Copyright © International Business Machines Corp., 2009
  5. *
  6. * DESCRIPTION
  7. * Test if FUTEX_WAIT op returns -EWOULDBLOCK if the futex value differs
  8. * from the expected one.
  9. *
  10. * AUTHOR
  11. * Gowrishankar <gowrishankar.m@in.ibm.com>
  12. *
  13. * HISTORY
  14. * 2009-Nov-14: Initial version by Gowrishankar <gowrishankar.m@in.ibm.com>
  15. *
  16. *****************************************************************************/
  17. #include <errno.h>
  18. #include <getopt.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include "futextest.h"
  24. #include "futex2test.h"
  25. #include "kselftest_harness.h"
  26. #define timeout_ns 100000
  27. TEST(futex_wait_wouldblock)
  28. {
  29. struct timespec to = {.tv_sec = 0, .tv_nsec = timeout_ns};
  30. futex_t f1 = FUTEX_INITIALIZER;
  31. int res;
  32. ksft_print_dbg_msg("Calling futex_wait on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
  33. res = futex_wait(&f1, f1+1, &to, FUTEX_PRIVATE_FLAG);
  34. if (!res || errno != EWOULDBLOCK) {
  35. ksft_test_result_fail("futex_wait returned: %d %s\n",
  36. res ? errno : res,
  37. res ? strerror(errno) : "");
  38. } else {
  39. ksft_test_result_pass("futex_wait\n");
  40. }
  41. }
  42. TEST(futex_waitv_wouldblock)
  43. {
  44. struct timespec to = {.tv_sec = 0, .tv_nsec = timeout_ns};
  45. futex_t f1 = FUTEX_INITIALIZER;
  46. struct futex_waitv waitv = {
  47. .uaddr = (uintptr_t)&f1,
  48. .val = f1 + 1,
  49. .flags = FUTEX_32,
  50. .__reserved = 0,
  51. };
  52. int res;
  53. if (clock_gettime(CLOCK_MONOTONIC, &to))
  54. ksft_exit_fail_msg("clock_gettime failed %d\n", errno);
  55. to.tv_nsec += timeout_ns;
  56. if (to.tv_nsec >= 1000000000) {
  57. to.tv_sec++;
  58. to.tv_nsec -= 1000000000;
  59. }
  60. ksft_print_dbg_msg("Calling futex_waitv on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
  61. res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
  62. if (!res || errno != EWOULDBLOCK) {
  63. ksft_test_result_fail("futex_waitv returned: %d %s\n",
  64. res ? errno : res,
  65. res ? strerror(errno) : "");
  66. } else {
  67. ksft_test_result_pass("futex_waitv\n");
  68. }
  69. }
  70. TEST_HARNESS_MAIN