2 * Copyright (c) 2016 Mahdi Mokhtari <mokhi64@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <atf-c++.hpp>
36 static FILE *output = NULL;
39 Foo() { ATF_REQUIRE(fprintf(output, "Created\n") > 0); }
40 ~Foo() { ATF_REQUIRE(fprintf(output, "Destroyed\n") > 0); }
41 void use() { ATF_REQUIRE(fprintf(output, "Used\n") > 0); }
47 thread_local static Foo foo;
48 ATF_REQUIRE(fprintf(output, "DIED\n") > 0);
53 extern "C" int __cxa_thread_atexit(void (*)(void *), void *, void *);
59 __cxa_thread_atexit(again, arg, &output);
70 static thread_local Foo f;
71 static thread_local Foo g;
72 static thread_local Bar h;
73 static thread_local Baz e;
76 * This test must be linked to libpthread.
78 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thr);
79 ATF_TEST_CASE_BODY(cxx__thr)
83 /* Avoid coredump during f construction. */
86 libthr_handle = dlopen("libthr.so.3", RTLD_LAZY | RTLD_GLOBAL |
88 ATF_REQUIRE(libthr_handle != NULL);
89 dlclose(libthr_handle);
93 * In this test f.use() will test cxa_thread_atexit() in non-threaded mode.
94 * After f.use() main will be threaded and we'll have one additional thread
95 * with its own TLS data.
97 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_before);
98 ATF_TEST_CASE_BODY(cxx__thread_local_before)
100 static const char out_log[] = "Created\nCreated\nUsed\nCreated\n"
101 "Created\nUsed\nCreated\nDIED\nDestroyed\nDestroyed\nDestroyed\n";
103 ATF_REQUIRE((output = fopen("test_before.txt", "w")) != NULL);
106 std::thread t([]() { f.use(); });
111 ATF_REQUIRE(atf::utils::compare_file("test_before.txt", out_log));
115 * In this test, f.use() will test __cxa_thread_atexit()
116 * in threaded mode (but still in main-threaed).
118 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_after);
119 ATF_TEST_CASE_BODY(cxx__thread_local_after)
121 static const char out_log[] = "Created\nCreated\nUsed\nCreated\n"
122 "DIED\nDestroyed\nDestroyed\nDestroyed\nCreated\nCreated\nUsed\n";
124 ATF_REQUIRE((output = fopen("test_after.txt", "w")) != NULL);
126 std::thread t([]() { g.use(); });
133 ATF_REQUIRE(atf::utils::compare_file("test_after.txt", out_log));
137 * In this test, we register a new dtor while dtors are being run
138 * in __cxa_thread_atexit().
140 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_add_while_calling_dtors);
141 ATF_TEST_CASE_BODY(cxx__thread_local_add_while_calling_dtors)
143 static const char out_log[] = "Created\nCreated\nCreated\nDIED\n"
144 "Destroyed\nDestroyed\nDestroyed\n";
146 ATF_REQUIRE((output = fopen("test_add_meanwhile.txt", "w")) != NULL);
148 std::thread t([]() { h.use(); });
154 ATF_REQUIRE(atf::utils::compare_file("test_add_meanwhile.txt", out_log));
157 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_inf_dtors);
158 ATF_TEST_CASE_BODY(cxx__thread_inf_dtors)
162 * Only added to make isolated run of this test not
163 * coredumping. Construction of Foo objects require filled
168 std::thread t([]() { e.use(); });
172 ATF_INIT_TEST_CASES(tcs)
175 ATF_ADD_TEST_CASE(tcs, cxx__thr);
176 ATF_ADD_TEST_CASE(tcs, cxx__thread_local_before);
177 ATF_ADD_TEST_CASE(tcs, cxx__thread_local_after);
178 ATF_ADD_TEST_CASE(tcs, cxx__thread_local_add_while_calling_dtors);
179 ATF_ADD_TEST_CASE(tcs, cxx__thread_inf_dtors);