tst-cond20.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Copyright (C) 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  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 <pthread.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #define N 10
  21. #define ROUNDS 1000
  22. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  23. static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
  24. static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  25. static pthread_barrier_t b;
  26. static int count;
  27. static void *
  28. tf (void *p)
  29. {
  30. int i;
  31. for (i = 0; i < ROUNDS; ++i)
  32. {
  33. pthread_mutex_lock (&mut);
  34. if (++count == N)
  35. pthread_cond_signal (&cond2);
  36. #ifdef TIMED
  37. struct timeval tv;
  38. gettimeofday (&tv, NULL);
  39. struct timespec ts;
  40. /* Wait three seconds. */
  41. ts.tv_sec = tv.tv_sec + 3;
  42. ts.tv_nsec = tv.tv_usec * 1000;
  43. pthread_cond_timedwait (&cond, &mut, &ts);
  44. #else
  45. pthread_cond_wait (&cond, &mut);
  46. #endif
  47. pthread_mutex_unlock (&mut);
  48. int err = pthread_barrier_wait (&b);
  49. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  50. {
  51. puts ("child: barrier_wait failed");
  52. exit (1);
  53. }
  54. err = pthread_barrier_wait (&b);
  55. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  56. {
  57. puts ("child: barrier_wait failed");
  58. exit (1);
  59. }
  60. }
  61. return NULL;
  62. }
  63. static int
  64. do_test (void)
  65. {
  66. if (pthread_barrier_init (&b, NULL, N + 1) != 0)
  67. {
  68. puts ("barrier_init failed");
  69. return 1;
  70. }
  71. pthread_mutex_lock (&mut);
  72. int i, j, err;
  73. pthread_t th[N];
  74. for (i = 0; i < N; ++i)
  75. if ((err = pthread_create (&th[i], NULL, tf, NULL)) != 0)
  76. {
  77. printf ("cannot create thread %d: %s\n", i, strerror (err));
  78. return 1;
  79. }
  80. for (i = 0; i < ROUNDS; ++i)
  81. {
  82. pthread_cond_wait (&cond2, &mut);
  83. if (i & 1)
  84. pthread_mutex_unlock (&mut);
  85. if (i & 2)
  86. pthread_cond_broadcast (&cond);
  87. else if (i & 4)
  88. for (j = 0; j < N; ++j)
  89. pthread_cond_signal (&cond);
  90. else
  91. {
  92. for (j = 0; j < (i / 8) % N; ++j)
  93. pthread_cond_signal (&cond);
  94. pthread_cond_broadcast (&cond);
  95. }
  96. if ((i & 1) == 0)
  97. pthread_mutex_unlock (&mut);
  98. err = pthread_cond_destroy (&cond);
  99. if (err)
  100. {
  101. printf ("pthread_cond_destroy failed: %s\n", strerror (err));
  102. return 1;
  103. }
  104. /* Now clobber the cond variable which has been successfully
  105. destroyed above. */
  106. memset (&cond, (char) i, sizeof (cond));
  107. err = pthread_barrier_wait (&b);
  108. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  109. {
  110. puts ("parent: barrier_wait failed");
  111. return 1;
  112. }
  113. pthread_mutex_lock (&mut);
  114. err = pthread_barrier_wait (&b);
  115. if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
  116. {
  117. puts ("parent: barrier_wait failed");
  118. return 1;
  119. }
  120. count = 0;
  121. err = pthread_cond_init (&cond, NULL);
  122. if (err)
  123. {
  124. printf ("pthread_cond_init failed: %s\n", strerror (err));
  125. return 1;
  126. }
  127. }
  128. for (i = 0; i < N; ++i)
  129. if ((err = pthread_join (th[i], NULL)) != 0)
  130. {
  131. printf ("failed to join thread %d: %s\n", i, strerror (err));
  132. return 1;
  133. }
  134. puts ("done");
  135. return 0;
  136. }
  137. #define TEST_FUNCTION do_test ()
  138. #include "../test-skeleton.c"