Prevent an implicit int promotion in malloc/tst-alloc_buffer.c
[glibc.git] / nptl / tst-tls3.c
blobf768e88d54206672047931f770c2cb3d6a904257
1 /* Copyright (C) 2003-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@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 <http://www.gnu.org/licenses/>. */
19 #include <dlfcn.h>
20 #include <errno.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include <semaphore.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <pthreaddef.h>
30 #define THE_SIG SIGUSR1
32 /* The stack size can be overriden. With a sufficiently large stack
33 size, thread stacks for terminated threads are freed, but this does
34 not happen with the default size of 1 MiB. */
35 enum { default_stack_size_in_mb = 1 };
36 static long stack_size_in_mb;
38 #define N 10
39 static pthread_t th[N];
42 static int do_test (void);
44 #define TIMEOUT 5
45 #define TEST_FUNCTION do_test ()
46 #include "../test-skeleton.c"
48 #define CB(n) \
49 static void \
50 cb##n (void) \
51 { \
52 if (th[n] != pthread_self ()) \
53 { \
54 write_message ("wrong callback\n"); \
55 _exit (1); \
56 } \
58 CB (0)
59 CB (1)
60 CB (2)
61 CB (3)
62 CB (4)
63 CB (5)
64 CB (6)
65 CB (7)
66 CB (8)
67 CB (9)
68 static void (*cbs[]) (void) =
70 cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
74 sem_t s;
77 pthread_barrier_t b;
79 #define TOTAL_SIGS 1000
80 int nsigs;
83 int
84 do_test (void)
86 if (stack_size_in_mb == 0)
87 stack_size_in_mb = default_stack_size_in_mb;
89 if ((uintptr_t) pthread_self () & (TCB_ALIGNMENT - 1))
91 puts ("initial thread's struct pthread not aligned enough");
92 exit (1);
95 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
97 puts ("barrier_init failed");
98 exit (1);
101 if (sem_init (&s, 0, 0) != 0)
103 puts ("sem_init failed");
104 exit (1);
107 void *h = dlopen ("tst-tls3mod.so", RTLD_LAZY);
108 if (h == NULL)
110 puts ("dlopen failed");
111 exit (1);
114 void *(*tf) (void *) = dlsym (h, "tf");
115 if (tf == NULL)
117 puts ("dlsym for tf failed");
118 exit (1);
121 struct sigaction sa;
122 sa.sa_handler = dlsym (h, "handler");
123 if (sa.sa_handler == NULL)
125 puts ("dlsym for handler failed");
126 exit (1);
128 sigemptyset (&sa.sa_mask);
129 sa.sa_flags = 0;
130 if (sigaction (THE_SIG, &sa, NULL) != 0)
132 puts ("sigaction failed");
133 exit (1);
136 pthread_attr_t a;
138 if (pthread_attr_init (&a) != 0)
140 puts ("attr_init failed");
141 exit (1);
144 if (pthread_attr_setstacksize (&a, stack_size_in_mb * 1024 * 1024) != 0)
146 puts ("attr_setstacksize failed");
147 return 1;
150 int r;
151 for (r = 0; r < 10; ++r)
153 int i;
154 for (i = 0; i < N; ++i)
155 if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
157 puts ("pthread_create failed");
158 exit (1);
161 nsigs = 0;
163 pthread_barrier_wait (&b);
165 sigset_t ss;
166 sigemptyset (&ss);
167 sigaddset (&ss, THE_SIG);
168 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
170 puts ("pthread_sigmask failed");
171 exit (1);
174 /* Start sending signals. */
175 for (i = 0; i < TOTAL_SIGS; ++i)
177 if (kill (getpid (), THE_SIG) != 0)
179 puts ("kill failed");
180 exit (1);
183 if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
185 puts ("sem_wait failed");
186 exit (1);
189 ++nsigs;
192 pthread_barrier_wait (&b);
194 if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
196 puts ("pthread_sigmask failed");
197 exit (1);
200 for (i = 0; i < N; ++i)
201 if (pthread_join (th[i], NULL) != 0)
203 puts ("join failed");
204 exit (1);
208 if (pthread_attr_destroy (&a) != 0)
210 puts ("attr_destroy failed");
211 exit (1);
214 return 0;