tst-join5.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* Copyright (C) 2003-2019 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 void
  22. wait_code (void)
  23. {
  24. struct timespec ts = { .tv_sec = 0, .tv_nsec = 200000000 };
  25. while (nanosleep (&ts, &ts) < 0)
  26. ;
  27. }
  28. #ifdef WAIT_IN_CHILD
  29. static pthread_barrier_t b;
  30. #endif
  31. static void *
  32. tf1 (void *arg)
  33. {
  34. #ifdef WAIT_IN_CHILD
  35. int e = pthread_barrier_wait (&b);
  36. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  37. {
  38. printf ("%s: barrier_wait failed\n", __func__);
  39. exit (1);
  40. }
  41. wait_code ();
  42. #endif
  43. pthread_join ((pthread_t) arg, NULL);
  44. exit (42);
  45. }
  46. static void *
  47. tf2 (void *arg)
  48. {
  49. #ifdef WAIT_IN_CHILD
  50. int e = pthread_barrier_wait (&b);
  51. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  52. {
  53. printf ("%s: barrier_wait failed\n", __func__);
  54. exit (1);
  55. }
  56. wait_code ();
  57. #endif
  58. pthread_join ((pthread_t) arg, NULL);
  59. exit (43);
  60. }
  61. static int
  62. do_test (void)
  63. {
  64. #ifdef WAIT_IN_CHILD
  65. if (pthread_barrier_init (&b, NULL, 2) != 0)
  66. {
  67. puts ("barrier_init failed");
  68. return 1;
  69. }
  70. #endif
  71. pthread_t th;
  72. int err = pthread_join (pthread_self (), NULL);
  73. if (err == 0)
  74. {
  75. puts ("1st circular join succeeded");
  76. return 1;
  77. }
  78. if (err != EDEADLK)
  79. {
  80. printf ("1st circular join %d, not EDEADLK\n", err);
  81. return 1;
  82. }
  83. if (pthread_create (&th, NULL, tf1, (void *) pthread_self ()) != 0)
  84. {
  85. puts ("1st create failed");
  86. return 1;
  87. }
  88. #ifndef WAIT_IN_CHILD
  89. wait_code ();
  90. #endif
  91. if (pthread_cancel (th) != 0)
  92. {
  93. puts ("cannot cancel 1st thread");
  94. return 1;
  95. }
  96. #ifdef WAIT_IN_CHILD
  97. int e = pthread_barrier_wait (&b);
  98. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  99. {
  100. printf ("%s: barrier_wait failed\n", __func__);
  101. return 1;
  102. }
  103. #endif
  104. void *r;
  105. err = pthread_join (th, &r);
  106. if (err != 0)
  107. {
  108. printf ("cannot join 1st thread: %d\n", err);
  109. return 1;
  110. }
  111. if (r != PTHREAD_CANCELED)
  112. {
  113. puts ("1st thread not canceled");
  114. return 1;
  115. }
  116. err = pthread_join (pthread_self (), NULL);
  117. if (err == 0)
  118. {
  119. puts ("2nd circular join succeeded");
  120. return 1;
  121. }
  122. if (err != EDEADLK)
  123. {
  124. printf ("2nd circular join %d, not EDEADLK\n", err);
  125. return 1;
  126. }
  127. if (pthread_create (&th, NULL, tf2, (void *) pthread_self ()) != 0)
  128. {
  129. puts ("2nd create failed");
  130. return 1;
  131. }
  132. #ifndef WAIT_IN_CHILD
  133. wait_code ();
  134. #endif
  135. if (pthread_cancel (th) != 0)
  136. {
  137. puts ("cannot cancel 2nd thread");
  138. return 1;
  139. }
  140. #ifdef WAIT_IN_CHILD
  141. e = pthread_barrier_wait (&b);
  142. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  143. {
  144. printf ("%s: barrier_wait failed\n", __func__);
  145. return 1;
  146. }
  147. #endif
  148. if (pthread_join (th, &r) != 0)
  149. {
  150. puts ("cannot join 2nd thread");
  151. return 1;
  152. }
  153. if (r != PTHREAD_CANCELED)
  154. {
  155. puts ("2nd thread not canceled");
  156. return 1;
  157. }
  158. err = pthread_join (pthread_self (), NULL);
  159. if (err == 0)
  160. {
  161. puts ("3rd circular join succeeded");
  162. return 1;
  163. }
  164. if (err != EDEADLK)
  165. {
  166. printf ("3rd circular join %d, not EDEADLK\n", err);
  167. return 1;
  168. }
  169. return 0;
  170. }
  171. #define TEST_FUNCTION do_test ()
  172. #include "../test-skeleton.c"