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
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)
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.
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. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
24 #if USE_POSIX_THREADS || USE_SOLARIS_THREADS || USE_PTH_THREADS || USE_WIN32_THREADS
27 # define TEST_POSIX_THREADS 1
29 #if USE_SOLARIS_THREADS
30 # define TEST_SOLARIS_THREADS 1
33 # define TEST_PTH_THREADS 1
36 # define TEST_WIN32_THREADS 1
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
43 /* Whether to print debugging messages. */
44 #define ENABLE_DEBUGGING 0
46 /* Number of simultaneous threads. */
47 #define THREAD_COUNT 16
49 /* Number of operations performed in each thread. */
50 #define REPEAT_COUNT 50000
59 # define dbgprintf printf
61 # define dbgprintf if (0) printf
64 #if TEST_POSIX_THREADS
67 typedef pthread_t gl_thread_t;
68 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
71 if (pthread_create (&thread, NULL, func, arg) != 0)
75 static inline void gl_thread_join (gl_thread_t thread)
78 if (pthread_join (thread, &retval) != 0)
81 static inline void gl_thread_yield (void)
85 static inline void * gl_thread_self (void)
87 return (void *) pthread_self ();
92 typedef pth_t gl_thread_t;
93 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
95 pth_t thread = pth_spawn (NULL, func, arg);
100 static inline void gl_thread_join (gl_thread_t thread)
102 if (!pth_join (thread, NULL))
105 static inline void gl_thread_yield (void)
109 static inline void * gl_thread_self (void)
114 #if TEST_SOLARIS_THREADS
116 typedef thread_t gl_thread_t;
117 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
120 if (thr_create (NULL, 0, func, arg, 0, &thread) != 0)
124 static inline void gl_thread_join (gl_thread_t thread)
127 if (thr_join (thread, NULL, &retval) != 0)
130 static inline void gl_thread_yield (void)
134 static inline void * gl_thread_self (void)
136 return (void *) thr_self ();
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)
146 struct wrapper_args *warg = (struct wrapper_args *)varg;
147 void * (*func) (void *) = warg->func;
148 void *arg = warg->arg;
153 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
155 struct wrapper_args *warg =
156 (struct wrapper_args *) malloc (sizeof (struct wrapper_args));
164 CreateThread (NULL, 100000, wrapper_func, warg, 0, &thread_id);
170 static inline void gl_thread_join (gl_thread_t thread)
172 if (WaitForSingleObject (thread, INFINITE) == WAIT_FAILED)
174 if (!CloseHandle (thread))
177 static inline void gl_thread_yield (void)
181 static inline void * gl_thread_self (void)
183 return (void *) GetCurrentThreadId ();
187 # define yield() gl_thread_yield ()
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)
203 static gl_tls_key_t mykeys[KEYS_COUNT];
206 worker_thread (void *arg)
208 unsigned int id = (unsigned int) (unsigned long) arg;
210 unsigned int values[KEYS_COUNT];
212 dbgprintf ("Worker %p started\n", gl_thread_self ());
214 /* Initialize the per-thread storage. */
215 for (i = 0; i < KEYS_COUNT; i++)
217 values[i] = (((unsigned int) rand() >> 3) % 1000000) * THREAD_COUNT + id;
218 /* Hopefully no arithmetic overflow. */
219 if ((values[i] % THREAD_COUNT) != id)
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)
229 dbgprintf ("Worker %p after initial verify\n", gl_thread_self ());
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++)
236 unsigned int *ptr = (unsigned int *) malloc (sizeof (unsigned int));
238 gl_tls_set (mykeys[i], ptr);
240 dbgprintf ("Worker %p after first tls_set\n", gl_thread_self ());
243 /* Shuffle around the pointers. */
244 for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
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;
251 void *vi = gl_tls_get (mykeys[i]);
252 void *vj = gl_tls_get (mykeys[j]);
254 gl_tls_set (mykeys[i], vj);
255 gl_tls_set (mykeys[j], vi);
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)
265 dbgprintf ("Worker %p after final verify\n", gl_thread_self ());
268 dbgprintf ("Worker %p dying.\n", gl_thread_self ());
277 for (pass = 0; pass < 2; pass++)
279 gl_thread_t threads[THREAD_COUNT];
282 for (i = 0; i < KEYS_COUNT; i++)
283 gl_tls_key_init (mykeys[i], free);
285 for (i = KEYS_COUNT - 1; i >= 0; i--)
286 gl_tls_key_init (mykeys[i], free);
288 /* Spawn the threads. */
289 for (i = 0; i < THREAD_COUNT; i++)
290 threads[i] = gl_thread_create (worker_thread, NULL);
292 /* Wait for the threads to terminate. */
293 for (i = 0; i < THREAD_COUNT; i++)
294 gl_thread_join (threads[i]);
296 for (i = 0; i < KEYS_COUNT; i++)
297 gl_tls_key_destroy (mykeys[i]);
309 printf ("Starting test_tls ..."); fflush (stdout);
311 printf (" OK\n"); fflush (stdout);
318 /* No multithreading available. */