tst-cond10.c 3.6 KB

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