Unconditionally include <config.h> in unit tests.
[gnulib.git] / tests / test-tls.c
1 /* Test of thread-local storage in multithreaded situations.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2005.  */
19
20 #include <config.h>
21
22 #if USE_POSIX_THREADS || USE_SOLARIS_THREADS || USE_PTH_THREADS || USE_WIN32_THREADS
23
24 #if USE_POSIX_THREADS
25 # define TEST_POSIX_THREADS 1
26 #endif
27 #if USE_SOLARIS_THREADS
28 # define TEST_SOLARIS_THREADS 1
29 #endif
30 #if USE_PTH_THREADS
31 # define TEST_PTH_THREADS 1
32 #endif
33 #if USE_WIN32_THREADS
34 # define TEST_WIN32_THREADS 1
35 #endif
36
37 /* Whether to help the scheduler through explicit yield().
38    Uncomment this to see if the operating system has a fair scheduler.  */
39 #define EXPLICIT_YIELD 1
40
41 /* Whether to print debugging messages.  */
42 #define ENABLE_DEBUGGING 0
43
44 /* Number of simultaneous threads.  */
45 #define THREAD_COUNT 16
46
47 /* Number of operations performed in each thread.  */
48 #define REPEAT_COUNT 50000
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "tls.h"
55
56 #if ENABLE_DEBUGGING
57 # define dbgprintf printf
58 #else
59 # define dbgprintf if (0) printf
60 #endif
61
62 #if TEST_POSIX_THREADS
63 # include <pthread.h>
64 # include <sched.h>
65 typedef pthread_t gl_thread_t;
66 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
67 {
68   pthread_t thread;
69   if (pthread_create (&thread, NULL, func, arg) != 0)
70     abort ();
71   return thread;
72 }
73 static inline void gl_thread_join (gl_thread_t thread)
74 {
75   void *retval;
76   if (pthread_join (thread, &retval) != 0)
77     abort ();
78 }
79 static inline void gl_thread_yield (void)
80 {
81   sched_yield ();
82 }
83 static inline void * gl_thread_self (void)
84 {
85   return (void *) pthread_self ();
86 }
87 #endif
88 #if TEST_PTH_THREADS
89 # include <pth.h>
90 typedef pth_t gl_thread_t;
91 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
92 {
93   pth_t thread = pth_spawn (NULL, func, arg);
94   if (thread == NULL)
95     abort ();
96   return thread;
97 }
98 static inline void gl_thread_join (gl_thread_t thread)
99 {
100   if (!pth_join (thread, NULL))
101     abort ();
102 }
103 static inline void gl_thread_yield (void)
104 {
105   pth_yield (NULL);
106 }
107 static inline void * gl_thread_self (void)
108 {
109   return pth_self ();
110 }
111 #endif
112 #if TEST_SOLARIS_THREADS
113 # include <thread.h>
114 typedef thread_t gl_thread_t;
115 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
116 {
117   thread_t thread;
118   if (thr_create (NULL, 0, func, arg, 0, &thread) != 0)
119     abort ();
120   return thread;
121 }
122 static inline void gl_thread_join (gl_thread_t thread)
123 {
124   void *retval;
125   if (thr_join (thread, NULL, &retval) != 0)
126     abort ();
127 }
128 static inline void gl_thread_yield (void)
129 {
130   thr_yield ();
131 }
132 static inline void * gl_thread_self (void)
133 {
134   return (void *) thr_self ();
135 }
136 #endif
137 #if TEST_WIN32_THREADS
138 # include <windows.h>
139 typedef HANDLE gl_thread_t;
140 /* Use a wrapper function, instead of adding WINAPI through a cast.  */
141 struct wrapper_args { void * (*func) (void *); void *arg; };
142 static DWORD WINAPI wrapper_func (void *varg)
143 {
144   struct wrapper_args *warg = (struct wrapper_args *)varg;
145   void * (*func) (void *) = warg->func;
146   void *arg = warg->arg;
147   free (warg);
148   func (arg);
149   return 0;
150 }
151 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
152 {
153   struct wrapper_args *warg =
154     (struct wrapper_args *) malloc (sizeof (struct wrapper_args));
155   if (warg == NULL)
156     abort ();
157   warg->func = func;
158   warg->arg = arg;
159   {
160     DWORD thread_id;
161     HANDLE thread =
162       CreateThread (NULL, 100000, wrapper_func, warg, 0, &thread_id);
163     if (thread == NULL)
164       abort ();
165     return thread;
166   }
167 }
168 static inline void gl_thread_join (gl_thread_t thread)
169 {
170   if (WaitForSingleObject (thread, INFINITE) == WAIT_FAILED)
171     abort ();
172   if (!CloseHandle (thread))
173     abort ();
174 }
175 static inline void gl_thread_yield (void)
176 {
177   Sleep (0);
178 }
179 static inline void * gl_thread_self (void)
180 {
181   return (void *) GetCurrentThreadId ();
182 }
183 #endif
184 #if EXPLICIT_YIELD
185 # define yield() gl_thread_yield ()
186 #else
187 # define yield()
188 #endif
189
190 static inline void
191 perhaps_yield (void)
192 {
193   /* Call yield () only with a certain probability, otherwise with GNU Pth
194      the sequence of thread activations is too predictable.  */
195   if ((((unsigned int) rand () >> 3) % 4) == 0)
196     yield ();
197 }
198
199 #define KEYS_COUNT 4
200
201 static gl_tls_key_t mykeys[KEYS_COUNT];
202
203 static void *
204 worker_thread (void *arg)
205 {
206   unsigned int id = (unsigned int) (unsigned long) arg;
207   int i, j, repeat;
208   unsigned int values[KEYS_COUNT];
209
210   dbgprintf ("Worker %p started\n", gl_thread_self ());
211
212   /* Initialize the per-thread storage.  */
213   for (i = 0; i < KEYS_COUNT; i++)
214     {
215       values[i] = (((unsigned int) rand() >> 3) % 1000000) * THREAD_COUNT + id;
216       /* Hopefully no arithmetic overflow.  */
217       if ((values[i] % THREAD_COUNT) != id)
218         abort ();
219     }
220   perhaps_yield ();
221
222   /* Verify that the initial value is NULL.  */
223   dbgprintf ("Worker %p before initial verify\n", gl_thread_self ());
224   for (i = 0; i < KEYS_COUNT; i++)
225     if (gl_tls_get (mykeys[i]) != NULL)
226       abort ();
227   dbgprintf ("Worker %p after  initial verify\n", gl_thread_self ());
228   perhaps_yield ();
229
230   /* Initialize the per-thread storage.  */
231   dbgprintf ("Worker %p before first tls_set\n", gl_thread_self ());
232   for (i = 0; i < KEYS_COUNT; i++)
233     {
234       unsigned int *ptr = (unsigned int *) malloc (sizeof (unsigned int));
235       *ptr = values[i];
236       gl_tls_set (mykeys[i], ptr);
237     }
238   dbgprintf ("Worker %p after  first tls_set\n", gl_thread_self ());
239   perhaps_yield ();
240
241   /* Shuffle around the pointers.  */
242   for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
243     {
244       dbgprintf ("Worker %p doing value swapping\n", gl_thread_self ());
245       i = ((unsigned int) rand() >> 3) % KEYS_COUNT;
246       j = ((unsigned int) rand() >> 3) % KEYS_COUNT;
247       if (i != j)
248         {
249           void *vi = gl_tls_get (mykeys[i]);
250           void *vj = gl_tls_get (mykeys[j]);
251
252           gl_tls_set (mykeys[i], vj);
253           gl_tls_set (mykeys[j], vi);
254         }
255       perhaps_yield ();
256     }
257
258   /* Verify that all the values are from this thread.  */
259   dbgprintf ("Worker %p before final verify\n", gl_thread_self ());
260   for (i = 0; i < KEYS_COUNT; i++)
261     if ((*(unsigned int *) gl_tls_get (mykeys[i]) % THREAD_COUNT) != id)
262       abort ();
263   dbgprintf ("Worker %p after  final verify\n", gl_thread_self ());
264   perhaps_yield ();
265
266   dbgprintf ("Worker %p dying.\n", gl_thread_self ());
267   return NULL;
268 }
269
270 void
271 test_tls (void)
272 {
273   int pass, i;
274
275   for (pass = 0; pass < 2; pass++)
276     {
277       gl_thread_t threads[THREAD_COUNT];
278
279       if (pass == 0)
280         for (i = 0; i < KEYS_COUNT; i++)
281           gl_tls_key_init (mykeys[i], free);
282       else
283         for (i = KEYS_COUNT - 1; i >= 0; i--)
284           gl_tls_key_init (mykeys[i], free);
285
286       /* Spawn the threads.  */
287       for (i = 0; i < THREAD_COUNT; i++)
288         threads[i] = gl_thread_create (worker_thread, NULL);
289
290       /* Wait for the threads to terminate.  */
291       for (i = 0; i < THREAD_COUNT; i++)
292         gl_thread_join (threads[i]);
293
294       for (i = 0; i < KEYS_COUNT; i++)
295         gl_tls_key_destroy (mykeys[i]);
296     }
297 }
298
299 int
300 main ()
301 {
302 #if TEST_PTH_THREADS
303   if (!pth_init ())
304     abort ();
305 #endif
306
307   printf ("Starting test_tls ..."); fflush (stdout);
308   test_tls ();
309   printf (" OK\n"); fflush (stdout);
310
311   return 0;
312 }
313
314 #else
315
316 /* No multithreading available.  */
317
318 int
319 main ()
320 {
321   return 77;
322 }
323
324 #endif