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. */
25 /* Whether to help the scheduler through explicit thrd_yield().
26 Uncomment this to see if the operating system has a fair scheduler. */
27 #define EXPLICIT_YIELD 1
29 /* Whether to print debugging messages. */
30 #define ENABLE_DEBUGGING 0
32 /* Number of simultaneous threads. */
33 #define THREAD_COUNT 16
35 /* Number of operations performed in each thread. */
36 #define REPEAT_COUNT 50000
50 # define dbgprintf printf
52 # define dbgprintf if (0) printf
56 # define yield() thrd_yield ()
61 /* Returns a reference to the current thread as a pointer, for debugging. */
63 /* On IBM z/OS, pthread_t is a struct with an 8-byte '__' field.
64 The first three bytes of this field appear to uniquely identify a
65 pthread_t, though not necessarily representing a pointer. */
66 # define thrd_current_pointer() (*((void **) thrd_current ().__))
68 /* On Solaris, thrd_t is merely an 'unsigned int'. */
69 # define thrd_current_pointer() ((void *) (uintptr_t) thrd_current ())
71 # define thrd_current_pointer() ((void *) thrd_current ())
77 /* Call yield () only with a certain probability, otherwise the
78 sequence of thread activations may be too predictable. */
79 if ((((unsigned int) rand () >> 3) % 4) == 0)
84 /* ----------------------- Test thread-local storage ----------------------- */
87 static unsigned int thread_local value0
;
88 static unsigned int thread_local value1
;
89 static unsigned int thread_local value2
;
90 static unsigned int thread_local value3
;
93 worker_thread (void *arg
)
95 unsigned int id
= (unsigned int) (uintptr_t) arg
;
97 unsigned int *values
[KEYS_COUNT
] = { &value0
, &value1
, &value2
, &value3
};
99 dbgprintf ("Worker %p started\n", thrd_current_pointer ());
101 /* Initialize the per-thread storage. */
102 dbgprintf ("Worker %p before first assignment\n", thrd_current_pointer ());
103 for (i
= 0; i
< KEYS_COUNT
; i
++)
105 *values
[i
] = (((unsigned int) rand () >> 3) % 1000000) * THREAD_COUNT
+ id
;
106 /* Hopefully no arithmetic overflow. */
107 if ((*values
[i
] % THREAD_COUNT
) != id
)
110 dbgprintf ("Worker %p after first assignment\n", thrd_current_pointer ());
113 /* Shuffle around the pointers. */
114 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
116 dbgprintf ("Worker %p doing value swapping\n", thrd_current_pointer ());
117 i
= ((unsigned int) rand () >> 3) % KEYS_COUNT
;
118 j
= ((unsigned int) rand () >> 3) % KEYS_COUNT
;
121 unsigned int vi
= *values
[i
];
122 unsigned int vj
= *values
[j
];
130 /* Verify that all the values are from this thread. */
131 dbgprintf ("Worker %p before final verify\n", thrd_current_pointer ());
132 for (i
= 0; i
< KEYS_COUNT
; i
++)
133 if ((*values
[i
] % THREAD_COUNT
) != id
)
135 dbgprintf ("Worker %p after final verify\n", thrd_current_pointer ());
138 dbgprintf ("Worker %p dying.\n", thrd_current_pointer ());
143 test_thread_local (void)
147 for (pass
= 0; pass
< 2; pass
++)
149 thrd_t threads
[THREAD_COUNT
];
151 /* Spawn the threads. */
152 for (i
= 0; i
< THREAD_COUNT
; i
++)
153 ASSERT (thrd_create (&threads
[i
], worker_thread
, (void *) (uintptr_t) i
)
156 /* Wait for the threads to terminate. */
157 for (i
= 0; i
< THREAD_COUNT
; i
++)
158 ASSERT (thrd_join (threads
[i
], NULL
) == thrd_success
);
163 /* -------------------------------------------------------------------------- */
169 /* Declare failure if test takes too long, by using default abort
170 caused by SIGALRM. */
171 int alarm_value
= 600;
172 signal (SIGALRM
, SIG_DFL
);
176 printf ("Starting test_thread_local ..."); fflush (stdout
);
177 test_thread_local ();
178 printf (" OK\n"); fflush (stdout
);
185 /* No thread-local storage support available in the compiler and linker. */
192 fputs ("Skipping test: thread_local not supported\n", stderr
);