futex_wait_wouldblock.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "logging.h"
  26. #define TEST_NAME "futex-wait-wouldblock"
  27. #define timeout_ns 100000
  28. void usage(char *prog)
  29. {
  30. printf("Usage: %s\n", prog);
  31. printf(" -c Use color\n");
  32. printf(" -h Display this help message\n");
  33. printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
  34. VQUIET, VCRITICAL, VINFO);
  35. }
  36. int main(int argc, char *argv[])
  37. {
  38. struct timespec to = {.tv_sec = 0, .tv_nsec = timeout_ns};
  39. futex_t f1 = FUTEX_INITIALIZER;
  40. int res, ret = RET_PASS;
  41. int c;
  42. struct futex_waitv waitv = {
  43. .uaddr = (uintptr_t)&f1,
  44. .val = f1+1,
  45. .flags = FUTEX_32,
  46. .__reserved = 0
  47. };
  48. while ((c = getopt(argc, argv, "cht:v:")) != -1) {
  49. switch (c) {
  50. case 'c':
  51. log_color(1);
  52. break;
  53. case 'h':
  54. usage(basename(argv[0]));
  55. exit(0);
  56. case 'v':
  57. log_verbosity(atoi(optarg));
  58. break;
  59. default:
  60. usage(basename(argv[0]));
  61. exit(1);
  62. }
  63. }
  64. ksft_print_header();
  65. ksft_set_plan(2);
  66. ksft_print_msg("%s: Test the unexpected futex value in FUTEX_WAIT\n",
  67. basename(argv[0]));
  68. info("Calling futex_wait on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
  69. res = futex_wait(&f1, f1+1, &to, FUTEX_PRIVATE_FLAG);
  70. if (!res || errno != EWOULDBLOCK) {
  71. ksft_test_result_fail("futex_wait returned: %d %s\n",
  72. res ? errno : res,
  73. res ? strerror(errno) : "");
  74. ret = RET_FAIL;
  75. } else {
  76. ksft_test_result_pass("futex_wait\n");
  77. }
  78. if (clock_gettime(CLOCK_MONOTONIC, &to)) {
  79. error("clock_gettime failed\n", errno);
  80. return errno;
  81. }
  82. to.tv_nsec += timeout_ns;
  83. if (to.tv_nsec >= 1000000000) {
  84. to.tv_sec++;
  85. to.tv_nsec -= 1000000000;
  86. }
  87. info("Calling futex_waitv on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
  88. res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
  89. if (!res || errno != EWOULDBLOCK) {
  90. ksft_test_result_fail("futex_waitv returned: %d %s\n",
  91. res ? errno : res,
  92. res ? strerror(errno) : "");
  93. ret = RET_FAIL;
  94. } else {
  95. ksft_test_result_pass("futex_waitv\n");
  96. }
  97. ksft_print_cnts();
  98. return ret;
  99. }