tst-barrier3.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (C) 2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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. /* Test of POSIX barriers. */
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #define NTHREADS 20
  22. #define ROUNDS 20
  23. static pthread_barrier_t barriers[NTHREADS];
  24. static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  25. static int counters[NTHREADS];
  26. static int serial[NTHREADS];
  27. static void *
  28. worker (void *arg)
  29. {
  30. void *result = NULL;
  31. int nr = (long int) arg;
  32. int i;
  33. for (i = 0; i < ROUNDS; ++i)
  34. {
  35. int j;
  36. int retval;
  37. if (nr == 0)
  38. {
  39. memset (counters, '\0', sizeof (counters));
  40. memset (serial, '\0', sizeof (serial));
  41. }
  42. retval = pthread_barrier_wait (&barriers[NTHREADS - 1]);
  43. if (retval != 0 && retval != PTHREAD_BARRIER_SERIAL_THREAD)
  44. {
  45. printf ("thread %d failed to wait for all the others\n", nr);
  46. result = (void *) 1;
  47. }
  48. for (j = nr; j < NTHREADS; ++j)
  49. {
  50. /* Increment the counter for this round. */
  51. pthread_mutex_lock (&lock);
  52. ++counters[j];
  53. pthread_mutex_unlock (&lock);
  54. /* Wait for the rest. */
  55. retval = pthread_barrier_wait (&barriers[j]);
  56. /* Test the result. */
  57. if (nr == 0 && counters[j] != j + 1)
  58. {
  59. printf ("barrier in round %d released but count is %d\n",
  60. j, counters[j]);
  61. result = (void *) 1;
  62. }
  63. if (retval != 0)
  64. {
  65. if (retval != PTHREAD_BARRIER_SERIAL_THREAD)
  66. {
  67. printf ("thread %d in round %d has nonzero return value != PTHREAD_BARRIER_SERIAL_THREAD\n",
  68. nr, j);
  69. result = (void *) 1;
  70. }
  71. else
  72. {
  73. pthread_mutex_lock (&lock);
  74. ++serial[j];
  75. pthread_mutex_unlock (&lock);
  76. }
  77. }
  78. /* Wait for the rest again. */
  79. retval = pthread_barrier_wait (&barriers[j]);
  80. /* Now we can check whether exactly one thread was serializing. */
  81. if (nr == 0 && serial[j] != 1)
  82. {
  83. printf ("not exactly one serial thread in round %d\n", j);
  84. result = (void *) 1;
  85. }
  86. }
  87. }
  88. return result;
  89. }
  90. #define TEST_FUNCTION do_test ()
  91. #define TIMEOUT 60
  92. static int
  93. do_test (void)
  94. {
  95. pthread_t threads[NTHREADS];
  96. int i;
  97. void *res;
  98. int result = 0;
  99. /* Initialized the barrier variables. */
  100. for (i = 0; i < NTHREADS; ++i)
  101. if (pthread_barrier_init (&barriers[i], NULL, i + 1) != 0)
  102. {
  103. printf ("Failed to initialize barrier %d\n", i);
  104. exit (1);
  105. }
  106. /* Start the threads. */
  107. for (i = 0; i < NTHREADS; ++i)
  108. if (pthread_create (&threads[i], NULL, worker, (void *) (long int) i) != 0)
  109. {
  110. printf ("Failed to start thread %d\n", i);
  111. exit (1);
  112. }
  113. /* And wait for them. */
  114. for (i = 0; i < NTHREADS; ++i)
  115. if (pthread_join (threads[i], &res) != 0 || res != NULL)
  116. {
  117. printf ("thread %d returned a failure\n", i);
  118. result = 1;
  119. }
  120. else
  121. printf ("joined threads %d\n", i);
  122. if (result == 0)
  123. puts ("all OK");
  124. return result;
  125. }
  126. #include "../test-skeleton.c"