1 /* Test of thread-local storage in multithreaded situations.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
25 #if USE_POSIX_THREADS || USE_SOLARIS_THREADS || USE_PTH_THREADS || USE_WIN32_THREADS
28 # define TEST_POSIX_THREADS 1
30 #if USE_SOLARIS_THREADS
31 # define TEST_SOLARIS_THREADS 1
34 # define TEST_PTH_THREADS 1
37 # define TEST_WIN32_THREADS 1
40 /* Whether to help the scheduler through explicit yield().
41 Uncomment this to see if the operating system has a fair scheduler. */
42 #define EXPLICIT_YIELD 1
44 /* Whether to print debugging messages. */
45 #define ENABLE_DEBUGGING 0
47 /* Number of simultaneous threads. */
48 #define THREAD_COUNT 16
50 /* Number of operations performed in each thread. */
51 #define REPEAT_COUNT 50000
60 # define dbgprintf printf
62 # define dbgprintf if (0) printf
65 #if TEST_POSIX_THREADS
68 typedef pthread_t gl_thread_t;
69 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
72 if (pthread_create (&thread, NULL, func, arg) != 0)
76 static inline void gl_thread_join (gl_thread_t thread)
79 if (pthread_join (thread, &retval) != 0)
82 static inline void gl_thread_yield (void)
86 static inline void * gl_thread_self (void)
88 return (void *) pthread_self ();
93 typedef pth_t gl_thread_t;
94 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
96 pth_t thread = pth_spawn (NULL, func, arg);
101 static inline void gl_thread_join (gl_thread_t thread)
103 if (!pth_join (thread, NULL))
106 static inline void gl_thread_yield (void)
110 static inline void * gl_thread_self (void)
115 #if TEST_SOLARIS_THREADS
117 typedef thread_t gl_thread_t;
118 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
121 if (thr_create (NULL, 0, func, arg, 0, &thread) != 0)
125 static inline void gl_thread_join (gl_thread_t thread)
128 if (thr_join (thread, NULL, &retval) != 0)
131 static inline void gl_thread_yield (void)
135 static inline void * gl_thread_self (void)
137 return (void *) thr_self ();
140 #if TEST_WIN32_THREADS
141 # include <windows.h>
142 typedef HANDLE gl_thread_t;
143 /* Use a wrapper function, instead of adding WINAPI through a cast. */
144 struct wrapper_args { void * (*func) (void *); void *arg; };
145 static DWORD WINAPI wrapper_func (void *varg)
147 struct wrapper_args *warg = (struct wrapper_args *)varg;
148 void * (*func) (void *) = warg->func;
149 void *arg = warg->arg;
154 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
156 struct wrapper_args *warg =
157 (struct wrapper_args *) malloc (sizeof (struct wrapper_args));
165 CreateThread (NULL, 100000, wrapper_func, warg, 0, &thread_id);
171 static inline void gl_thread_join (gl_thread_t thread)
173 if (WaitForSingleObject (thread, INFINITE) == WAIT_FAILED)
175 if (!CloseHandle (thread))
178 static inline void gl_thread_yield (void)
182 static inline void * gl_thread_self (void)
184 return (void *) GetCurrentThreadId ();
188 # define yield() gl_thread_yield ()
196 /* Call yield () only with a certain probability, otherwise with GNU Pth
197 the sequence of thread activations is too predictable. */
198 if ((((unsigned int) rand () >> 3) % 4) == 0)
204 static gl_tls_key_t mykeys[KEYS_COUNT];
207 worker_thread (void *arg)
209 unsigned int id = (unsigned int) (unsigned long) arg;
211 unsigned int values[KEYS_COUNT];
213 dbgprintf ("Worker %p started\n", gl_thread_self ());
215 /* Initialize the per-thread storage. */
216 for (i = 0; i < KEYS_COUNT; i++)
218 values[i] = (((unsigned int) rand() >> 3) % 1000000) * THREAD_COUNT + id;
219 /* Hopefully no arithmetic overflow. */
220 if ((values[i] % THREAD_COUNT) != id)
225 /* Verify that the initial value is NULL. */
226 dbgprintf ("Worker %p before initial verify\n", gl_thread_self ());
227 for (i = 0; i < KEYS_COUNT; i++)
228 if (gl_tls_get (mykeys[i]) != NULL)
230 dbgprintf ("Worker %p after initial verify\n", gl_thread_self ());
233 /* Initialize the per-thread storage. */
234 dbgprintf ("Worker %p before first tls_set\n", gl_thread_self ());
235 for (i = 0; i < KEYS_COUNT; i++)
237 unsigned int *ptr = (unsigned int *) malloc (sizeof (unsigned int));
239 gl_tls_set (mykeys[i], ptr);
241 dbgprintf ("Worker %p after first tls_set\n", gl_thread_self ());
244 /* Shuffle around the pointers. */
245 for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
247 dbgprintf ("Worker %p doing value swapping\n", gl_thread_self ());
248 i = ((unsigned int) rand() >> 3) % KEYS_COUNT;
249 j = ((unsigned int) rand() >> 3) % KEYS_COUNT;
252 void *vi = gl_tls_get (mykeys[i]);
253 void *vj = gl_tls_get (mykeys[j]);
255 gl_tls_set (mykeys[i], vj);
256 gl_tls_set (mykeys[j], vi);
261 /* Verify that all the values are from this thread. */
262 dbgprintf ("Worker %p before final verify\n", gl_thread_self ());
263 for (i = 0; i < KEYS_COUNT; i++)
264 if ((*(unsigned int *) gl_tls_get (mykeys[i]) % THREAD_COUNT) != id)
266 dbgprintf ("Worker %p after final verify\n", gl_thread_self ());
269 dbgprintf ("Worker %p dying.\n", gl_thread_self ());
278 for (pass = 0; pass < 2; pass++)
280 gl_thread_t threads[THREAD_COUNT];
283 for (i = 0; i < KEYS_COUNT; i++)
284 gl_tls_key_init (mykeys[i], free);
286 for (i = KEYS_COUNT - 1; i >= 0; i--)
287 gl_tls_key_init (mykeys[i], free);
289 /* Spawn the threads. */
290 for (i = 0; i < THREAD_COUNT; i++)
291 threads[i] = gl_thread_create (worker_thread, NULL);
293 /* Wait for the threads to terminate. */
294 for (i = 0; i < THREAD_COUNT; i++)
295 gl_thread_join (threads[i]);
297 for (i = 0; i < KEYS_COUNT; i++)
298 gl_tls_key_destroy (mykeys[i]);
310 printf ("Starting test_tls ..."); fflush (stdout);
312 printf (" OK\n"); fflush (stdout);
319 /* No multithreading available. */