* @brief 测试线程组的动态加入和离开 */
| 121 | * @brief 测试线程组的动态加入和离开 |
| 122 | */ |
| 123 | void test_thread_group_dynamic(void* /*arg*/) { |
| 124 | klog::Info("=== Thread Group Dynamic Test ==="); |
| 125 | |
| 126 | auto leader_holder = kstd::make_unique<TaskControlBlock>("DynamicLeader", 10, |
| 127 | nullptr, nullptr); |
| 128 | auto* leader = leader_holder.get(); |
| 129 | leader->pid = g_local_pid_counter.fetch_add(1); |
| 130 | leader->aux->tgid = leader->pid; |
| 131 | |
| 132 | constexpr int kThreadCount = 5; |
| 133 | etl::unique_ptr<TaskControlBlock> thread_holders[kThreadCount]; |
| 134 | TaskControlBlock* threads[kThreadCount]; |
| 135 | |
| 136 | for (int i = 0; i < kThreadCount; ++i) { |
| 137 | thread_holders[i] = kstd::make_unique<TaskControlBlock>("DynamicThread", 10, |
| 138 | nullptr, nullptr); |
| 139 | threads[i] = thread_holders[i].get(); |
| 140 | threads[i]->pid = g_local_pid_counter.fetch_add(1); |
| 141 | } |
| 142 | |
| 143 | // 动态加入 |
| 144 | klog::Info("Joining threads..."); |
| 145 | for (int i = 0; i < kThreadCount; ++i) { |
| 146 | threads[i]->JoinThreadGroup(leader); |
| 147 | size_t size = leader->GetThreadGroupSize(); |
| 148 | klog::Debug("After join {}: group size={}", i, size); |
| 149 | } |
| 150 | |
| 151 | size_t final_size = leader->GetThreadGroupSize(); |
| 152 | klog::Info("Final group size: {} (expected {})", final_size, |
| 153 | kThreadCount + 1); |
| 154 | |
| 155 | klog::Info("Leaving threads..."); |
| 156 | for (int i = 0; i < kThreadCount; ++i) { |
| 157 | threads[i]->LeaveThreadGroup(); |
| 158 | size_t size = leader->GetThreadGroupSize(); |
| 159 | klog::Debug("After leave {}: group size={}", i, size); |
| 160 | } |
| 161 | |
| 162 | size_t remaining_size = leader->GetThreadGroupSize(); |
| 163 | klog::Info("Remaining group size: {} (expected 1)", remaining_size); |
| 164 | |
| 165 | bool passed = (final_size == static_cast<size_t>(kThreadCount + 1) && |
| 166 | remaining_size == 1); |
| 167 | if (passed) { |
| 168 | klog::Info("Thread Group Dynamic Test: PASS"); |
| 169 | } else { |
| 170 | klog::Err("Thread Group Dynamic Test: FAIL"); |
| 171 | g_tests_failed++; |
| 172 | } |
| 173 | |
| 174 | g_tests_completed++; |
| 175 | sys_exit(0); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @brief 测试线程组成员同时退出 |
nothing calls this directly
no test coverage detected