| 192 | } |
| 193 | |
| 194 | void Threads::exit(void) |
| 195 | { |
| 196 | // Idempotent: called explicitly at the end of main (so every worker is joined |
| 197 | // before exit() runs static destructors) and again via atexit as a backstop |
| 198 | // for early-error exits that never reach the explicit call. |
| 199 | static std::atomic<bool> alreadyExited{false}; |
| 200 | if (alreadyExited.exchange(true)) { |
| 201 | return; |
| 202 | } |
| 203 | workerTasks.lock()->clear(); |
| 204 | LightSemaphore_Release(&moreTasks, numWorkers); |
| 205 | svcSignalEvent(threads.lock()->second[0]); |
| 206 | threadJoin(reaperThread, U64_MAX); |
| 207 | threadFree(reaperThread); |
| 208 | svcCloseHandle(threads.lock()->second[0]); |
| 209 | svcCloseHandle(threads.lock()->second[1]); |
| 210 | } |