| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2004-2016 Free Software Foundation, Inc. | 
|  | 2 | This file is part of the GNU C Library. | 
|  | 3 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2004. | 
|  | 4 |  | 
|  | 5 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 6 | modify it under the terms of the GNU Lesser General Public | 
|  | 7 | License as published by the Free Software Foundation; either | 
|  | 8 | version 2.1 of the License, or (at your option) any later version. | 
|  | 9 |  | 
|  | 10 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 13 | Lesser General Public License for more details. | 
|  | 14 |  | 
|  | 15 | You should have received a copy of the GNU Lesser General Public | 
|  | 16 | License along with the GNU C Library; if not, see | 
|  | 17 | <http://www.gnu.org/licenses/>.  */ | 
|  | 18 |  | 
|  | 19 | #include <pthread.h> | 
|  | 20 | #include <stdio.h> | 
|  | 21 | #include <stdlib.h> | 
|  | 22 | #include <string.h> | 
|  | 23 | #include <unistd.h> | 
|  | 24 |  | 
|  | 25 | #define N 10 | 
|  | 26 | #define ROUNDS 1000 | 
|  | 27 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; | 
|  | 28 | static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER; | 
|  | 29 | static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; | 
|  | 30 | static pthread_barrier_t b; | 
|  | 31 | static int count; | 
|  | 32 |  | 
|  | 33 | static void * | 
|  | 34 | tf (void *p) | 
|  | 35 | { | 
|  | 36 | int i; | 
|  | 37 | for (i = 0; i < ROUNDS; ++i) | 
|  | 38 | { | 
|  | 39 | pthread_mutex_lock (&mut); | 
|  | 40 |  | 
|  | 41 | if (++count == N) | 
|  | 42 | pthread_cond_signal (&cond2); | 
|  | 43 |  | 
|  | 44 | #ifdef TIMED | 
|  | 45 | struct timeval tv; | 
|  | 46 | gettimeofday (&tv, NULL); | 
|  | 47 | struct timespec ts; | 
|  | 48 | /* Wait three seconds.  */ | 
|  | 49 | ts.tv_sec = tv.tv_sec + 3; | 
|  | 50 | ts.tv_nsec = tv.tv_usec * 1000; | 
|  | 51 | pthread_cond_timedwait (&cond, &mut, &ts); | 
|  | 52 | #else | 
|  | 53 | pthread_cond_wait (&cond, &mut); | 
|  | 54 | #endif | 
|  | 55 |  | 
|  | 56 | pthread_mutex_unlock (&mut); | 
|  | 57 |  | 
|  | 58 | int err = pthread_barrier_wait (&b); | 
|  | 59 | if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD) | 
|  | 60 | { | 
|  | 61 | puts ("child: barrier_wait failed"); | 
|  | 62 | exit (1); | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | err = pthread_barrier_wait (&b); | 
|  | 66 | if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD) | 
|  | 67 | { | 
|  | 68 | puts ("child: barrier_wait failed"); | 
|  | 69 | exit (1); | 
|  | 70 | } | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | return NULL; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 |  | 
|  | 77 | static int | 
|  | 78 | do_test (void) | 
|  | 79 | { | 
|  | 80 | if (pthread_barrier_init (&b, NULL, N + 1) != 0) | 
|  | 81 | { | 
|  | 82 | puts ("barrier_init failed"); | 
|  | 83 | return 1; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | pthread_mutex_lock (&mut); | 
|  | 87 |  | 
|  | 88 | int i, j, err; | 
|  | 89 | pthread_t th[N]; | 
|  | 90 | for (i = 0; i < N; ++i) | 
|  | 91 | if ((err = pthread_create (&th[i], NULL, tf, NULL)) != 0) | 
|  | 92 | { | 
|  | 93 | printf ("cannot create thread %d: %s\n", i, strerror (err)); | 
|  | 94 | return 1; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | for (i = 0; i < ROUNDS; ++i) | 
|  | 98 | { | 
|  | 99 | pthread_cond_wait (&cond2, &mut); | 
|  | 100 |  | 
|  | 101 | if (i & 1) | 
|  | 102 | pthread_mutex_unlock (&mut); | 
|  | 103 |  | 
|  | 104 | if (i & 2) | 
|  | 105 | pthread_cond_broadcast (&cond); | 
|  | 106 | else if (i & 4) | 
|  | 107 | for (j = 0; j < N; ++j) | 
|  | 108 | pthread_cond_signal (&cond); | 
|  | 109 | else | 
|  | 110 | { | 
|  | 111 | for (j = 0; j < (i / 8) % N; ++j) | 
|  | 112 | pthread_cond_signal (&cond); | 
|  | 113 | pthread_cond_broadcast (&cond); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | if ((i & 1) == 0) | 
|  | 117 | pthread_mutex_unlock (&mut); | 
|  | 118 |  | 
|  | 119 | err = pthread_cond_destroy (&cond); | 
|  | 120 | if (err) | 
|  | 121 | { | 
|  | 122 | printf ("pthread_cond_destroy failed: %s\n", strerror (err)); | 
|  | 123 | return 1; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | /* Now clobber the cond variable which has been successfully | 
|  | 127 | destroyed above.  */ | 
|  | 128 | memset (&cond, (char) i, sizeof (cond)); | 
|  | 129 |  | 
|  | 130 | err = pthread_barrier_wait (&b); | 
|  | 131 | if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD) | 
|  | 132 | { | 
|  | 133 | puts ("parent: barrier_wait failed"); | 
|  | 134 | return 1; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | pthread_mutex_lock (&mut); | 
|  | 138 |  | 
|  | 139 | err = pthread_barrier_wait (&b); | 
|  | 140 | if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD) | 
|  | 141 | { | 
|  | 142 | puts ("parent: barrier_wait failed"); | 
|  | 143 | return 1; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | count = 0; | 
|  | 147 | err = pthread_cond_init (&cond, NULL); | 
|  | 148 | if (err) | 
|  | 149 | { | 
|  | 150 | printf ("pthread_cond_init failed: %s\n", strerror (err)); | 
|  | 151 | return 1; | 
|  | 152 | } | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | for (i = 0; i < N; ++i) | 
|  | 156 | if ((err = pthread_join (th[i], NULL)) != 0) | 
|  | 157 | { | 
|  | 158 | printf ("failed to join thread %d: %s\n", i, strerror (err)); | 
|  | 159 | return 1; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | puts ("done"); | 
|  | 163 |  | 
|  | 164 | return 0; | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 |  | 
|  | 168 | #define TEST_FUNCTION do_test () | 
|  | 169 | #include "../test-skeleton.c" |