WP issues
[glibc.git] / nptl / tst-tls2.c
blobff3ab3a024c63af4f9d1f5ad07289e7e103598f7
1 /* Copyright (C) 2003, 2004, 2011 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include <semaphore.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
29 #define N 10
30 static pthread_t th[N];
33 #define CB(n) \
34 static void \
35 cb##n (void) \
36 { \
37 if (th[n] != pthread_self ()) \
38 { \
39 write (STDOUT_FILENO, "wrong callback\n", 15); \
40 _exit (1); \
41 } \
43 CB (0)
44 CB (1)
45 CB (2)
46 CB (3)
47 CB (4)
48 CB (5)
49 CB (6)
50 CB (7)
51 CB (8)
52 CB (9)
53 static void (*cbs[]) (void) =
55 cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
59 static __thread void (*fp) (void) __attribute__ ((tls_model ("local-exec")));
62 static sem_t s;
65 #define THE_SIG SIGUSR1
66 static void
67 handler (int sig)
69 if (sig != THE_SIG)
71 write (STDOUT_FILENO, "wrong signal\n", 13);
72 _exit (1);
75 fp ();
77 if (sem_post (&s) != 0)
79 write (STDOUT_FILENO, "sem_post failed\n", 16);
80 _exit (1);
85 static pthread_barrier_t b;
87 #define TOTAL_SIGS 1000
88 static int nsigs;
91 static void *
92 tf (void *arg)
94 fp = arg;
96 pthread_barrier_wait (&b);
98 pthread_barrier_wait (&b);
100 if (nsigs != TOTAL_SIGS)
102 puts ("barrier_wait prematurely returns");
103 exit (1);
106 return NULL;
111 do_test (void)
113 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
115 puts ("barrier_init failed");
116 exit (1);
119 if (sem_init (&s, 0, 0) != 0)
121 puts ("sem_init failed");
122 exit (1);
125 struct sigaction sa;
126 sa.sa_handler = handler;
127 sigemptyset (&sa.sa_mask);
128 sa.sa_flags = 0;
129 if (sigaction (THE_SIG, &sa, NULL) != 0)
131 puts ("sigaction failed");
132 exit (1);
135 pthread_attr_t a;
137 if (pthread_attr_init (&a) != 0)
139 puts ("attr_init failed");
140 exit (1);
143 if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
145 puts ("attr_setstacksize failed");
146 return 1;
149 int i;
150 for (i = 0; i < N; ++i)
151 if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
153 puts ("pthread_create failed");
154 exit (1);
157 if (pthread_attr_destroy (&a) != 0)
159 puts ("attr_destroy failed");
160 exit (1);
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 for (i = 0; i < N; ++i)
195 if (pthread_join (th[i], NULL) != 0)
197 puts ("join failed");
198 exit (1);
201 return 0;
205 #define TEST_FUNCTION do_test ()
206 #include "../test-skeleton.c"