LoongArch: Use "$fcsr0" instead of "$r0" in _FPU_{GET,SET}CW
[glibc.git] / sysdeps / pthread / tst-robust7.c
blob569d60c4f5f61b82b7bbc11d550e237b39a5fdb1
1 /* Copyright (C) 2005-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 <errno.h>
19 #include <pthread.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
25 static pthread_barrier_t b;
26 static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
27 static pthread_mutex_t m;
28 static bool first = true;
31 static void *
32 tf (void *arg)
34 long int n = (long int) arg;
36 if (pthread_mutex_lock (&m) != 0)
38 printf ("thread %ld: mutex_lock failed\n", n + 1);
39 exit (1);
42 int e = pthread_barrier_wait (&b);
43 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
45 printf ("thread %ld: barrier_wait failed\n", n + 1);
46 exit (1);
49 e = pthread_cond_wait (&c, &m);
50 if (first)
52 if (e != 0)
54 printf ("thread %ld: cond_wait failed\n", n + 1);
55 exit (1);
57 first = false;
59 else
61 if (e != EOWNERDEAD)
63 printf ("thread %ld: cond_wait did not return EOWNERDEAD\n", n + 1);
64 exit (1);
68 if (pthread_cancel (pthread_self ()) != 0)
70 printf ("thread %ld: cancel failed\n", n + 1);
71 exit (1);
74 pthread_testcancel ();
76 printf ("thread %ld: testcancel returned\n", n + 1);
77 exit (1);
81 static int
82 do_test (void)
84 pthread_mutexattr_t a;
85 if (pthread_mutexattr_init (&a) != 0)
87 puts ("mutexattr_init failed");
88 return 1;
91 if (pthread_mutexattr_setrobust (&a, PTHREAD_MUTEX_ROBUST_NP) != 0)
93 puts ("mutexattr_setrobust failed");
94 return 1;
97 #ifdef ENABLE_PI
98 if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
100 puts ("pthread_mutexattr_setprotocol failed");
101 return 1;
103 #endif
105 int e;
106 e = pthread_mutex_init (&m, &a);
107 if (e != 0)
109 #ifdef ENABLE_PI
110 if (e == ENOTSUP)
112 puts ("PI robust mutexes not supported");
113 return 0;
115 #endif
116 puts ("mutex_init failed");
117 return 1;
120 if (pthread_mutexattr_destroy (&a) != 0)
122 puts ("mutexattr_destroy failed");
123 return 1;
126 if (pthread_barrier_init (&b, NULL, 2) != 0)
128 puts ("barrier_init failed");
129 return 1;
132 #define N 5
133 pthread_t th[N];
134 for (long int n = 0; n < N; ++n)
136 if (pthread_create (&th[n], NULL, tf, (void *) n) != 0)
138 printf ("pthread_create loop %ld failed\n", n + 1);
139 return 1;
142 e = pthread_barrier_wait (&b);
143 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
145 printf ("parent: barrier_wait failed in round %ld\n", n + 1);
146 return 1;
150 if (pthread_mutex_lock (&m) != 0)
152 puts ("parent: mutex_lock failed");
153 return 1;
156 if (pthread_mutex_unlock (&m) != 0)
158 puts ("parent: mutex_unlock failed");
159 return 1;
162 if (pthread_cond_broadcast (&c) != 0)
164 puts ("cond_broadcast failed");
165 return 1;
168 for (int n = 0; n < N; ++n)
170 void *res;
171 if (pthread_join (th[n], &res) != 0)
173 printf ("join round %d failed\n", n + 1);
174 return 1;
176 if (res != PTHREAD_CANCELED)
178 printf ("thread %d not canceled\n", n + 1);
179 return 1;
183 e = pthread_mutex_lock (&m);
184 if (e == 0)
186 puts ("parent: 2nd mutex_lock succeeded");
187 return 1;
189 if (e != EOWNERDEAD)
191 puts ("parent: mutex_lock did not return EOWNERDEAD");
192 return 1;
195 if (pthread_mutex_unlock (&m) != 0)
197 puts ("parent: 2nd mutex_unlock failed");
198 return 1;
201 if (pthread_mutex_destroy (&m) != 0)
203 puts ("mutex_destroy failed");
204 return 1;
207 return 0;
210 #define TEST_FUNCTION do_test ()
211 #include "../test-skeleton.c"