tst-cond10.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <error.h>
  17. #include <pthread.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #define N 10
  22. #define ROUNDS 100
  23. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  24. static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  25. static pthread_barrier_t bN1;
  26. static pthread_barrier_t b2;
  27. static void *
  28. tf (void *p)
  29. {
  30. if (pthread_mutex_lock (&mut) != 0)
  31. {
  32. puts ("child: 1st mutex_lock failed");
  33. exit (1);
  34. }
  35. int e = pthread_barrier_wait (&b2);
  36. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  37. {
  38. puts ("child: 1st barrier_wait failed");
  39. exit (1);
  40. }
  41. if (pthread_cond_wait (&cond, &mut) != 0)
  42. {
  43. puts ("child: cond_wait failed");
  44. exit (1);
  45. }
  46. if (pthread_mutex_unlock (&mut) != 0)
  47. {
  48. puts ("child: mutex_unlock failed");
  49. exit (1);
  50. }
  51. e = pthread_barrier_wait (&bN1);
  52. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  53. {
  54. puts ("child: 2nd barrier_wait failed");
  55. exit (1);
  56. }
  57. return NULL;
  58. }
  59. static int
  60. do_test (void)
  61. {
  62. if (pthread_barrier_init (&bN1, NULL, N + 1) != 0)
  63. {
  64. puts ("barrier_init failed");
  65. exit (1);
  66. }
  67. if (pthread_barrier_init (&b2, NULL, 2) != 0)
  68. {
  69. puts ("barrier_init failed");
  70. exit (1);
  71. }
  72. pthread_attr_t at;
  73. if (pthread_attr_init (&at) != 0)
  74. {
  75. puts ("attr_init failed");
  76. return 1;
  77. }
  78. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  79. {
  80. puts ("attr_setstacksize failed");
  81. return 1;
  82. }
  83. int r;
  84. for (r = 0; r < ROUNDS; ++r)
  85. {
  86. printf ("round %d\n", r + 1);
  87. int i;
  88. pthread_t th[N];
  89. for (i = 0; i < N; ++i)
  90. {
  91. if (pthread_create (&th[i], &at, tf, NULL) != 0)
  92. {
  93. puts ("create failed");
  94. exit (1);
  95. }
  96. int e = pthread_barrier_wait (&b2);
  97. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  98. {
  99. puts ("parent: 1st barrier_wait failed");
  100. exit (1);
  101. }
  102. }
  103. if (pthread_mutex_lock (&mut) != 0)
  104. {
  105. puts ("parent: mutex_lock failed");
  106. exit (1);
  107. }
  108. if (pthread_mutex_unlock (&mut) != 0)
  109. {
  110. puts ("parent: mutex_unlock failed");
  111. exit (1);
  112. }
  113. /* N single signal calls. Without locking. This tests that no
  114. signal gets lost. */
  115. for (i = 0; i < N; ++i)
  116. if (pthread_cond_signal (&cond) != 0)
  117. {
  118. puts ("cond_signal failed");
  119. exit (1);
  120. }
  121. int e = pthread_barrier_wait (&bN1);
  122. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  123. {
  124. puts ("parent: 2nd barrier_wait failed");
  125. exit (1);
  126. }
  127. for (i = 0; i < N; ++i)
  128. if (pthread_join (th[i], NULL) != 0)
  129. {
  130. puts ("join failed");
  131. exit (1);
  132. }
  133. }
  134. if (pthread_attr_destroy (&at) != 0)
  135. {
  136. puts ("attr_destroy failed");
  137. return 1;
  138. }
  139. return 0;
  140. }
  141. #define TEST_FUNCTION do_test ()
  142. #include "../test-skeleton.c"