OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / test / nptl / tst-tls2.c
1 /* Copyright (C) 2003, 2004 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 <errno.h>
20 #include <pthread.h>
21 #include <signal.h>
22 #include <semaphore.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <tls.h>
27
28 #if HAVE___THREAD
29
30 #define N 10
31 static pthread_t th[N];
32
33
34 #define CB(n) \
35 static void                                                                   \
36 cb##n (void)                                                                  \
37 {                                                                             \
38   if (th[n] != pthread_self ())                                               \
39     {                                                                         \
40       write (STDOUT_FILENO, "wrong callback\n", 15);                          \
41       _exit (1);                                                              \
42     }                                                                         \
43 }
44 CB (0)
45 CB (1)
46 CB (2)
47 CB (3)
48 CB (4)
49 CB (5)
50 CB (6)
51 CB (7)
52 CB (8)
53 CB (9)
54 static void (*cbs[]) (void) =
55 {
56   cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
57 };
58
59
60 static __thread void (*fp) (void) __attribute__ ((tls_model ("local-exec")));
61
62
63 static sem_t s;
64
65
66 #define THE_SIG SIGUSR1
67 static void
68 handler (int sig)
69 {
70   if (sig != THE_SIG)
71     {
72       write (STDOUT_FILENO, "wrong signal\n", 13);
73       _exit (1);
74     }
75
76   fp ();
77
78   if (sem_post (&s) != 0)
79     {
80       write (STDOUT_FILENO, "sem_post failed\n", 16);
81       _exit (1);
82     }
83 }
84
85
86 static pthread_barrier_t b;
87
88 #define TOTAL_SIGS 1000
89 static int nsigs;
90
91
92 static void *
93 tf (void *arg)
94 {
95   fp = arg;
96
97   pthread_barrier_wait (&b);
98
99   pthread_barrier_wait (&b);
100
101   if (nsigs != TOTAL_SIGS)
102     {
103       puts ("barrier_wait prematurely returns");
104       exit (1);
105     }
106
107   return NULL;
108 }
109 #endif
110
111 int
112 do_test (void)
113 {
114 #if !HAVE___THREAD
115
116   puts ("No __thread support in compiler, test skipped.");
117
118   return 0;
119 #else
120
121   if (pthread_barrier_init (&b, NULL, N + 1) != 0)
122     {
123       puts ("barrier_init failed");
124       exit (1);
125     }
126
127   if (sem_init (&s, 0, 0) != 0)
128     {
129       puts ("sem_init failed");
130       exit (1);
131     }
132
133   struct sigaction sa;
134   sa.sa_handler = handler;
135   sigemptyset (&sa.sa_mask);
136   sa.sa_flags = 0;
137   if (sigaction (THE_SIG, &sa, NULL) != 0)
138     {
139       puts ("sigaction failed");
140       exit (1);
141     }
142
143   pthread_attr_t a;
144
145   if (pthread_attr_init (&a) != 0)
146     {
147       puts ("attr_init failed");
148       exit (1);
149     }
150
151   if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
152     {
153       puts ("attr_setstacksize failed");
154       return 1;
155     }
156
157   int i;
158   for (i = 0; i < N; ++i)
159     if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
160       {
161         puts ("pthread_create failed");
162         exit (1);
163       }
164
165   if (pthread_attr_destroy (&a) != 0)
166     {
167       puts ("attr_destroy failed");
168       exit (1);
169     }
170
171   pthread_barrier_wait (&b);
172
173   sigset_t ss;
174   sigemptyset (&ss);
175   sigaddset (&ss, THE_SIG);
176   if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
177     {
178       puts ("pthread_sigmask failed");
179       exit (1);
180     }
181
182   /* Start sending signals.  */
183   for (i = 0; i < TOTAL_SIGS; ++i)
184     {
185       if (kill (getpid (), THE_SIG) != 0)
186         {
187           puts ("kill failed");
188           exit (1);
189         }
190
191       if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
192         {
193           puts ("sem_wait failed");
194           exit (1);
195         }
196
197       ++nsigs;
198     }
199
200   pthread_barrier_wait (&b);
201
202   for (i = 0; i < N; ++i)
203     if (pthread_join (th[i], NULL) != 0)
204       {
205         puts ("join failed");
206         exit (1);
207       }
208
209   return 0;
210 #endif
211 }
212
213
214 #define TEST_FUNCTION do_test ()
215 #include "../test-skeleton.c"