tst-cancel18.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. static pthread_barrier_t b;
  22. /* Cleanup handling test. */
  23. static int cl_called;
  24. static void
  25. cl (void *arg)
  26. {
  27. ++cl_called;
  28. }
  29. static void *
  30. tf (void *arg)
  31. {
  32. int r = pthread_barrier_wait (&b);
  33. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  34. {
  35. puts ("barrier_wait failed");
  36. exit (1);
  37. }
  38. pthread_cleanup_push (cl, NULL);
  39. struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
  40. TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts));
  41. pthread_cleanup_pop (0);
  42. puts ("clock_nanosleep returned");
  43. exit (1);
  44. }
  45. static int
  46. do_test (void)
  47. {
  48. if (pthread_barrier_init (&b, NULL, 2) != 0)
  49. {
  50. puts ("barrier_init failed");
  51. return 1;
  52. }
  53. pthread_t th;
  54. if (pthread_create (&th, NULL, tf, NULL) != 0)
  55. {
  56. puts ("1st create failed");
  57. return 1;
  58. }
  59. int r = pthread_barrier_wait (&b);
  60. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  61. {
  62. puts ("barrier_wait failed");
  63. exit (1);
  64. }
  65. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  66. while (nanosleep (&ts, &ts) != 0)
  67. continue;
  68. puts ("going to cancel in-time");
  69. if (pthread_cancel (th) != 0)
  70. {
  71. puts ("1st cancel failed");
  72. return 1;
  73. }
  74. void *status;
  75. if (pthread_join (th, &status) != 0)
  76. {
  77. puts ("1st join failed");
  78. return 1;
  79. }
  80. if (status != PTHREAD_CANCELED)
  81. {
  82. puts ("1st thread not canceled");
  83. return 1;
  84. }
  85. if (cl_called == 0)
  86. {
  87. puts ("cleanup handler not called");
  88. return 1;
  89. }
  90. if (cl_called > 1)
  91. {
  92. puts ("cleanup handler called more than once");
  93. return 1;
  94. }
  95. puts ("in-time cancellation succeeded");
  96. cl_called = 0;
  97. if (pthread_create (&th, NULL, tf, NULL) != 0)
  98. {
  99. puts ("2nd create failed");
  100. return 1;
  101. }
  102. puts ("going to cancel early");
  103. if (pthread_cancel (th) != 0)
  104. {
  105. puts ("2nd cancel failed");
  106. return 1;
  107. }
  108. r = pthread_barrier_wait (&b);
  109. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  110. {
  111. puts ("barrier_wait failed");
  112. exit (1);
  113. }
  114. if (pthread_join (th, &status) != 0)
  115. {
  116. puts ("2nd join failed");
  117. return 1;
  118. }
  119. if (status != PTHREAD_CANCELED)
  120. {
  121. puts ("2nd thread not canceled");
  122. return 1;
  123. }
  124. if (cl_called == 0)
  125. {
  126. printf ("cleanup handler not called\n");
  127. return 1;
  128. }
  129. if (cl_called > 1)
  130. {
  131. printf ("cleanup handler called more than once\n");
  132. return 1;
  133. }
  134. puts ("early cancellation succeeded");
  135. return 0;
  136. }
  137. #define TEST_FUNCTION do_test ()
  138. #include "../test-skeleton.c"