1 /* Test of thread-local storage in multithreaded situations.
2 Copyright (C) 2005, 2008-2024 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 3 of the License, or
7 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
21 #if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
23 #if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
24 # define TEST_ISOC_THREADS 1
27 # define TEST_POSIX_THREADS 1
29 #if USE_WINDOWS_THREADS
30 # define TEST_WINDOWS_THREADS 1
33 /* Whether to help the scheduler through explicit yield().
34 Uncomment this to see if the operating system has a fair scheduler. */
35 #define EXPLICIT_YIELD 1
37 /* Whether to print debugging messages. */
38 #define ENABLE_DEBUGGING 0
45 #include "glthread/tls.h"
46 #include "glthread/thread.h"
47 #include "glthread/lock.h"
48 #include "glthread/yield.h"
56 # define dbgprintf printf
58 # define dbgprintf if (0) printf
62 # define yield() gl_thread_yield ()
70 /* This helps making the sequence of thread activations less predictable. */
71 if ((((unsigned long) random () >> 3) % 4) == 0)
76 /* ----------------------- Test thread-local storage ----------------------- */
78 /* Number of simultaneous threads. */
79 #define THREAD_COUNT 16
81 /* Number of operations performed in each thread. */
82 #define REPEAT_COUNT 50000
86 static gl_tls_key_t mykeys
[KEYS_COUNT
];
89 worker_thread (void *arg
)
91 unsigned int id
= (unsigned int) (uintptr_t) arg
;
93 unsigned int values
[KEYS_COUNT
];
95 dbgprintf ("Worker %p started\n", gl_thread_self_pointer ());
97 /* Initialize the per-thread storage. */
98 for (i
= 0; i
< KEYS_COUNT
; i
++)
100 values
[i
] = (((unsigned long) random () >> 3) % 1000000) * THREAD_COUNT
+ id
;
101 /* Hopefully no arithmetic overflow. */
102 if ((values
[i
] % THREAD_COUNT
) != id
)
107 /* Verify that the initial value is NULL. */
108 dbgprintf ("Worker %p before initial verify\n", gl_thread_self_pointer ());
109 for (i
= 0; i
< KEYS_COUNT
; i
++)
110 if (gl_tls_get (mykeys
[i
]) != NULL
)
112 dbgprintf ("Worker %p after initial verify\n", gl_thread_self_pointer ());
115 /* Initialize the per-thread storage. */
116 dbgprintf ("Worker %p before first tls_set\n", gl_thread_self_pointer ());
117 for (i
= 0; i
< KEYS_COUNT
; i
++)
119 unsigned int *ptr
= (unsigned int *) malloc (sizeof (unsigned int));
121 gl_tls_set (mykeys
[i
], ptr
);
123 dbgprintf ("Worker %p after first tls_set\n", gl_thread_self_pointer ());
126 /* Shuffle around the pointers. */
127 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
129 dbgprintf ("Worker %p doing value swapping\n", gl_thread_self_pointer ());
130 i
= ((unsigned long) random () >> 3) % KEYS_COUNT
;
131 j
= ((unsigned long) random () >> 3) % KEYS_COUNT
;
134 void *vi
= gl_tls_get (mykeys
[i
]);
135 void *vj
= gl_tls_get (mykeys
[j
]);
137 gl_tls_set (mykeys
[i
], vj
);
138 gl_tls_set (mykeys
[j
], vi
);
143 /* Verify that all the values are from this thread. */
144 dbgprintf ("Worker %p before final verify\n", gl_thread_self_pointer ());
145 for (i
= 0; i
< KEYS_COUNT
; i
++)
146 if ((*(unsigned int *) gl_tls_get (mykeys
[i
]) % THREAD_COUNT
) != id
)
148 dbgprintf ("Worker %p after final verify\n", gl_thread_self_pointer ());
151 dbgprintf ("Worker %p dying.\n", gl_thread_self_pointer ());
160 for (pass
= 0; pass
< 2; pass
++)
162 gl_thread_t threads
[THREAD_COUNT
];
165 for (i
= 0; i
< KEYS_COUNT
; i
++)
166 gl_tls_key_init (mykeys
[i
], free
);
168 for (i
= KEYS_COUNT
- 1; i
>= 0; i
--)
169 gl_tls_key_init (mykeys
[i
], free
);
171 /* Spawn the threads. */
172 for (i
= 0; i
< THREAD_COUNT
; i
++)
173 threads
[i
] = gl_thread_create (worker_thread
, (void *) (uintptr_t) i
);
175 /* Wait for the threads to terminate. */
176 for (i
= 0; i
< THREAD_COUNT
; i
++)
177 gl_thread_join (threads
[i
], NULL
);
179 for (i
= 0; i
< KEYS_COUNT
; i
++)
180 gl_tls_key_destroy (mykeys
[i
]);
189 /* --------------- Test thread-local storage with destructors --------------- */
191 /* Number of simultaneous threads. */
192 #define THREAD_COUNT 10
194 /* Number of keys to allocate in each thread. */
195 #define KEYS_COUNT 10
197 gl_lock_define_initialized(static, sumlock
)
198 static uintptr_t sum
;
201 inc_sum (uintptr_t value
)
203 gl_lock_lock (sumlock
);
205 gl_lock_unlock (sumlock
);
209 destructor0 (void *value
)
211 if ((((uintptr_t) value
- 1) % 10) != 0)
213 inc_sum ((uintptr_t) value
);
217 destructor1 (void *value
)
219 if ((((uintptr_t) value
- 1) % 10) != 1)
221 inc_sum ((uintptr_t) value
);
225 destructor2 (void *value
)
227 if ((((uintptr_t) value
- 1) % 10) != 2)
229 inc_sum ((uintptr_t) value
);
233 destructor3 (void *value
)
235 if ((((uintptr_t) value
- 1) % 10) != 3)
237 inc_sum ((uintptr_t) value
);
241 destructor4 (void *value
)
243 if ((((uintptr_t) value
- 1) % 10) != 4)
245 inc_sum ((uintptr_t) value
);
249 destructor5 (void *value
)
251 if ((((uintptr_t) value
- 1) % 10) != 5)
253 inc_sum ((uintptr_t) value
);
257 destructor6 (void *value
)
259 if ((((uintptr_t) value
- 1) % 10) != 6)
261 inc_sum ((uintptr_t) value
);
265 destructor7 (void *value
)
267 if ((((uintptr_t) value
- 1) % 10) != 7)
269 inc_sum ((uintptr_t) value
);
273 destructor8 (void *value
)
275 if ((((uintptr_t) value
- 1) % 10) != 8)
277 inc_sum ((uintptr_t) value
);
281 destructor9 (void *value
)
283 if ((((uintptr_t) value
- 1) % 10) != 9)
285 inc_sum ((uintptr_t) value
);
288 static void (*destructor_table
[10]) (void *) =
302 static gl_tls_key_t dtorcheck_keys
[THREAD_COUNT
][KEYS_COUNT
];
304 /* Worker thread that uses destructors that verify that the destructor belongs
305 to the right thread. */
307 dtorcheck1_thread (void *arg
)
309 unsigned int id
= (unsigned int) (uintptr_t) arg
;
310 gl_tls_key_t
*keys
= dtorcheck_keys
[id
]; /* an array of KEYS_COUNT keys */
313 for (i
= 0; i
< KEYS_COUNT
; i
++)
314 gl_tls_key_init (keys
[i
], destructor_table
[i
]);
316 for (i
= 0; i
< KEYS_COUNT
; i
++)
317 gl_tls_set (keys
[i
], (void *) (uintptr_t) (10 * id
+ i
+ 1));
323 test_tls_dtorcheck1 (void)
325 gl_thread_t threads
[THREAD_COUNT
];
328 uintptr_t expected_sum
;
332 /* Spawn the threads. */
333 for (id
= 0; id
< THREAD_COUNT
; id
++)
334 threads
[id
] = gl_thread_create (dtorcheck1_thread
, (void *) (uintptr_t) id
);
336 /* Wait for the threads to terminate. */
337 for (id
= 0; id
< THREAD_COUNT
; id
++)
338 gl_thread_join (threads
[id
], NULL
);
340 /* Clean up the keys. */
341 for (id
= 0; id
< THREAD_COUNT
; id
++)
342 for (i
= 0; i
< KEYS_COUNT
; i
++)
343 gl_tls_key_destroy (dtorcheck_keys
[id
][i
]);
345 /* Check that the destructor was invoked for each key. */
346 expected_sum
= 10 * KEYS_COUNT
* (THREAD_COUNT
* (THREAD_COUNT
- 1) / 2)
347 + THREAD_COUNT
* (KEYS_COUNT
* (KEYS_COUNT
- 1) / 2)
348 + THREAD_COUNT
* KEYS_COUNT
;
349 if (sum
!= expected_sum
)
353 /* Worker thread that uses destructors that verify that the destructor belongs
354 to the right key allocated within the thread. */
356 dtorcheck2_thread (void *arg
)
358 unsigned int id
= (unsigned int) (uintptr_t) arg
;
359 gl_tls_key_t
*keys
= dtorcheck_keys
[id
]; /* an array of KEYS_COUNT keys */
362 for (i
= 0; i
< KEYS_COUNT
; i
++)
363 gl_tls_key_init (keys
[i
], destructor_table
[id
]);
365 for (i
= 0; i
< KEYS_COUNT
; i
++)
366 gl_tls_set (keys
[i
], (void *) (uintptr_t) (10 * i
+ id
+ 1));
372 test_tls_dtorcheck2 (void)
374 gl_thread_t threads
[THREAD_COUNT
];
377 uintptr_t expected_sum
;
381 /* Spawn the threads. */
382 for (id
= 0; id
< THREAD_COUNT
; id
++)
383 threads
[id
] = gl_thread_create (dtorcheck2_thread
, (void *) (uintptr_t) id
);
385 /* Wait for the threads to terminate. */
386 for (id
= 0; id
< THREAD_COUNT
; id
++)
387 gl_thread_join (threads
[id
], NULL
);
389 /* Clean up the keys. */
390 for (id
= 0; id
< THREAD_COUNT
; id
++)
391 for (i
= 0; i
< KEYS_COUNT
; i
++)
392 gl_tls_key_destroy (dtorcheck_keys
[id
][i
]);
394 /* Check that the destructor was invoked for each key. */
395 expected_sum
= 10 * THREAD_COUNT
* (KEYS_COUNT
* (KEYS_COUNT
- 1) / 2)
396 + KEYS_COUNT
* (THREAD_COUNT
* (THREAD_COUNT
- 1) / 2)
397 + THREAD_COUNT
* KEYS_COUNT
;
398 if (sum
!= expected_sum
)
406 /* --- Test thread-local storage with races between init and destroy --- */
408 /* Number of simultaneous threads. */
409 #define THREAD_COUNT 10
411 /* Number of keys to allocate in each thread. */
412 #define KEYS_COUNT 10
414 /* Number of times to destroy and reallocate a key in each thread. */
415 #define REPEAT_COUNT 100000
417 static gl_tls_key_t racecheck_keys
[THREAD_COUNT
][KEYS_COUNT
];
419 /* Worker thread that does many destructions and reallocations of keys, and also
420 uses destructors that verify that the destructor belongs to the right key. */
422 racecheck_thread (void *arg
)
424 unsigned int id
= (unsigned int) (uintptr_t) arg
;
425 gl_tls_key_t
*keys
= racecheck_keys
[id
]; /* an array of KEYS_COUNT keys */
429 dbgprintf ("Worker %p started\n", gl_thread_self_pointer ());
431 for (i
= 0; i
< KEYS_COUNT
; i
++)
433 gl_tls_key_init (keys
[i
], destructor_table
[i
]);
434 gl_tls_set (keys
[i
], (void *) (uintptr_t) (10 * id
+ i
+ 1));
437 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
439 i
= ((unsigned long) random () >> 3) % KEYS_COUNT
;
440 dbgprintf ("Worker %p reallocating key %d\n", gl_thread_self_pointer (), i
);
441 gl_tls_key_destroy (keys
[i
]);
442 gl_tls_key_init (keys
[i
], destructor_table
[i
]);
443 gl_tls_set (keys
[i
], (void *) (uintptr_t) (10 * id
+ i
+ 1));
446 dbgprintf ("Worker %p dying.\n", gl_thread_self_pointer ());
451 test_tls_racecheck (void)
453 gl_thread_t threads
[THREAD_COUNT
];
456 uintptr_t expected_sum
;
460 /* Spawn the threads. */
461 for (id
= 0; id
< THREAD_COUNT
; id
++)
462 threads
[id
] = gl_thread_create (racecheck_thread
, (void *) (uintptr_t) id
);
464 /* Wait for the threads to terminate. */
465 for (id
= 0; id
< THREAD_COUNT
; id
++)
466 gl_thread_join (threads
[id
], NULL
);
468 /* Clean up the keys. */
469 for (id
= 0; id
< THREAD_COUNT
; id
++)
470 for (i
= 0; i
< KEYS_COUNT
; i
++)
471 gl_tls_key_destroy (racecheck_keys
[id
][i
]);
473 /* Check that the destructor was invoked for each key. */
474 expected_sum
= 10 * KEYS_COUNT
* (THREAD_COUNT
* (THREAD_COUNT
- 1) / 2)
475 + THREAD_COUNT
* (KEYS_COUNT
* (KEYS_COUNT
- 1) / 2)
476 + THREAD_COUNT
* KEYS_COUNT
;
477 if (sum
!= expected_sum
)
486 /* -------------------------------------------------------------------------- */
492 /* Declare failure if test takes too long, by using default abort
493 caused by SIGALRM. */
494 int alarm_value
= 600;
495 signal (SIGALRM
, SIG_DFL
);
499 printf ("Starting test_tls ..."); fflush (stdout
);
501 printf (" OK\n"); fflush (stdout
);
503 printf ("Starting test_tls_dtorcheck1 ..."); fflush (stdout
);
504 test_tls_dtorcheck1 ();
505 printf (" OK\n"); fflush (stdout
);
507 printf ("Starting test_tls_dtorcheck2 ..."); fflush (stdout
);
508 test_tls_dtorcheck2 ();
509 printf (" OK\n"); fflush (stdout
);
511 /* This test hangs with the mingw-w64 winpthreads. */
512 #if (defined _WIN32 && ! defined __CYGWIN__) && TEST_POSIX_THREADS
513 fputs ("Skipping test: it is known to hang with the mingw-w64 winpthreads.\n",
517 printf ("Starting test_tls_racecheck ..."); fflush (stdout
);
518 test_tls_racecheck ();
519 printf (" OK\n"); fflush (stdout
);
527 /* No multithreading available. */
534 fputs ("Skipping test: multithreading not enabled\n", stderr
);