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. */
22 #if USE_POSIX_THREADS || USE_SOLARIS_THREADS || USE_PTH_THREADS || USE_WIN32_THREADS
25 # define TEST_POSIX_THREADS 1
27 #if USE_SOLARIS_THREADS
28 # define TEST_SOLARIS_THREADS 1
31 # define TEST_PTH_THREADS 1
34 # define TEST_WIN32_THREADS 1
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
41 /* Whether to print debugging messages. */
42 #define ENABLE_DEBUGGING 0
44 /* Number of simultaneous threads. */
45 #define THREAD_COUNT 16
47 /* Number of operations performed in each thread. */
48 #define REPEAT_COUNT 50000
57 # define dbgprintf printf
59 # define dbgprintf if (0) printf
62 #if TEST_POSIX_THREADS
65 typedef pthread_t gl_thread_t;
66 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
69 if (pthread_create (&thread, NULL, func, arg) != 0)
73 static inline void gl_thread_join (gl_thread_t thread)
76 if (pthread_join (thread, &retval) != 0)
79 static inline void gl_thread_yield (void)
83 static inline void * gl_thread_self (void)
85 return (void *) pthread_self ();
90 typedef pth_t gl_thread_t;
91 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
93 pth_t thread = pth_spawn (NULL, func, arg);
98 static inline void gl_thread_join (gl_thread_t thread)
100 if (!pth_join (thread, NULL))
103 static inline void gl_thread_yield (void)
107 static inline void * gl_thread_self (void)
112 #if TEST_SOLARIS_THREADS
114 typedef thread_t gl_thread_t;
115 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
118 if (thr_create (NULL, 0, func, arg, 0, &thread) != 0)
122 static inline void gl_thread_join (gl_thread_t thread)
125 if (thr_join (thread, NULL, &retval) != 0)
128 static inline void gl_thread_yield (void)
132 static inline void * gl_thread_self (void)
134 return (void *) thr_self ();
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)
144 struct wrapper_args *warg = (struct wrapper_args *)varg;
145 void * (*func) (void *) = warg->func;
146 void *arg = warg->arg;
151 static inline gl_thread_t gl_thread_create (void * (*func) (void *), void *arg)
153 struct wrapper_args *warg =
154 (struct wrapper_args *) malloc (sizeof (struct wrapper_args));
162 CreateThread (NULL, 100000, wrapper_func, warg, 0, &thread_id);
168 static inline void gl_thread_join (gl_thread_t thread)
170 if (WaitForSingleObject (thread, INFINITE) == WAIT_FAILED)
172 if (!CloseHandle (thread))
175 static inline void gl_thread_yield (void)
179 static inline void * gl_thread_self (void)
181 return (void *) GetCurrentThreadId ();
185 # define yield() gl_thread_yield ()
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)
201 static gl_tls_key_t mykeys[KEYS_COUNT];
204 worker_thread (void *arg)
206 unsigned int id = (unsigned int) (unsigned long) arg;
208 unsigned int values[KEYS_COUNT];
210 dbgprintf ("Worker %p started\n", gl_thread_self ());
212 /* Initialize the per-thread storage. */
213 for (i = 0; i < KEYS_COUNT; i++)
215 values[i] = (((unsigned int) rand() >> 3) % 1000000) * THREAD_COUNT + id;
216 /* Hopefully no arithmetic overflow. */
217 if ((values[i] % THREAD_COUNT) != id)
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)
227 dbgprintf ("Worker %p after initial verify\n", gl_thread_self ());
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++)
234 unsigned int *ptr = (unsigned int *) malloc (sizeof (unsigned int));
236 gl_tls_set (mykeys[i], ptr);
238 dbgprintf ("Worker %p after first tls_set\n", gl_thread_self ());
241 /* Shuffle around the pointers. */
242 for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
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;
249 void *vi = gl_tls_get (mykeys[i]);
250 void *vj = gl_tls_get (mykeys[j]);
252 gl_tls_set (mykeys[i], vj);
253 gl_tls_set (mykeys[j], vi);
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)
263 dbgprintf ("Worker %p after final verify\n", gl_thread_self ());
266 dbgprintf ("Worker %p dying.\n", gl_thread_self ());
275 for (pass = 0; pass < 2; pass++)
277 gl_thread_t threads[THREAD_COUNT];
280 for (i = 0; i < KEYS_COUNT; i++)
281 gl_tls_key_init (mykeys[i], free);
283 for (i = KEYS_COUNT - 1; i >= 0; i--)
284 gl_tls_key_init (mykeys[i], free);
286 /* Spawn the threads. */
287 for (i = 0; i < THREAD_COUNT; i++)
288 threads[i] = gl_thread_create (worker_thread, NULL);
290 /* Wait for the threads to terminate. */
291 for (i = 0; i < THREAD_COUNT; i++)
292 gl_thread_join (threads[i]);
294 for (i = 0; i < KEYS_COUNT; i++)
295 gl_tls_key_destroy (mykeys[i]);
307 printf ("Starting test_tls ..."); fflush (stdout);
309 printf (" OK\n"); fflush (stdout);
316 /* No multithreading available. */