| 1 | /* |
| 2 | Copyright (c) 2019, 2026, Oracle and/or its affiliates. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License, version 2.0, |
| 6 | as published by the Free Software Foundation. |
| 7 | |
| 8 | This program is designed to work with certain software (including |
| 9 | but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | as designated in a particular file or component or in included license |
| 11 | documentation. The authors of MySQL hereby grant you an additional |
| 12 | permission to link the program and your derivative works with the |
| 13 | separately licensed software that they have either included with |
| 14 | the program or referenced in the documentation. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License, version 2.0, for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "os0thread-create.h" |
| 29 | |
| 30 | extern uint32_t srv_max_n_threads; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | struct Global_init { |
| 35 | Global_init() { srv_max_n_threads = 100; } |
| 36 | }; |
| 37 | |
| 38 | static Global_init init{}; |
| 39 | |
| 40 | TEST(CreateDetachedThreadTest, BasicUsage) { |
| 41 | std::atomic_bool stop{false}, active{false}; |
| 42 | |
| 43 | auto f = [&stop, &active](int arg) { |
| 44 | EXPECT_EQ(42, arg); |
| 45 | active.store(true); |
| 46 | while (!stop.load()) { |
| 47 | UT_RELAX_CPU(); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | IB_thread thread; |
| 52 | EXPECT_FALSE(thread_is_active(thread)); |
| 53 | |
| 54 | thread = create_detached_thread(0, 0, std::move(f), 42); |
| 55 | EXPECT_FALSE(thread_is_active(thread)); |
| 56 | |
| 57 | thread.start(); |
| 58 | EXPECT_TRUE(thread_is_active(thread)); |
| 59 | |
| 60 | while (!active.load()) { |
| 61 | UT_RELAX_CPU(); |
| 62 | } |
| 63 | |
| 64 | EXPECT_TRUE(thread_is_active(thread)); |
| 65 | |
| 66 | stop.store(true); |
| 67 | |
| 68 | while (thread_is_active(thread)) { |
| 69 | UT_RELAX_CPU(); |
| 70 | } |
| 71 | |
| 72 | thread = {}; |
| 73 | |
| 74 | EXPECT_FALSE(thread_is_active(thread)); |
| 75 | EXPECT_FALSE(os_thread_any_active()); |
| 76 | } |
| 77 | |
| 78 | TEST(CreateDetachedThreadTest, Move) { |
| 79 | std::atomic_bool stop{false}, done{false}; |
| 80 | |
| 81 | auto f = [&stop, &done](int arg) { |
| 82 | EXPECT_EQ(42, arg); |
| 83 | while (!stop.load()) { |
| 84 | UT_RELAX_CPU(); |
| 85 | } |
| 86 | |
| 87 | done.store(true); |
| 88 | }; |
| 89 | |
| 90 | IB_thread t1, t2; |
| 91 | |
| 92 | t1 = create_detached_thread(0, 0, std::move(f), 42); |
| 93 | t1.start(); |
| 94 | EXPECT_TRUE(thread_is_active(t1)); |
| 95 | |
| 96 | t2 = t1; |
| 97 | EXPECT_TRUE(thread_is_active(t1)); |
| 98 | EXPECT_TRUE(thread_is_active(t2)); |
| 99 | |
| 100 | t2 = std::move(t1); |
| 101 | |
| 102 | EXPECT_FALSE(thread_is_active(t1)); |
| 103 | EXPECT_TRUE(thread_is_active(t2)); |
| 104 | |
| 105 | t1 = std::move(t2); |
| 106 | |
| 107 | EXPECT_TRUE(thread_is_active(t1)); |
| 108 | EXPECT_FALSE(thread_is_active(t2)); |
| 109 | |
| 110 | t2 = t1; |
| 111 | |
| 112 | EXPECT_TRUE(thread_is_active(t1)); |
| 113 | EXPECT_TRUE(thread_is_active(t2)); |
| 114 | |
| 115 | t1 = {}; |
| 116 | |
| 117 | EXPECT_FALSE(thread_is_active(t1)); |
| 118 | EXPECT_TRUE(thread_is_active(t2)); |
| 119 | |
| 120 | t1 = std::move(t2); |
| 121 | t2 = {}; |
| 122 | |
| 123 | EXPECT_FALSE(thread_is_active(t2)); |
| 124 | EXPECT_TRUE(thread_is_active(t1)); |
| 125 | |
| 126 | t1 = {}; |
| 127 | EXPECT_FALSE(thread_is_active(t1)); |
| 128 | |
| 129 | EXPECT_FALSE(done.load()); |
| 130 | EXPECT_TRUE(os_thread_any_active()); |
| 131 | |
| 132 | stop.store(true); |
| 133 | while (os_thread_any_active()) { |
| 134 | UT_RELAX_CPU(); |
| 135 | } |
| 136 | EXPECT_TRUE(done.load()); |
| 137 | } |
| 138 | |
| 139 | } // namespace |