tst-cond20.c 3.9 KB

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