Use a #pragma to suppress a bogus GCC 10 warning instead of an assert [BZ 27832].
[glibc.git] / nptl / tst-cleanup4.c
blob4a275ed950d6c5f8914fd2e34be054bc58eaa2bb
1 /* Copyright (C) 2003-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 #include <pthread.h>
20 #include <shlib-compat.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 /* LinuxThreads pthread_cleanup_{push,pop} helpers. */
26 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer,
27 void (*__routine) (void *),
28 void *__arg);
29 compat_symbol_reference (libpthread, _pthread_cleanup_push,
30 _pthread_cleanup_push, GLIBC_2_0);
31 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer,
32 int __execute);
33 compat_symbol_reference (libpthread, _pthread_cleanup_pop,
34 _pthread_cleanup_pop, GLIBC_2_0);
36 static int fds[2];
37 static pthread_barrier_t b2;
38 static int global;
40 /* Defined in tst-cleanup4aux.c, never compiled with -fexceptions. */
41 extern void fn5 (void);
42 extern void fn7 (void);
43 extern void fn9 (void);
45 void
46 clh (void *arg)
48 int val = (long int) arg;
50 printf ("clh (%d)\n", val);
52 global *= val;
53 global += val;
57 static __attribute__((noinline)) void
58 fn_read (void)
60 int r = pthread_barrier_wait (&b2);
61 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
63 printf ("%s: barrier_wait failed\n", __FUNCTION__);
64 exit (1);
67 char c;
68 read (fds[0], &c, 1);
72 __attribute__((noinline)) void
73 fn0 (void)
75 pthread_cleanup_push (clh, (void *) 1l);
77 fn_read ();
79 pthread_cleanup_pop (1);
83 __attribute__((noinline)) void
84 fn1 (void)
86 /* This is the old LinuxThreads pthread_cleanup_{push,pop}. */
87 struct _pthread_cleanup_buffer b;
88 _pthread_cleanup_push (&b, clh, (void *) 2l);
90 fn0 ();
92 _pthread_cleanup_pop (&b, 1);
96 static __attribute__((noinline)) void
97 fn2 (void)
99 pthread_cleanup_push (clh, (void *) 3l);
101 fn1 ();
103 pthread_cleanup_pop (1);
107 static void *
108 tf (void *a)
110 switch ((long) a)
112 case 0:
113 fn2 ();
114 break;
115 case 1:
116 fn5 ();
117 break;
118 case 2:
119 fn7 ();
120 break;
121 case 3:
122 fn9 ();
123 break;
126 return NULL;
131 do_test (void)
133 int result = 0;
135 if (pipe (fds) != 0)
137 puts ("pipe failed");
138 exit (1);
141 if (pthread_barrier_init (&b2, NULL, 2) != 0)
143 puts ("b2 init failed");
144 exit (1);
147 const int expect[] =
149 15, /* 1 2 3 */
150 276, /* 1 4 5 6 */
151 120, /* 1 7 8 */
152 460 /* 1 2 9 10 */
155 long i;
156 for (i = 0; i < 4; ++i)
158 global = 0;
160 printf ("test %ld\n", i);
162 pthread_t th;
163 if (pthread_create (&th, NULL, tf, (void *) i) != 0)
165 puts ("create failed");
166 exit (1);
169 int e = pthread_barrier_wait (&b2);
170 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
172 printf ("%s: barrier_wait failed\n", __FUNCTION__);
173 exit (1);
176 pthread_cancel (th);
178 void *r;
179 if ((e = pthread_join (th, &r)) != 0)
181 printf ("join failed: %d\n", e);
182 _exit (1);
185 if (r != PTHREAD_CANCELED)
187 puts ("thread not canceled");
188 exit (1);
191 if (global != expect[i])
193 printf ("global = %d, expected %d\n", global, expect[i]);
194 result = 1;
198 return result;
201 #define TEST_FUNCTION do_test ()
202 #include "../test-skeleton.c"