1 /* Test basic thread_local support.
2 Copyright (C) 2015-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
45 to_string (const counter
&c
)
48 snprintf (buf
, sizeof (buf
), "%d/%d",
49 c
.constructed
, c
.destructed
);
53 template <counter
*Counter
>
56 counting () __attribute__ ((noinline
, noclone
));
57 ~counting () __attribute__ ((noinline
, noclone
));
58 void operation () __attribute__ ((noinline
, noclone
));
61 template<counter
*Counter
>
62 __attribute__ ((noinline
, noclone
))
63 counting
<Counter
>::counting ()
65 ++Counter
->constructed
;
68 template<counter
*Counter
>
69 __attribute__ ((noinline
, noclone
))
70 counting
<Counter
>::~counting ()
72 ++Counter
->destructed
;
75 template<counter
*Counter
>
76 void __attribute__ ((noinline
, noclone
))
77 counting
<Counter
>::operation ()
79 // Optimization barrier.
83 static counter counter_static
;
84 static counter counter_anonymous_namespace
;
85 static counter counter_extern
;
86 static counter counter_function_local
;
87 static bool errors (false);
92 return to_string (counter_static
)
93 + ' ' + to_string (counter_anonymous_namespace
)
94 + ' ' + to_string (counter_extern
)
95 + ' ' + to_string (counter_function_local
);
99 check_counters (const char *name
, const char *expected
)
101 std::string actual
{all_counters ()};
102 if (actual
!= expected
)
104 printf ("error: %s: (%s) != (%s)\n",
105 name
, actual
.c_str (), expected
);
113 counter_static
.reset ();
114 counter_anonymous_namespace
.reset ();
115 counter_extern
.reset ();
116 counter_function_local
.reset ();
119 static thread_local counting
<&counter_static
> counting_static
;
121 thread_local counting
<&counter_anonymous_namespace
>
122 counting_anonymous_namespace
;
124 extern thread_local counting
<&counter_extern
> counting_extern
;
125 thread_local counting
<&counter_extern
> counting_extern
;
128 thread_without_access (void *)
134 thread_with_access (void *)
136 thread_local counting
<&counter_function_local
> counting_function_local
;
137 counting_function_local
.operation ();
138 check_counters ("early in thread_with_access", "0/0 0/0 0/0 1/0");
139 counting_static
.operation ();
140 counting_anonymous_namespace
.operation ();
141 counting_extern
.operation ();
142 check_counters ("in thread_with_access", "1/0 1/0 1/0 1/0");
149 std::function
<void (void *(void *))> do_pthread
=
150 [](void *(func
) (void *))
153 int ret
= pthread_create (&thr
, nullptr, func
, nullptr);
157 printf ("error: pthread_create: %m\n");
161 ret
= pthread_join (thr
, nullptr);
165 printf ("error: pthread_join: %m\n");
170 std::function
<void (void *(void *))> do_std_thread
=
171 [](void *(func
) (void *))
173 std::thread thr
{[func
] {func (nullptr);}};
177 std::array
<std::pair
<const char *, std::function
<void (void *(void *))>>, 2>
180 {"pthread_create", do_pthread
},
181 {"std::thread", do_std_thread
},
184 for (auto do_thread
: do_thread_X
)
186 printf ("info: testing %s\n", do_thread
.first
);
187 check_counters ("initial", "0/0 0/0 0/0 0/0");
188 do_thread
.second (thread_without_access
);
189 check_counters ("after thread_without_access", "0/0 0/0 0/0 0/0");
191 do_thread
.second (thread_with_access
);
192 check_counters ("after thread_with_access", "1/1 1/1 1/1 1/1");
199 #define TEST_FUNCTION do_test ()
200 #include "../test-skeleton.c"