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. */
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
42 # define dbgprintf printf
44 # define dbgprintf if (0) printf
48 # define yield() thrd_yield ()
53 /* Returns a reference to the current thread as a pointer, for debugging. */
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 ().__))
60 /* On Solaris, thrd_t is merely an 'unsigned int'. */
61 # define thrd_current_pointer() ((void *) (uintptr_t) thrd_current ())
63 # define thrd_current_pointer() ((void *) thrd_current ())
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)
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 tss_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", 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
)
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
)
112 dbgprintf ("Worker %p after initial verify\n", thrd_current_pointer ());
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));
121 ASSERT (tss_set (mykeys
[i
], ptr
) == thrd_success
);
123 dbgprintf ("Worker %p after first tss_set\n", thrd_current_pointer ());
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
;
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
);
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
)
148 dbgprintf ("Worker %p after final verify\n", thrd_current_pointer ());
151 dbgprintf ("Worker %p dying.\n", thrd_current_pointer ());
160 for (pass
= 0; pass
< 2; pass
++)
162 thrd_t threads
[THREAD_COUNT
];
165 for (i
= 0; i
< KEYS_COUNT
; i
++)
166 ASSERT (tss_create (&mykeys
[i
], free
) == thrd_success
);
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
)
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
]);
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
;
202 inc_sum (uintptr_t value
)
204 ASSERT (mtx_lock (&sumlock
) == thrd_success
);
206 ASSERT (mtx_unlock (&sumlock
) == thrd_success
);
210 destructor0 (void *value
)
212 if ((((uintptr_t) value
- 1) % 10) != 0)
214 inc_sum ((uintptr_t) value
);
218 destructor1 (void *value
)
220 if ((((uintptr_t) value
- 1) % 10) != 1)
222 inc_sum ((uintptr_t) value
);
226 destructor2 (void *value
)
228 if ((((uintptr_t) value
- 1) % 10) != 2)
230 inc_sum ((uintptr_t) value
);
234 destructor3 (void *value
)
236 if ((((uintptr_t) value
- 1) % 10) != 3)
238 inc_sum ((uintptr_t) value
);
242 destructor4 (void *value
)
244 if ((((uintptr_t) value
- 1) % 10) != 4)
246 inc_sum ((uintptr_t) value
);
250 destructor5 (void *value
)
252 if ((((uintptr_t) value
- 1) % 10) != 5)
254 inc_sum ((uintptr_t) value
);
258 destructor6 (void *value
)
260 if ((((uintptr_t) value
- 1) % 10) != 6)
262 inc_sum ((uintptr_t) value
);
266 destructor7 (void *value
)
268 if ((((uintptr_t) value
- 1) % 10) != 7)
270 inc_sum ((uintptr_t) value
);
274 destructor8 (void *value
)
276 if ((((uintptr_t) value
- 1) % 10) != 8)
278 inc_sum ((uintptr_t) value
);
282 destructor9 (void *value
)
284 if ((((uintptr_t) value
- 1) % 10) != 9)
286 inc_sum ((uintptr_t) value
);
289 static void (*destructor_table
[10]) (void *) =
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. */
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 */
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))
325 test_tss_dtorcheck1 (void)
327 thrd_t threads
[THREAD_COUNT
];
330 uintptr_t expected_sum
;
334 /* Spawn the threads. */
335 for (id
= 0; id
< THREAD_COUNT
; id
++)
336 ASSERT (thrd_create (&threads
[id
], dtorcheck1_thread
, (void *) (uintptr_t) id
)
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
)
356 /* Worker thread that uses destructors that verify that the destructor belongs
357 to the right key allocated within the thread. */
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 */
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))
376 test_tss_dtorcheck2 (void)
378 thrd_t threads
[THREAD_COUNT
];
381 uintptr_t expected_sum
;
385 /* Spawn the threads. */
386 for (id
= 0; id
< THREAD_COUNT
; id
++)
387 ASSERT (thrd_create (&threads
[id
], dtorcheck2_thread
, (void *) (uintptr_t) id
)
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
)
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. */
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 */
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))
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))
453 dbgprintf ("Worker %p dying.\n", thrd_current_pointer ());
458 test_tss_racecheck (void)
460 thrd_t threads
[THREAD_COUNT
];
463 uintptr_t expected_sum
;
467 /* Spawn the threads. */
468 for (id
= 0; id
< THREAD_COUNT
; id
++)
469 ASSERT (thrd_create (&threads
[id
], racecheck_thread
, (void *) (uintptr_t) id
)
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
)
494 /* -------------------------------------------------------------------------- */
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
);
507 ASSERT (mtx_init (&sumlock
, mtx_plain
) == thrd_success
);
509 printf ("Starting test_tss ..."); fflush (stdout
);
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
);