LoongArch: Use "$fcsr0" instead of "$r0" in _FPU_{GET,SET}CW
[glibc.git] / sysdeps / pthread / tst-cond20.c
blob141587465362872d0185c056b838361d9a13c419
1 /* Copyright (C) 2004-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <pthread.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
24 #define N 10
25 #define ROUNDS 1000
26 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
27 static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
28 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
29 static pthread_barrier_t b;
30 static int count;
32 static void *
33 tf (void *p)
35 int i;
36 for (i = 0; i < ROUNDS; ++i)
38 pthread_mutex_lock (&mut);
40 if (++count == N)
41 pthread_cond_signal (&cond2);
43 #ifdef TIMED
44 struct timeval tv;
45 gettimeofday (&tv, NULL);
46 struct timespec ts;
47 /* Wait three seconds. */
48 ts.tv_sec = tv.tv_sec + 3;
49 ts.tv_nsec = tv.tv_usec * 1000;
50 pthread_cond_timedwait (&cond, &mut, &ts);
51 #else
52 pthread_cond_wait (&cond, &mut);
53 #endif
55 pthread_mutex_unlock (&mut);
57 int err = pthread_barrier_wait (&b);
58 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
60 puts ("child: barrier_wait failed");
61 exit (1);
64 err = pthread_barrier_wait (&b);
65 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
67 puts ("child: barrier_wait failed");
68 exit (1);
72 return NULL;
76 static int
77 do_test (void)
79 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
81 puts ("barrier_init failed");
82 return 1;
85 pthread_mutex_lock (&mut);
87 int i, j, err;
88 pthread_t th[N];
89 for (i = 0; i < N; ++i)
90 if ((err = pthread_create (&th[i], NULL, tf, NULL)) != 0)
92 printf ("cannot create thread %d: %s\n", i, strerror (err));
93 return 1;
96 for (i = 0; i < ROUNDS; ++i)
98 /* Make sure we discard spurious wake-ups. */
100 pthread_cond_wait (&cond2, &mut);
101 while (count != N);
103 if (i & 1)
104 pthread_mutex_unlock (&mut);
106 if (i & 2)
107 pthread_cond_broadcast (&cond);
108 else if (i & 4)
109 for (j = 0; j < N; ++j)
110 pthread_cond_signal (&cond);
111 else
113 for (j = 0; j < (i / 8) % N; ++j)
114 pthread_cond_signal (&cond);
115 pthread_cond_broadcast (&cond);
118 if ((i & 1) == 0)
119 pthread_mutex_unlock (&mut);
121 err = pthread_cond_destroy (&cond);
122 if (err)
124 printf ("pthread_cond_destroy failed: %s\n", strerror (err));
125 return 1;
128 /* Now clobber the cond variable which has been successfully
129 destroyed above. */
130 memset (&cond, (char) i, sizeof (cond));
132 err = pthread_barrier_wait (&b);
133 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
135 puts ("parent: barrier_wait failed");
136 return 1;
139 pthread_mutex_lock (&mut);
141 err = pthread_barrier_wait (&b);
142 if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
144 puts ("parent: barrier_wait failed");
145 return 1;
148 count = 0;
149 err = pthread_cond_init (&cond, NULL);
150 if (err)
152 printf ("pthread_cond_init failed: %s\n", strerror (err));
153 return 1;
157 for (i = 0; i < N; ++i)
158 if ((err = pthread_join (th[i], NULL)) != 0)
160 printf ("failed to join thread %d: %s\n", i, strerror (err));
161 return 1;
164 puts ("done");
166 return 0;
170 #define TEST_FUNCTION do_test ()
171 #include "../test-skeleton.c"