]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-tls3.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nptl / tst-tls3.c
1 /* Copyright (C) 2003-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
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.
9
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.
14
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/>. */
18
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>
29
30 #define THE_SIG SIGUSR1
31
32
33 #define N 10
34 static pthread_t th[N];
35
36
37 #define CB(n) \
38 static void \
39 cb##n (void) \
40 { \
41 if (th[n] != pthread_self ()) \
42 { \
43 write (STDOUT_FILENO, "wrong callback\n", 15); \
44 _exit (1); \
45 } \
46 }
47 CB (0)
48 CB (1)
49 CB (2)
50 CB (3)
51 CB (4)
52 CB (5)
53 CB (6)
54 CB (7)
55 CB (8)
56 CB (9)
57 static void (*cbs[]) (void) =
58 {
59 cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
60 };
61
62
63 sem_t s;
64
65
66 pthread_barrier_t b;
67
68 #define TOTAL_SIGS 1000
69 int nsigs;
70
71
72 int
73 do_test (void)
74 {
75 if ((uintptr_t) pthread_self () & (TCB_ALIGNMENT - 1))
76 {
77 puts ("initial thread's struct pthread not aligned enough");
78 exit (1);
79 }
80
81 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
82 {
83 puts ("barrier_init failed");
84 exit (1);
85 }
86
87 if (sem_init (&s, 0, 0) != 0)
88 {
89 puts ("sem_init failed");
90 exit (1);
91 }
92
93 void *h = dlopen ("tst-tls3mod.so", RTLD_LAZY);
94 if (h == NULL)
95 {
96 puts ("dlopen failed");
97 exit (1);
98 }
99
100 void *(*tf) (void *) = dlsym (h, "tf");
101 if (tf == NULL)
102 {
103 puts ("dlsym for tf failed");
104 exit (1);
105 }
106
107 struct sigaction sa;
108 sa.sa_handler = dlsym (h, "handler");
109 if (sa.sa_handler == NULL)
110 {
111 puts ("dlsym for handler failed");
112 exit (1);
113 }
114 sigemptyset (&sa.sa_mask);
115 sa.sa_flags = 0;
116 if (sigaction (THE_SIG, &sa, NULL) != 0)
117 {
118 puts ("sigaction failed");
119 exit (1);
120 }
121
122 pthread_attr_t a;
123
124 if (pthread_attr_init (&a) != 0)
125 {
126 puts ("attr_init failed");
127 exit (1);
128 }
129
130 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
131 {
132 puts ("attr_setstacksize failed");
133 return 1;
134 }
135
136 int r;
137 for (r = 0; r < 10; ++r)
138 {
139 int i;
140 for (i = 0; i < N; ++i)
141 if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
142 {
143 puts ("pthread_create failed");
144 exit (1);
145 }
146
147 nsigs = 0;
148
149 pthread_barrier_wait (&b);
150
151 sigset_t ss;
152 sigemptyset (&ss);
153 sigaddset (&ss, THE_SIG);
154 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
155 {
156 puts ("pthread_sigmask failed");
157 exit (1);
158 }
159
160 /* Start sending signals. */
161 for (i = 0; i < TOTAL_SIGS; ++i)
162 {
163 if (kill (getpid (), THE_SIG) != 0)
164 {
165 puts ("kill failed");
166 exit (1);
167 }
168
169 if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
170 {
171 puts ("sem_wait failed");
172 exit (1);
173 }
174
175 ++nsigs;
176 }
177
178 pthread_barrier_wait (&b);
179
180 if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
181 {
182 puts ("pthread_sigmask failed");
183 exit (1);
184 }
185
186 for (i = 0; i < N; ++i)
187 if (pthread_join (th[i], NULL) != 0)
188 {
189 puts ("join failed");
190 exit (1);
191 }
192 }
193
194 if (pthread_attr_destroy (&a) != 0)
195 {
196 puts ("attr_destroy failed");
197 exit (1);
198 }
199
200 return 0;
201 }
202
203
204 #define TIMEOUT 5
205 #define TEST_FUNCTION do_test ()
206 #include "../test-skeleton.c"