| 49 | } |
| 50 | |
| 51 | bool Threads::create(std::optional<size_t> stackSize, void (*entrypoint)(void)) |
| 52 | { |
| 53 | // Reap the previous worker before reusing the slot. The sole caller |
| 54 | // (ScriptRunner) only creates a new thread once the previous run's result |
| 55 | // was collected, so this wait is at most the tail of a returning thread. |
| 56 | join(); |
| 57 | |
| 58 | // Stack size must be page-aligned or threadCreate fails with |
| 59 | // LibnxError_BadInput (see the network thread in main.cpp). |
| 60 | size_t stack = stackSize.value_or(0x8000); |
| 61 | stack = (stack + 0xFFF) & ~(size_t)0xFFF; |
| 62 | |
| 63 | gEntry = entrypoint; |
| 64 | Result rc = threadCreate(&gThread, thunk, nullptr, nullptr, stack, 0x2C, -2); |
| 65 | if (R_FAILED(rc)) { |
| 66 | Logging::error("[thread] threadCreate failed with result 0x{:08X}", rc); |
| 67 | return false; |
| 68 | } |
| 69 | if (R_FAILED(rc = threadStart(&gThread))) { |
| 70 | Logging::error("[thread] threadStart failed with result 0x{:08X}", rc); |
| 71 | threadClose(&gThread); |
| 72 | return false; |
| 73 | } |
| 74 | gThreadLive = true; |
| 75 | return true; |
| 76 | } |
no test coverage detected