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