attribute: Clarify which file to include.
[gnulib.git] / tests / test-tss.c
blob1bb51e8a0b74c74581a50f67cd1e2e4aa12e9ce1
1 /* Test of thread-local storage in multithreaded situations.
2 Copyright (C) 2005, 2008-2020 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. */
19 #include <config.h>
21 /* Whether to help the scheduler through explicit thrd_yield().
22 Uncomment this to see if the operating system has a fair scheduler. */
23 #define EXPLICIT_YIELD 1
25 /* Whether to print debugging messages. */
26 #define ENABLE_DEBUGGING 0
28 #include <threads.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #if HAVE_DECL_ALARM
35 # include <signal.h>
36 # include <unistd.h>
37 #endif
39 #include "macros.h"
41 #if ENABLE_DEBUGGING
42 # define dbgprintf printf
43 #else
44 # define dbgprintf if (0) printf
45 #endif
47 #if EXPLICIT_YIELD
48 # define yield() thrd_yield ()
49 #else
50 # define yield()
51 #endif
53 /* Returns a reference to the current thread as a pointer, for debugging. */
54 #if defined __MVS__
55 /* On IBM z/OS, pthread_t is a struct with an 8-byte '__' field.
56 The first three bytes of this field appear to uniquely identify a
57 pthread_t, though not necessarily representing a pointer. */
58 # define thrd_current_pointer() (*((void **) thrd_current ().__))
59 #elif defined __sun
60 /* On Solaris, thrd_t is merely an 'unsigned int'. */
61 # define thrd_current_pointer() ((void *) (uintptr_t) thrd_current ())
62 #else
63 # define thrd_current_pointer() ((void *) thrd_current ())
64 #endif
66 static void
67 perhaps_yield (void)
69 /* Call yield () only with a certain probability, otherwise the
70 sequence of thread activations may be too predictable. */
71 if ((((unsigned int) rand () >> 3) % 4) == 0)
72 yield ();
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
84 #define KEYS_COUNT 4
86 static tss_t mykeys[KEYS_COUNT];
88 static int
89 worker_thread (void *arg)
91 unsigned int id = (unsigned int) (uintptr_t) arg;
92 int i, j, repeat;
93 unsigned int values[KEYS_COUNT];
95 dbgprintf ("Worker %p started\n", thrd_current_pointer ());
97 /* Initialize the per-thread storage. */
98 for (i = 0; i < KEYS_COUNT; i++)
100 values[i] = (((unsigned int) rand () >> 3) % 1000000) * THREAD_COUNT + id;
101 /* Hopefully no arithmetic overflow. */
102 if ((values[i] % THREAD_COUNT) != id)
103 abort ();
105 perhaps_yield ();
107 /* Verify that the initial value is NULL. */
108 dbgprintf ("Worker %p before initial verify\n", thrd_current_pointer ());
109 for (i = 0; i < KEYS_COUNT; i++)
110 if (tss_get (mykeys[i]) != NULL)
111 abort ();
112 dbgprintf ("Worker %p after initial verify\n", thrd_current_pointer ());
113 perhaps_yield ();
115 /* Initialize the per-thread storage. */
116 dbgprintf ("Worker %p before first tss_set\n", thrd_current_pointer ());
117 for (i = 0; i < KEYS_COUNT; i++)
119 unsigned int *ptr = (unsigned int *) malloc (sizeof (unsigned int));
120 *ptr = values[i];
121 ASSERT (tss_set (mykeys[i], ptr) == thrd_success);
123 dbgprintf ("Worker %p after first tss_set\n", thrd_current_pointer ());
124 perhaps_yield ();
126 /* Shuffle around the pointers. */
127 for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
129 dbgprintf ("Worker %p doing value swapping\n", thrd_current_pointer ());
130 i = ((unsigned int) rand () >> 3) % KEYS_COUNT;
131 j = ((unsigned int) rand () >> 3) % KEYS_COUNT;
132 if (i != j)
134 void *vi = tss_get (mykeys[i]);
135 void *vj = tss_get (mykeys[j]);
137 ASSERT (tss_set (mykeys[i], vj) == thrd_success);
138 ASSERT (tss_set (mykeys[j], vi) == thrd_success);
140 perhaps_yield ();
143 /* Verify that all the values are from this thread. */
144 dbgprintf ("Worker %p before final verify\n", thrd_current_pointer ());
145 for (i = 0; i < KEYS_COUNT; i++)
146 if ((*(unsigned int *) tss_get (mykeys[i]) % THREAD_COUNT) != id)
147 abort ();
148 dbgprintf ("Worker %p after final verify\n", thrd_current_pointer ());
149 perhaps_yield ();
151 dbgprintf ("Worker %p dying.\n", thrd_current_pointer ());
152 return 0;
155 static void
156 test_tss (void)
158 int pass, i;
160 for (pass = 0; pass < 2; pass++)
162 thrd_t threads[THREAD_COUNT];
164 if (pass == 0)
165 for (i = 0; i < KEYS_COUNT; i++)
166 ASSERT (tss_create (&mykeys[i], free) == thrd_success);
167 else
168 for (i = KEYS_COUNT - 1; i >= 0; i--)
169 ASSERT (tss_create (&mykeys[i], free) == thrd_success);
171 /* Spawn the threads. */
172 for (i = 0; i < THREAD_COUNT; i++)
173 ASSERT (thrd_create (&threads[i], worker_thread, (void *) (uintptr_t) i)
174 == thrd_success);
176 /* Wait for the threads to terminate. */
177 for (i = 0; i < THREAD_COUNT; i++)
178 ASSERT (thrd_join (threads[i], NULL) == thrd_success);
180 for (i = 0; i < KEYS_COUNT; i++)
181 tss_delete (mykeys[i]);
185 #undef KEYS_COUNT
186 #undef REPEAT_COUNT
187 #undef THREAD_COUNT
190 /* --------------- Test thread-local storage with destructors --------------- */
192 /* Number of simultaneous threads. */
193 #define THREAD_COUNT 10
195 /* Number of keys to allocate in each thread. */
196 #define KEYS_COUNT 10
198 static mtx_t sumlock;
199 static uintptr_t sum;
201 static void
202 inc_sum (uintptr_t value)
204 ASSERT (mtx_lock (&sumlock) == thrd_success);
205 sum += value;
206 ASSERT (mtx_unlock (&sumlock) == thrd_success);
209 static void
210 destructor0 (void *value)
212 if ((((uintptr_t) value - 1) % 10) != 0)
213 abort ();
214 inc_sum ((uintptr_t) value);
217 static void
218 destructor1 (void *value)
220 if ((((uintptr_t) value - 1) % 10) != 1)
221 abort ();
222 inc_sum ((uintptr_t) value);
225 static void
226 destructor2 (void *value)
228 if ((((uintptr_t) value - 1) % 10) != 2)
229 abort ();
230 inc_sum ((uintptr_t) value);
233 static void
234 destructor3 (void *value)
236 if ((((uintptr_t) value - 1) % 10) != 3)
237 abort ();
238 inc_sum ((uintptr_t) value);
241 static void
242 destructor4 (void *value)
244 if ((((uintptr_t) value - 1) % 10) != 4)
245 abort ();
246 inc_sum ((uintptr_t) value);
249 static void
250 destructor5 (void *value)
252 if ((((uintptr_t) value - 1) % 10) != 5)
253 abort ();
254 inc_sum ((uintptr_t) value);
257 static void
258 destructor6 (void *value)
260 if ((((uintptr_t) value - 1) % 10) != 6)
261 abort ();
262 inc_sum ((uintptr_t) value);
265 static void
266 destructor7 (void *value)
268 if ((((uintptr_t) value - 1) % 10) != 7)
269 abort ();
270 inc_sum ((uintptr_t) value);
273 static void
274 destructor8 (void *value)
276 if ((((uintptr_t) value - 1) % 10) != 8)
277 abort ();
278 inc_sum ((uintptr_t) value);
281 static void
282 destructor9 (void *value)
284 if ((((uintptr_t) value - 1) % 10) != 9)
285 abort ();
286 inc_sum ((uintptr_t) value);
289 static void (*destructor_table[10]) (void *) =
291 destructor0,
292 destructor1,
293 destructor2,
294 destructor3,
295 destructor4,
296 destructor5,
297 destructor6,
298 destructor7,
299 destructor8,
300 destructor9
303 static tss_t dtorcheck_keys[THREAD_COUNT][KEYS_COUNT];
305 /* Worker thread that uses destructors that verify that the destructor belongs
306 to the right thread. */
307 static int
308 dtorcheck1_thread (void *arg)
310 unsigned int id = (unsigned int) (uintptr_t) arg;
311 tss_t *keys = dtorcheck_keys[id]; /* an array of KEYS_COUNT keys */
312 int i;
314 for (i = 0; i < KEYS_COUNT; i++)
315 ASSERT (tss_create (&keys[i], destructor_table[i]) == thrd_success);
317 for (i = 0; i < KEYS_COUNT; i++)
318 ASSERT (tss_set (keys[i], (void *) (uintptr_t) (10 * id + i + 1))
319 == thrd_success);
321 return 0;
324 static void
325 test_tss_dtorcheck1 (void)
327 thrd_t threads[THREAD_COUNT];
328 unsigned int id;
329 int i;
330 uintptr_t expected_sum;
332 sum = 0;
334 /* Spawn the threads. */
335 for (id = 0; id < THREAD_COUNT; id++)
336 ASSERT (thrd_create (&threads[id], dtorcheck1_thread, (void *) (uintptr_t) id)
337 == thrd_success);
339 /* Wait for the threads to terminate. */
340 for (id = 0; id < THREAD_COUNT; id++)
341 ASSERT (thrd_join (threads[id], NULL) == thrd_success);
343 /* Clean up the keys. */
344 for (id = 0; id < THREAD_COUNT; id++)
345 for (i = 0; i < KEYS_COUNT; i++)
346 tss_delete (dtorcheck_keys[id][i]);
348 /* Check that the destructor was invoked for each key. */
349 expected_sum = 10 * KEYS_COUNT * (THREAD_COUNT * (THREAD_COUNT - 1) / 2)
350 + THREAD_COUNT * (KEYS_COUNT * (KEYS_COUNT - 1) / 2)
351 + THREAD_COUNT * KEYS_COUNT;
352 if (sum != expected_sum)
353 abort ();
356 /* Worker thread that uses destructors that verify that the destructor belongs
357 to the right key allocated within the thread. */
358 static int
359 dtorcheck2_thread (void *arg)
361 unsigned int id = (unsigned int) (uintptr_t) arg;
362 tss_t *keys = dtorcheck_keys[id]; /* an array of KEYS_COUNT keys */
363 int i;
365 for (i = 0; i < KEYS_COUNT; i++)
366 ASSERT (tss_create (&keys[i], destructor_table[id]) == thrd_success);
368 for (i = 0; i < KEYS_COUNT; i++)
369 ASSERT (tss_set (keys[i], (void *) (uintptr_t) (10 * i + id + 1))
370 == thrd_success);
372 return 0;
375 static void
376 test_tss_dtorcheck2 (void)
378 thrd_t threads[THREAD_COUNT];
379 unsigned int id;
380 int i;
381 uintptr_t expected_sum;
383 sum = 0;
385 /* Spawn the threads. */
386 for (id = 0; id < THREAD_COUNT; id++)
387 ASSERT (thrd_create (&threads[id], dtorcheck2_thread, (void *) (uintptr_t) id)
388 == thrd_success);
390 /* Wait for the threads to terminate. */
391 for (id = 0; id < THREAD_COUNT; id++)
392 ASSERT (thrd_join (threads[id], NULL) == thrd_success);
394 /* Clean up the keys. */
395 for (id = 0; id < THREAD_COUNT; id++)
396 for (i = 0; i < KEYS_COUNT; i++)
397 tss_delete (dtorcheck_keys[id][i]);
399 /* Check that the destructor was invoked for each key. */
400 expected_sum = 10 * THREAD_COUNT * (KEYS_COUNT * (KEYS_COUNT - 1) / 2)
401 + KEYS_COUNT * (THREAD_COUNT * (THREAD_COUNT - 1) / 2)
402 + THREAD_COUNT * KEYS_COUNT;
403 if (sum != expected_sum)
404 abort ();
407 #undef KEYS_COUNT
408 #undef THREAD_COUNT
411 /* --- Test thread-local storage with with races between init and destroy --- */
413 /* Number of simultaneous threads. */
414 #define THREAD_COUNT 10
416 /* Number of keys to allocate in each thread. */
417 #define KEYS_COUNT 10
419 /* Number of times to destroy and reallocate a key in each thread. */
420 #define REPEAT_COUNT 100000
422 static tss_t racecheck_keys[THREAD_COUNT][KEYS_COUNT];
424 /* Worker thread that does many destructions and reallocations of keys, and also
425 uses destructors that verify that the destructor belongs to the right key. */
426 static int
427 racecheck_thread (void *arg)
429 unsigned int id = (unsigned int) (uintptr_t) arg;
430 tss_t *keys = racecheck_keys[id]; /* an array of KEYS_COUNT keys */
431 int repeat;
432 int i;
434 dbgprintf ("Worker %p started\n", thrd_current_pointer ());
436 for (i = 0; i < KEYS_COUNT; i++)
438 ASSERT (tss_create (&keys[i], destructor_table[i]) == thrd_success);
439 ASSERT (tss_set (keys[i], (void *) (uintptr_t) (10 * id + i + 1))
440 == thrd_success);
443 for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
445 i = ((unsigned int) rand () >> 3) % KEYS_COUNT;
446 dbgprintf ("Worker %p reallocating key %d\n", thrd_current_pointer (), i);
447 tss_delete (keys[i]);
448 ASSERT (tss_create (&keys[i], destructor_table[i]) == thrd_success);
449 ASSERT (tss_set (keys[i], (void *) (uintptr_t) (10 * id + i + 1))
450 == thrd_success);
453 dbgprintf ("Worker %p dying.\n", thrd_current_pointer ());
454 return 0;
457 static void
458 test_tss_racecheck (void)
460 thrd_t threads[THREAD_COUNT];
461 unsigned int id;
462 int i;
463 uintptr_t expected_sum;
465 sum = 0;
467 /* Spawn the threads. */
468 for (id = 0; id < THREAD_COUNT; id++)
469 ASSERT (thrd_create (&threads[id], racecheck_thread, (void *) (uintptr_t) id)
470 == thrd_success);
472 /* Wait for the threads to terminate. */
473 for (id = 0; id < THREAD_COUNT; id++)
474 ASSERT (thrd_join (threads[id], NULL) == thrd_success);
476 /* Clean up the keys. */
477 for (id = 0; id < THREAD_COUNT; id++)
478 for (i = 0; i < KEYS_COUNT; i++)
479 tss_delete (racecheck_keys[id][i]);
481 /* Check that the destructor was invoked for each key. */
482 expected_sum = 10 * KEYS_COUNT * (THREAD_COUNT * (THREAD_COUNT - 1) / 2)
483 + THREAD_COUNT * (KEYS_COUNT * (KEYS_COUNT - 1) / 2)
484 + THREAD_COUNT * KEYS_COUNT;
485 if (sum != expected_sum)
486 abort ();
489 #undef REPEAT_COUNT
490 #undef KEYS_COUNT
491 #undef THREAD_COUNT
494 /* -------------------------------------------------------------------------- */
497 main ()
499 #if HAVE_DECL_ALARM
500 /* Declare failure if test takes too long, by using default abort
501 caused by SIGALRM. */
502 int alarm_value = 600;
503 signal (SIGALRM, SIG_DFL);
504 alarm (alarm_value);
505 #endif
507 ASSERT (mtx_init (&sumlock, mtx_plain) == thrd_success);
509 printf ("Starting test_tss ..."); fflush (stdout);
510 test_tss ();
511 printf (" OK\n"); fflush (stdout);
513 printf ("Starting test_tss_dtorcheck1 ..."); fflush (stdout);
514 test_tss_dtorcheck1 ();
515 printf (" OK\n"); fflush (stdout);
517 printf ("Starting test_tss_dtorcheck2 ..."); fflush (stdout);
518 test_tss_dtorcheck2 ();
519 printf (" OK\n"); fflush (stdout);
521 printf ("Starting test_tss_racecheck ..."); fflush (stdout);
522 test_tss_racecheck ();
523 printf (" OK\n"); fflush (stdout);
525 return 0;