2003-09-05 Roland McGrath <roland@redhat.com>
[glibc.git] / nptl / tst-tls3.c
blob20ee3e30c2168ad69bed0317d4700822fcba1862
1 /* Copyright (C) 2003 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 <dlfcn.h>
21 #include <errno.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include <semaphore.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <pthreaddef.h>
31 #define THE_SIG SIGUSR1
34 #define N 10
35 static pthread_t th[N];
38 #define CB(n) \
39 static void \
40 cb##n (void) \
41 { \
42 if (th[n] != pthread_self ()) \
43 { \
44 write (STDOUT_FILENO, "wrong callback\n", 15); \
45 _exit (1); \
46 } \
48 CB (0)
49 CB (1)
50 CB (2)
51 CB (3)
52 CB (4)
53 CB (5)
54 CB (6)
55 CB (7)
56 CB (8)
57 CB (9)
58 static void (*cbs[]) (void) =
60 cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
64 sem_t s;
67 pthread_barrier_t b;
69 #define TOTAL_SIGS 1000
70 int nsigs;
73 int
74 do_test (void)
76 #if !HAVE___THREAD
78 puts ("No __thread support in compiler, test skipped.");
80 return 0;
81 #else
83 if ((uintptr_t) pthread_self () & (TCB_ALIGNMENT - 1))
85 puts ("initial thread's struct pthread not aligned enough");
86 exit (1);
89 if (pthread_barrier_init (&b, NULL, N + 1) != 0)
91 puts ("barrier_init failed");
92 exit (1);
95 if (sem_init (&s, 0, 0) != 0)
97 puts ("sem_init failed");
98 exit (1);
101 void *h = dlopen ("tst-tls3mod.so", RTLD_LAZY);
102 if (h == NULL)
104 puts ("dlopen failed");
105 exit (1);
108 void *(*tf) (void *) = dlsym (h, "tf");
109 if (tf == NULL)
111 puts ("dlsym for tf failed");
112 exit (1);
115 struct sigaction sa;
116 sa.sa_handler = dlsym (h, "handler");
117 if (sa.sa_handler == NULL)
119 puts ("dlsym for handler failed");
120 exit (1);
122 sigemptyset (&sa.sa_mask);
123 sa.sa_flags = 0;
124 if (sigaction (THE_SIG, &sa, NULL) != 0)
126 puts ("sigaction failed");
127 exit (1);
130 int r;
131 for (r = 0; r < 10; ++r)
133 int i;
134 for (i = 0; i < N; ++i)
135 if (pthread_create (&th[i], NULL, tf, cbs[i]) != 0)
137 puts ("pthread_create failed");
138 exit (1);
141 nsigs = 0;
143 pthread_barrier_wait (&b);
145 sigset_t ss;
146 sigemptyset (&ss);
147 sigaddset (&ss, THE_SIG);
148 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
150 puts ("pthread_sigmask failed");
151 exit (1);
154 /* Start sending signals. */
155 for (i = 0; i < TOTAL_SIGS; ++i)
157 if (kill (getpid (), THE_SIG) != 0)
159 puts ("kill failed");
160 exit (1);
163 if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
165 puts ("sem_wait failed");
166 exit (1);
169 ++nsigs;
172 pthread_barrier_wait (&b);
174 if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
176 puts ("pthread_sigmask failed");
177 exit (1);
180 for (i = 0; i < N; ++i)
181 if (pthread_join (th[i], NULL) != 0)
183 puts ("join failed");
184 exit (1);
188 return 0;
189 #endif
193 #define TIMEOUT 5
194 #define TEST_FUNCTION do_test ()
195 #include "../test-skeleton.c"