Fix failing nss/tst-nss-files-hosts-long with local resolver
[glibc.git] / htl / tests / test-6.c
blobdc2c36bc8be21770490315a77db19b2db5e3de3c
1 /* Test barriers.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <https://www.gnu.org/licenses/>. */
19 #define _GNU_SOURCE
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <error.h>
24 #include <assert.h>
25 #include <errno.h>
27 #define THREADS 500
28 #define WAITS 3
30 void *
31 dowait (void *arg)
33 pthread_barrier_t *barrier = arg;
34 int ret;
36 ret = pthread_barrier_wait (barrier);
37 printf ("%d ", pthread_self ());
38 return (void *) ret;
41 int
42 main (int argc, char **argv)
44 pthread_barrierattr_t attr;
45 pthread_barrier_t barrier;
47 int i, j;
48 error_t err;
49 pthread_t tid[THREADS];
51 int havesyncs;
53 err = pthread_barrierattr_init (&attr);
54 if (err)
55 error (1, err, "pthread_barrierattr_init");
57 err = pthread_barrierattr_getpshared (&attr, &i);
58 if (err)
59 error (1, err, "pthread_barrierattr_getpshared");
60 assert (i == PTHREAD_PROCESS_PRIVATE || i == PTHREAD_PROCESS_SHARED);
62 err = pthread_barrierattr_setpshared (&attr, PTHREAD_PROCESS_PRIVATE);
63 if (err)
64 error (1, err, "pthread_barrierattr_setpshared");
66 err = pthread_barrier_init (&barrier, &attr, THREADS + 1);
67 if (err)
68 error (1, err, "pthread_barrier_init");
70 for (j = 0; j < WAITS; j++)
73 for (i = 0; i < THREADS; i++)
75 err = pthread_create (&tid[i], 0, dowait, &barrier);
76 if (err)
77 error (1, err, "pthread_create (%d)", i);
80 printf ("Manager will now call pthread_barrier_wait.\n");
82 havesyncs
83 = pthread_barrier_wait (&barrier) == PTHREAD_BARRIER_SERIAL_THREAD
84 ? 1 : 0;
86 for (i = THREADS - 1; i >= 0; i--)
88 void *ret;
89 err = pthread_join (tid[i], &ret);
90 if (err)
91 error (1, err, "pthread_join");
93 switch ((int) ret)
95 case 0:
96 break;
98 case PTHREAD_BARRIER_SERIAL_THREAD:
99 havesyncs++;
100 break;
102 default:
103 assert (!"Unknown value returned from pthread_barrier_wait.");
104 break;
108 printf ("\n");
110 assert (havesyncs == 1);
113 return 0;