1 /* Test of thread-specific 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 /* Whether to help the scheduler through explicit sched_yield().
24 Uncomment this to see if the operating system has a fair scheduler. */
25 #define EXPLICIT_YIELD 1
27 /* Whether to print debugging messages. */
28 #define ENABLE_DEBUGGING 0
48 # define dbgprintf printf
50 # define dbgprintf if (0) printf
54 # define yield() sched_yield ()
59 /* Returns a reference to the current thread as a pointer, for debugging. */
61 /* On IBM z/OS, pthread_t is a struct with an 8-byte '__' field.
62 The first three bytes of this field appear to uniquely identify a
63 pthread_t, though not necessarily representing a pointer. */
64 # define pthread_self_pointer() (*((void **) pthread_self ().__))
66 # define pthread_self_pointer() ((void *) (uintptr_t) pthread_self ())
72 /* Call yield () only with a certain probability, otherwise the
73 sequence of thread activations may be too predictable. */
74 if ((((unsigned long) random () >> 3) % 4) == 0)
79 /* ----------------------- Test thread-local storage ----------------------- */
81 /* Number of simultaneous threads. */
82 #define THREAD_COUNT 16
84 /* Number of operations performed in each thread. */
85 #define REPEAT_COUNT 50000
89 static pthread_key_t mykeys
[KEYS_COUNT
];
92 worker_thread (void *arg
)
94 unsigned int id
= (unsigned int) (uintptr_t) arg
;
96 unsigned int values
[KEYS_COUNT
];
98 dbgprintf ("Worker %p started\n", pthread_self_pointer ());
100 /* Initialize the per-thread storage. */
101 for (i
= 0; i
< KEYS_COUNT
; i
++)
103 values
[i
] = (((unsigned long) random () >> 3) % 1000000) * THREAD_COUNT
+ id
;
104 /* Hopefully no arithmetic overflow. */
105 if ((values
[i
] % THREAD_COUNT
) != id
)
110 /* Verify that the initial value is NULL. */
111 dbgprintf ("Worker %p before initial verify\n", pthread_self_pointer ());
112 for (i
= 0; i
< KEYS_COUNT
; i
++)
113 if (pthread_getspecific (mykeys
[i
]) != NULL
)
115 dbgprintf ("Worker %p after initial verify\n", pthread_self_pointer ());
118 /* Initialize the per-thread storage. */
119 dbgprintf ("Worker %p before first pthread_setspecific\n",
120 pthread_self_pointer ());
121 for (i
= 0; i
< KEYS_COUNT
; i
++)
123 unsigned int *ptr
= (unsigned int *) malloc (sizeof (unsigned int));
125 ASSERT (pthread_setspecific (mykeys
[i
], ptr
) == 0);
127 dbgprintf ("Worker %p after first pthread_setspecific\n",
128 pthread_self_pointer ());
131 /* Shuffle around the pointers. */
132 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
134 dbgprintf ("Worker %p doing value swapping\n", pthread_self_pointer ());
135 i
= ((unsigned long) random () >> 3) % KEYS_COUNT
;
136 j
= ((unsigned long) random () >> 3) % KEYS_COUNT
;
139 void *vi
= pthread_getspecific (mykeys
[i
]);
140 void *vj
= pthread_getspecific (mykeys
[j
]);
142 ASSERT (pthread_setspecific (mykeys
[i
], vj
) == 0);
143 ASSERT (pthread_setspecific (mykeys
[j
], vi
) == 0);
148 /* Verify that all the values are from this thread. */
149 dbgprintf ("Worker %p before final verify\n", pthread_self_pointer ());
150 for (i
= 0; i
< KEYS_COUNT
; i
++)
151 if ((*(unsigned int *) pthread_getspecific (mykeys
[i
]) % THREAD_COUNT
)
154 dbgprintf ("Worker %p after final verify\n", pthread_self_pointer ());
157 dbgprintf ("Worker %p dying.\n", pthread_self_pointer ());
166 for (pass
= 0; pass
< 2; pass
++)
168 pthread_t threads
[THREAD_COUNT
];
171 for (i
= 0; i
< KEYS_COUNT
; i
++)
172 ASSERT (pthread_key_create (&mykeys
[i
], free
) == 0);
174 for (i
= KEYS_COUNT
- 1; i
>= 0; i
--)
175 ASSERT (pthread_key_create (&mykeys
[i
], free
) == 0);
177 /* Spawn the threads. */
178 for (i
= 0; i
< THREAD_COUNT
; i
++)
179 ASSERT (pthread_create (&threads
[i
], NULL
,
180 worker_thread
, (void *) (uintptr_t) i
)
183 /* Wait for the threads to terminate. */
184 for (i
= 0; i
< THREAD_COUNT
; i
++)
185 ASSERT (pthread_join (threads
[i
], NULL
) == 0);
187 for (i
= 0; i
< KEYS_COUNT
; i
++)
188 ASSERT (pthread_key_delete (mykeys
[i
]) == 0);
197 /* --------------- Test thread-local storage with destructors --------------- */
199 /* Number of simultaneous threads. */
200 #if defined __ANDROID__
201 # define THREAD_COUNT 5 /* to avoid a pthread_key_create failure */
203 # define THREAD_COUNT 10
206 /* Number of keys to allocate in each thread. */
207 #define KEYS_COUNT 10
209 static pthread_mutex_t sumlock
;
210 static uintptr_t sum
;
213 inc_sum (uintptr_t value
)
215 ASSERT (pthread_mutex_lock (&sumlock
) == 0);
217 ASSERT (pthread_mutex_unlock (&sumlock
) == 0);
221 destructor0 (void *value
)
223 if ((((uintptr_t) value
- 1) % 10) != 0)
225 inc_sum ((uintptr_t) value
);
229 destructor1 (void *value
)
231 if ((((uintptr_t) value
- 1) % 10) != 1)
233 inc_sum ((uintptr_t) value
);
237 destructor2 (void *value
)
239 if ((((uintptr_t) value
- 1) % 10) != 2)
241 inc_sum ((uintptr_t) value
);
245 destructor3 (void *value
)
247 if ((((uintptr_t) value
- 1) % 10) != 3)
249 inc_sum ((uintptr_t) value
);
253 destructor4 (void *value
)
255 if ((((uintptr_t) value
- 1) % 10) != 4)
257 inc_sum ((uintptr_t) value
);
261 destructor5 (void *value
)
263 if ((((uintptr_t) value
- 1) % 10) != 5)
265 inc_sum ((uintptr_t) value
);
269 destructor6 (void *value
)
271 if ((((uintptr_t) value
- 1) % 10) != 6)
273 inc_sum ((uintptr_t) value
);
277 destructor7 (void *value
)
279 if ((((uintptr_t) value
- 1) % 10) != 7)
281 inc_sum ((uintptr_t) value
);
285 destructor8 (void *value
)
287 if ((((uintptr_t) value
- 1) % 10) != 8)
289 inc_sum ((uintptr_t) value
);
293 destructor9 (void *value
)
295 if ((((uintptr_t) value
- 1) % 10) != 9)
297 inc_sum ((uintptr_t) value
);
300 static void (*destructor_table
[10]) (void *) =
314 static pthread_key_t dtorcheck_keys
[THREAD_COUNT
][KEYS_COUNT
];
316 /* Worker thread that uses destructors that verify that the destructor belongs
317 to the right thread. */
319 dtorcheck1_thread (void *arg
)
321 unsigned int id
= (unsigned int) (uintptr_t) arg
;
322 pthread_key_t
*keys
= dtorcheck_keys
[id
]; /* an array of KEYS_COUNT keys */
325 for (i
= 0; i
< KEYS_COUNT
; i
++)
326 ASSERT (pthread_key_create (&keys
[i
], destructor_table
[i
]) == 0);
328 for (i
= 0; i
< KEYS_COUNT
; i
++)
329 ASSERT (pthread_setspecific (keys
[i
],
330 (void *) (uintptr_t) (10 * id
+ i
+ 1))
337 test_tss_dtorcheck1 (void)
339 pthread_t threads
[THREAD_COUNT
];
342 uintptr_t expected_sum
;
346 /* Spawn the threads. */
347 for (id
= 0; id
< THREAD_COUNT
; id
++)
348 ASSERT (pthread_create (&threads
[id
], NULL
,
349 dtorcheck1_thread
, (void *) (uintptr_t) id
)
352 /* Wait for the threads to terminate. */
353 for (id
= 0; id
< THREAD_COUNT
; id
++)
354 ASSERT (pthread_join (threads
[id
], NULL
) == 0);
356 /* Clean up the keys. */
357 for (id
= 0; id
< THREAD_COUNT
; id
++)
358 for (i
= 0; i
< KEYS_COUNT
; i
++)
359 ASSERT (pthread_key_delete (dtorcheck_keys
[id
][i
]) == 0);
361 /* Check that the destructor was invoked for each key. */
362 expected_sum
= 10 * KEYS_COUNT
* (THREAD_COUNT
* (THREAD_COUNT
- 1) / 2)
363 + THREAD_COUNT
* (KEYS_COUNT
* (KEYS_COUNT
- 1) / 2)
364 + THREAD_COUNT
* KEYS_COUNT
;
365 if (sum
!= expected_sum
)
369 /* Worker thread that uses destructors that verify that the destructor belongs
370 to the right key allocated within the thread. */
372 dtorcheck2_thread (void *arg
)
374 unsigned int id
= (unsigned int) (uintptr_t) arg
;
375 pthread_key_t
*keys
= dtorcheck_keys
[id
]; /* an array of KEYS_COUNT keys */
378 for (i
= 0; i
< KEYS_COUNT
; i
++)
379 ASSERT (pthread_key_create (&keys
[i
], destructor_table
[id
]) == 0);
381 for (i
= 0; i
< KEYS_COUNT
; i
++)
382 ASSERT (pthread_setspecific (keys
[i
],
383 (void *) (uintptr_t) (10 * i
+ id
+ 1))
390 test_tss_dtorcheck2 (void)
392 pthread_t threads
[THREAD_COUNT
];
395 uintptr_t expected_sum
;
399 /* Spawn the threads. */
400 for (id
= 0; id
< THREAD_COUNT
; id
++)
401 ASSERT (pthread_create (&threads
[id
], NULL
,
402 dtorcheck2_thread
, (void *) (uintptr_t) id
)
405 /* Wait for the threads to terminate. */
406 for (id
= 0; id
< THREAD_COUNT
; id
++)
407 ASSERT (pthread_join (threads
[id
], NULL
) == 0);
409 /* Clean up the keys. */
410 for (id
= 0; id
< THREAD_COUNT
; id
++)
411 for (i
= 0; i
< KEYS_COUNT
; i
++)
412 ASSERT (pthread_key_delete (dtorcheck_keys
[id
][i
]) == 0);
414 /* Check that the destructor was invoked for each key. */
415 expected_sum
= 10 * THREAD_COUNT
* (KEYS_COUNT
* (KEYS_COUNT
- 1) / 2)
416 + KEYS_COUNT
* (THREAD_COUNT
* (THREAD_COUNT
- 1) / 2)
417 + THREAD_COUNT
* KEYS_COUNT
;
418 if (sum
!= expected_sum
)
426 /* --- Test thread-local storage with races between init and destroy --- */
428 /* Number of simultaneous threads. */
429 #if defined __ANDROID__
430 # define THREAD_COUNT 5 /* to avoid a pthread_key_create failure */
432 # define THREAD_COUNT 10
435 /* Number of keys to allocate in each thread. */
436 #define KEYS_COUNT 10
438 /* Number of times to destroy and reallocate a key in each thread. */
439 #define REPEAT_COUNT 100000
441 static pthread_key_t racecheck_keys
[THREAD_COUNT
][KEYS_COUNT
];
443 /* Worker thread that does many destructions and reallocations of keys, and also
444 uses destructors that verify that the destructor belongs to the right key. */
446 racecheck_thread (void *arg
)
448 unsigned int id
= (unsigned int) (uintptr_t) arg
;
449 pthread_key_t
*keys
= racecheck_keys
[id
]; /* an array of KEYS_COUNT keys */
453 dbgprintf ("Worker %p started\n", pthread_self_pointer ());
455 for (i
= 0; i
< KEYS_COUNT
; i
++)
457 ASSERT (pthread_key_create (&keys
[i
], destructor_table
[i
]) == 0);
458 ASSERT (pthread_setspecific (keys
[i
],
459 (void *) (uintptr_t) (10 * id
+ i
+ 1))
463 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
465 i
= ((unsigned long) random () >> 3) % KEYS_COUNT
;
466 dbgprintf ("Worker %p reallocating key %d\n", pthread_self_pointer (), i
);
467 ASSERT (pthread_key_delete (keys
[i
]) == 0);
468 ASSERT (pthread_key_create (&keys
[i
], destructor_table
[i
]) == 0);
469 ASSERT (pthread_setspecific (keys
[i
],
470 (void *) (uintptr_t) (10 * id
+ i
+ 1))
474 dbgprintf ("Worker %p dying.\n", pthread_self_pointer ());
479 test_tss_racecheck (void)
481 pthread_t threads
[THREAD_COUNT
];
484 uintptr_t expected_sum
;
488 /* Spawn the threads. */
489 for (id
= 0; id
< THREAD_COUNT
; id
++)
490 ASSERT (pthread_create (&threads
[id
], NULL
,
491 racecheck_thread
, (void *) (uintptr_t) id
)
494 /* Wait for the threads to terminate. */
495 for (id
= 0; id
< THREAD_COUNT
; id
++)
496 ASSERT (pthread_join (threads
[id
], NULL
) == 0);
498 /* Clean up the keys. */
499 for (id
= 0; id
< THREAD_COUNT
; id
++)
500 for (i
= 0; i
< KEYS_COUNT
; i
++)
501 ASSERT (pthread_key_delete (racecheck_keys
[id
][i
]) == 0);
503 /* Check that the destructor was invoked for each key. */
504 expected_sum
= 10 * KEYS_COUNT
* (THREAD_COUNT
* (THREAD_COUNT
- 1) / 2)
505 + THREAD_COUNT
* (KEYS_COUNT
* (KEYS_COUNT
- 1) / 2)
506 + THREAD_COUNT
* KEYS_COUNT
;
507 if (sum
!= expected_sum
)
516 /* -------------------------------------------------------------------------- */
522 /* Declare failure if test takes too long, by using default abort
523 caused by SIGALRM. */
524 int alarm_value
= 600;
525 signal (SIGALRM
, SIG_DFL
);
530 pthread_mutexattr_t attr
;
532 ASSERT (pthread_mutexattr_init (&attr
) == 0);
533 ASSERT (pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_NORMAL
) == 0);
534 ASSERT (pthread_mutex_init (&sumlock
, &attr
) == 0);
535 ASSERT (pthread_mutexattr_destroy (&attr
) == 0);
538 printf ("Starting test_tss ..."); fflush (stdout
);
540 printf (" OK\n"); fflush (stdout
);
542 printf ("Starting test_tss_dtorcheck1 ..."); fflush (stdout
);
543 test_tss_dtorcheck1 ();
544 printf (" OK\n"); fflush (stdout
);
546 printf ("Starting test_tss_dtorcheck2 ..."); fflush (stdout
);
547 test_tss_dtorcheck2 ();
548 printf (" OK\n"); fflush (stdout
);
550 printf ("Starting test_tss_racecheck ..."); fflush (stdout
);
551 test_tss_racecheck ();
552 printf (" OK\n"); fflush (stdout
);
554 return test_exit_status
;
559 /* No multithreading available. */
566 fputs ("Skipping test: multithreading not enabled\n", stderr
);