tst-cancel18.c 3.6 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. #include "../test-skeleton.h"
  22. static pthread_barrier_t b;
  23. /* Cleanup handling test. */
  24. static int cl_called;
  25. static void
  26. cl (void *arg)
  27. {
  28. ++cl_called;
  29. }
  30. static void *
  31. tf (void *arg)
  32. {
  33. int r = pthread_barrier_wait (&b);
  34. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  35. {
  36. puts ("barrier_wait failed");
  37. exit (1);
  38. }
  39. pthread_cleanup_push (cl, NULL);
  40. struct timespec ts = { .tv_sec = arg == NULL ? 10000000 : 0, .tv_nsec = 0 };
  41. TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts));
  42. pthread_cleanup_pop (0);
  43. puts ("clock_nanosleep returned");
  44. exit (1);
  45. }
  46. static int
  47. do_test (void)
  48. {
  49. if (pthread_barrier_init (&b, NULL, 2) != 0)
  50. {
  51. puts ("barrier_init failed");
  52. return 1;
  53. }
  54. pthread_t th;
  55. if (pthread_create (&th, NULL, tf, NULL) != 0)
  56. {
  57. puts ("1st create failed");
  58. return 1;
  59. }
  60. int r = pthread_barrier_wait (&b);
  61. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  62. {
  63. puts ("barrier_wait failed");
  64. exit (1);
  65. }
  66. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  67. while (nanosleep (&ts, &ts) != 0)
  68. continue;
  69. puts ("going to cancel in-time");
  70. if (pthread_cancel (th) != 0)
  71. {
  72. puts ("1st cancel failed");
  73. return 1;
  74. }
  75. void *status;
  76. if (pthread_join (th, &status) != 0)
  77. {
  78. puts ("1st join failed");
  79. return 1;
  80. }
  81. if (status != PTHREAD_CANCELED)
  82. {
  83. puts ("1st thread not canceled");
  84. return 1;
  85. }
  86. if (cl_called == 0)
  87. {
  88. puts ("cleanup handler not called");
  89. return 1;
  90. }
  91. if (cl_called > 1)
  92. {
  93. puts ("cleanup handler called more than once");
  94. return 1;
  95. }
  96. puts ("in-time cancellation succeeded");
  97. cl_called = 0;
  98. if (pthread_create (&th, NULL, tf, NULL) != 0)
  99. {
  100. puts ("2nd create failed");
  101. return 1;
  102. }
  103. puts ("going to cancel early");
  104. if (pthread_cancel (th) != 0)
  105. {
  106. puts ("2nd cancel failed");
  107. return 1;
  108. }
  109. r = pthread_barrier_wait (&b);
  110. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  111. {
  112. puts ("barrier_wait failed");
  113. exit (1);
  114. }
  115. if (pthread_join (th, &status) != 0)
  116. {
  117. puts ("2nd join failed");
  118. return 1;
  119. }
  120. if (status != PTHREAD_CANCELED)
  121. {
  122. puts ("2nd thread not canceled");
  123. return 1;
  124. }
  125. if (cl_called == 0)
  126. {
  127. printf ("cleanup handler not called\n");
  128. return 1;
  129. }
  130. if (cl_called > 1)
  131. {
  132. printf ("cleanup handler called more than once\n");
  133. return 1;
  134. }
  135. puts ("early cancellation succeeded");
  136. return 0;
  137. }
  138. #define TEST_FUNCTION do_test ()
  139. #include "../test-skeleton.c"