futex_wait_timeout.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /******************************************************************************
  2. *
  3. * Copyright © International Business Machines Corp., 2009
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DESCRIPTION
  11. * Block on a futex and wait for timeout.
  12. *
  13. * AUTHOR
  14. * Darren Hart <dvhart@linux.intel.com>
  15. *
  16. * HISTORY
  17. * 2009-Nov-6: Initial version by Darren Hart <dvhart@linux.intel.com>
  18. *
  19. *****************************************************************************/
  20. #include <errno.h>
  21. #include <getopt.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include "futextest.h"
  27. #include "logging.h"
  28. #define TEST_NAME "futex-wait-timeout"
  29. static long timeout_ns = 100000; /* 100us default timeout */
  30. void usage(char *prog)
  31. {
  32. printf("Usage: %s\n", prog);
  33. printf(" -c Use color\n");
  34. printf(" -h Display this help message\n");
  35. printf(" -t N Timeout in nanoseconds (default: 100,000)\n");
  36. printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
  37. VQUIET, VCRITICAL, VINFO);
  38. }
  39. int main(int argc, char *argv[])
  40. {
  41. futex_t f1 = FUTEX_INITIALIZER;
  42. struct timespec to;
  43. int res, ret = RET_PASS;
  44. int c;
  45. while ((c = getopt(argc, argv, "cht:v:")) != -1) {
  46. switch (c) {
  47. case 'c':
  48. log_color(1);
  49. break;
  50. case 'h':
  51. usage(basename(argv[0]));
  52. exit(0);
  53. case 't':
  54. timeout_ns = atoi(optarg);
  55. break;
  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_print_msg("%s: Block on a futex and wait for timeout\n",
  66. basename(argv[0]));
  67. ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
  68. /* initialize timeout */
  69. to.tv_sec = 0;
  70. to.tv_nsec = timeout_ns;
  71. info("Calling futex_wait on f1: %u @ %p\n", f1, &f1);
  72. res = futex_wait(&f1, f1, &to, FUTEX_PRIVATE_FLAG);
  73. if (!res || errno != ETIMEDOUT) {
  74. fail("futex_wait returned %d\n", ret < 0 ? errno : ret);
  75. ret = RET_FAIL;
  76. }
  77. print_result(TEST_NAME, ret);
  78. return ret;
  79. }