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