tst-once4.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (C) 2003, 2004 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. static pthread_once_t once = PTHREAD_ONCE_INIT;
  22. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  23. static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  24. static pthread_barrier_t bar;
  25. static int global;
  26. static int cl_called;
  27. static void
  28. once_handler1 (void)
  29. {
  30. if (pthread_mutex_lock (&mut) != 0)
  31. {
  32. puts ("once_handler1: mutex_lock failed");
  33. exit (1);
  34. }
  35. int r = pthread_barrier_wait (&bar);
  36. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  37. {
  38. puts ("once_handler1: barrier_wait failed");
  39. exit (1);
  40. }
  41. pthread_cond_wait (&cond, &mut);
  42. /* We should never get here. */
  43. }
  44. static void
  45. once_handler2 (void)
  46. {
  47. global = 1;
  48. }
  49. static void
  50. cl (void *arg)
  51. {
  52. ++cl_called;
  53. }
  54. static void *
  55. tf1 (void *arg)
  56. {
  57. pthread_cleanup_push (cl, NULL);
  58. pthread_once (&once, once_handler1);
  59. pthread_cleanup_pop (0);
  60. /* We should never get here. */
  61. puts ("pthread_once in tf returned");
  62. exit (1);
  63. }
  64. static void *
  65. tf2 (void *arg)
  66. {
  67. pthread_cleanup_push (cl, NULL);
  68. int r = pthread_barrier_wait (&bar);
  69. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  70. {
  71. puts ("once_handler2: barrier_wait failed");
  72. exit (1);
  73. }
  74. pthread_cleanup_pop (0);
  75. pthread_once (&once, once_handler2);
  76. return NULL;
  77. }
  78. static int
  79. do_test (void)
  80. {
  81. pthread_t th[2];
  82. if (pthread_barrier_init (&bar, NULL, 2) != 0)
  83. {
  84. puts ("barrier_init failed");
  85. return 1;
  86. }
  87. if (pthread_create (&th[0], NULL, tf1, NULL) != 0)
  88. {
  89. puts ("first create failed");
  90. return 1;
  91. }
  92. int r = pthread_barrier_wait (&bar);
  93. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  94. {
  95. puts ("first barrier_wait failed");
  96. return 1;
  97. }
  98. if (pthread_mutex_lock (&mut) != 0)
  99. {
  100. puts ("mutex_lock failed");
  101. return 1;
  102. }
  103. /* We unlock the mutex so that we catch the case where the pthread_cond_wait
  104. call incorrectly resumes and tries to get the mutex. */
  105. if (pthread_mutex_unlock (&mut) != 0)
  106. {
  107. puts ("mutex_unlock failed");
  108. return 1;
  109. }
  110. if (pthread_create (&th[1], NULL, tf2, NULL) != 0)
  111. {
  112. puts ("second create failed");
  113. return 1;
  114. }
  115. r = pthread_barrier_wait (&bar);
  116. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  117. {
  118. puts ("second barrier_wait failed");
  119. return 1;
  120. }
  121. /* Give the second thread a chance to reach the pthread_once call. */
  122. sleep (2);
  123. /* Cancel the thread. */
  124. if (pthread_cancel (th[0]) != 0)
  125. {
  126. puts ("cancel failed");
  127. return 1;
  128. }
  129. void *result;
  130. pthread_join (th[0], &result);
  131. if (result != PTHREAD_CANCELED)
  132. {
  133. puts ("first join didn't return PTHREAD_CANCELED");
  134. return 1;
  135. }
  136. puts ("joined first thread");
  137. pthread_join (th[1], &result);
  138. if (result != NULL)
  139. {
  140. puts ("second join didn't return PTHREAD_CANCELED");
  141. return 1;
  142. }
  143. if (global != 1)
  144. {
  145. puts ("global still 0");
  146. return 1;
  147. }
  148. if (cl_called != 1)
  149. {
  150. printf ("cl_called = %d\n", cl_called);
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. #define TEST_FUNCTION do_test ()
  156. #define TIMEOUT 4
  157. #include "../test-skeleton.c"