]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/uclibc/lib/contrib/uclibc/test/nptl/tst-tls4.c
Update
[l4.git] / l4 / pkg / l4re-core / uclibc / lib / contrib / uclibc / test / nptl / tst-tls4.c
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Jakub Jelinek <jakub@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 <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <tls.h>
26
27 #if HAVE___THREAD && defined HAVE_TLS_MODEL_ATTRIBUTE
28
29 #define N 3
30
31 void (*test1) (void), (*test2) (void);
32
33 pthread_barrier_t b2, b3;
34
35 static void *
36 tf (void *arg)
37 {
38   int i;
39
40   for (i = 0; i <= (uintptr_t) arg; ++i)
41     {
42       int r = pthread_barrier_wait (&b3);
43       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
44         {
45           puts ("tf: barrier_wait failed");
46           exit (1);
47         }
48     }
49
50   test1 ();
51
52   for (i = 0; i < 3; ++i)
53     {
54       int r = pthread_barrier_wait (&b3);
55       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
56         {
57           puts ("tf: barrier_wait failed");
58           exit (1);
59         }
60     }
61
62   test2 ();
63
64   for (i = 0; i < 3 - (uintptr_t) arg; ++i)
65     {
66       int r = pthread_barrier_wait (&b3);
67       if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
68         {
69           puts ("tf: barrier_wait failed");
70           exit (1);
71         }
72     }
73
74   return NULL;
75 }
76
77 static void *
78 tf2 (void *arg)
79 {
80   int r = pthread_barrier_wait (&b2);
81   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
82     {
83       puts ("tf2: barrier_wait failed");
84       exit (1);
85     }
86
87   int i;
88   for (i = 0; i < N; ++i)
89     tf (arg);
90   return NULL;
91 }
92
93 int
94 do_test (void)
95 {
96   pthread_t th[2];
97   const char *modules[N]
98     = { "tst-tls4moda.so", "tst-tls4moda.so", "tst-tls4modb.so" };
99
100   if (pthread_barrier_init (&b2, NULL, 2) != 0)
101     {
102       puts ("barrier_init failed");
103       return 1;
104     }
105
106   if (pthread_barrier_init (&b3, NULL, 3) != 0)
107     {
108       puts ("barrier_init failed");
109       return 1;
110     }
111
112   if (pthread_create (&th[0], NULL, tf2, (void *) (uintptr_t) 1))
113     {
114       puts ("pthread_create failed");
115       return 1;
116     }
117
118   int r = pthread_barrier_wait (&b2);
119   if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
120     {
121       puts ("barrier_wait failed");
122       return 1;
123     }
124
125   int i;
126   for (i = 0; i < N; ++i)
127     {
128       void *h = dlopen (modules[i], RTLD_LAZY);
129       if (h == NULL)
130         {
131           printf ("dlopen failed %s\n", dlerror ());
132           return 1;
133         }
134
135       test1 = dlsym (h, "test1");
136       if (test1 == NULL)
137         {
138           printf ("dlsym for test1 failed %s\n", dlerror ());
139           return 1;
140         }
141
142       test2 = dlsym (h, "test2");
143       if (test2 == NULL)
144         {
145           printf ("dlsym for test2 failed %s\n", dlerror ());
146           return 1;
147         }
148
149       if (pthread_create (&th[1], NULL, tf, (void *) (uintptr_t) 2))
150         {
151           puts ("pthread_create failed");
152           return 1;
153         }
154
155       tf ((void *) (uintptr_t) 0);
156
157       if (pthread_join (th[1], NULL) != 0)
158         {
159           puts ("join failed");
160           return 1;
161         }
162
163       if (dlclose (h))
164         {
165           puts ("dlclose failed");
166           return 1;
167         }
168
169       printf ("test %d with %s succeeded\n", i, modules[i]);
170     }
171
172   if (pthread_join (th[0], NULL) != 0)
173     {
174       puts ("join failed");
175       return 1;
176     }
177
178   return 0;
179 }
180
181 #define TIMEOUT 5
182 #define TEST_FUNCTION do_test ()
183
184 #else
185
186 #define TEST_FUNCTION 0
187
188 #endif
189
190 #include "../test-skeleton.c"