| 149 | */ |
| 150 | |
| 151 | bool phase3() |
| 152 | { |
| 153 | #ifdef DISTORTOS_THREAD_DETACH_ENABLE |
| 154 | |
| 155 | const auto allocatedMemory = mallinfo().uordblks; |
| 156 | const auto lambda = |
| 157 | [](int& sharedRet) |
| 158 | { |
| 159 | sharedRet = ThisThread::detach(); |
| 160 | }; |
| 161 | |
| 162 | // attempt to detach static thread must fail with ENOTSUP |
| 163 | { |
| 164 | int sharedRet {}; |
| 165 | auto staticThread = makeAndStartStaticThread<testThreadStackSize>(1, lambda, std::ref(sharedRet)); |
| 166 | bool result {true}; |
| 167 | if (staticThread.detach() != ENOTSUP) // static thread cannot be detached |
| 168 | result = false; |
| 169 | if (staticThread.getState() != ThreadState::runnable) |
| 170 | result = false; |
| 171 | if (staticThread.getIdentifier() == ThreadIdentifier{}) |
| 172 | result = false; |
| 173 | if (staticThread.join() != 0 || result == false || sharedRet != ENOTSUP) // self-detach must fail too |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // detaching dynamic thread that is not yet started must succeed, but the thread is just deleted and does not run |
| 178 | { |
| 179 | int sharedRet {0x2e981e48}; |
| 180 | auto dynamicThread = makeDynamicThread({testThreadStackSize, 1}, lambda, std::ref(sharedRet)); |
| 181 | bool result {true}; |
| 182 | if (dynamicThread.detach() != 0) |
| 183 | result = false; |
| 184 | if (dynamicThread.start() != EINVAL) // detached thread cannot be started |
| 185 | result = false; |
| 186 | if (dynamicThread.getState() != ThreadState::detached) |
| 187 | result = false; |
| 188 | if (dynamicThread.getIdentifier() != ThreadIdentifier{}) |
| 189 | result = false; |
| 190 | if (dynamicThread.join() != EINVAL || result == false || sharedRet != 0x2e981e48 || |
| 191 | dynamicThread.detach() != EINVAL) // detached thread cannot be joined, self-detach must fail |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | if (mallinfo().uordblks != allocatedMemory) // dynamic memory must be deallocated after each test phase |
| 196 | return false; |
| 197 | |
| 198 | // detaching dynamic thread that is started, but not yet terminated, must succeed |
| 199 | { |
| 200 | int sharedRet {0x0f0dad58}; |
| 201 | auto dynamicThread = makeAndStartDynamicThread({testThreadStackSize, 1}, lambda, std::ref(sharedRet)); |
| 202 | bool result {true}; |
| 203 | if (dynamicThread.detach() != 0) |
| 204 | result = false; |
| 205 | if (dynamicThread.getState() != ThreadState::detached) |
| 206 | result = false; |
| 207 | if (dynamicThread.getIdentifier() != ThreadIdentifier{}) |
| 208 | result = false; |
nothing calls this directly
no test coverage detected