hurd: Also make it possible to call strlen very early
[glibc.git] / nptl / tst-thread_local1.cc
blobe7cd0e310227e98e5e2f2b7425f5aab526897ecc
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/>. */
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <string.h>
24 #include <array>
25 #include <functional>
26 #include <string>
27 #include <thread>
29 struct counter
31 int constructed {};
32 int destructed {};
34 void reset ();
37 void
38 counter::reset ()
40 constructed = 0;
41 destructed = 0;
44 static std::string
45 to_string (const counter &c)
47 char buf[128];
48 snprintf (buf, sizeof (buf), "%d/%d",
49 c.constructed, c.destructed);
50 return buf;
53 template <counter *Counter>
54 struct counting
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.
80 asm ("");
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);
89 static std::string
90 all_counters ()
92 return to_string (counter_static)
93 + ' ' + to_string (counter_anonymous_namespace)
94 + ' ' + to_string (counter_extern)
95 + ' ' + to_string (counter_function_local);
98 static void
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);
106 errors = true;
110 static void
111 reset_all ()
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;
120 namespace {
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;
127 static void *
128 thread_without_access (void *)
130 return nullptr;
133 static 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");
143 return nullptr;
146 static int
147 do_test (void)
149 std::function<void (void *(void *))> do_pthread =
150 [](void *(func) (void *))
152 pthread_t thr;
153 int ret = pthread_create (&thr, nullptr, func, nullptr);
154 if (ret != 0)
156 errno = ret;
157 printf ("error: pthread_create: %m\n");
158 errors = true;
159 return;
161 ret = pthread_join (thr, nullptr);
162 if (ret != 0)
164 errno = ret;
165 printf ("error: pthread_join: %m\n");
166 errors = true;
167 return;
170 std::function<void (void *(void *))> do_std_thread =
171 [](void *(func) (void *))
173 std::thread thr{[func] {func (nullptr);}};
174 thr.join ();
177 std::array<std::pair<const char *, std::function<void (void *(void *))>>, 2>
178 do_thread_X
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");
190 reset_all ();
191 do_thread.second (thread_with_access);
192 check_counters ("after thread_with_access", "1/1 1/1 1/1 1/1");
193 reset_all ();
196 return errors;
199 #define TEST_FUNCTION do_test ()
200 #include "../test-skeleton.c"