tst-barrier3.c 3.6 KB

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