Fix potential softlock that could happen if the thread was canceled but is not (marked as) terminated

This commit is contained in:
Maschell 2022-10-06 11:55:58 +02:00
parent 352946fbf2
commit 7f963d1900
2 changed files with 25 additions and 18 deletions

View File

@ -24,25 +24,26 @@ INITIALIZE_PLUGIN() {
} }
} }
void stopThread() {
if (thread != nullptr) {
delete thread;
thread = nullptr;
}
}
/* Entry point */ /* Entry point */
ON_APPLICATION_START() { ON_APPLICATION_START() {
initLogging(); initLogging();
stopThread();
if (thread != nullptr) {
DEBUG_FUNCTION_LINE_WARN("The wiiload thread is still allocated but not running.");
thread->skipJoin = true;
delete thread;
thread = nullptr;
}
DEBUG_FUNCTION_LINE("Start wiiload thread"); DEBUG_FUNCTION_LINE("Start wiiload thread");
thread = new TcpReceiver(4299); thread = new TcpReceiver(4299);
} }
ON_APPLICATION_REQUESTS_EXIT() { ON_APPLICATION_REQUESTS_EXIT() {
DEBUG_FUNCTION_LINE("Stop wiiload thread"); DEBUG_FUNCTION_LINE("Stop wiiload thread");
stopThread(); if (thread != nullptr) {
delete thread;
thread = nullptr;
}
deinitLogging(); deinitLogging();
} }

View File

@ -100,17 +100,22 @@ public:
//! Shutdown thread //! Shutdown thread
void shutdownThread() { void shutdownThread() {
//! wait for thread to finish if (skipJoin) {
if (pThread && !(iAttributes & eAttributeDetach)) { DEBUG_FUNCTION_LINE_WARN("Skip joining the thread \"%s\", it's not running.", pThreadName.c_str());
if (!OSIsThreadTerminated(pThread)) { } else {
if (isThreadSuspended()) { //! wait for thread to finish
resumeThread(); if (!skipJoin && pThread && !(iAttributes & eAttributeDetach)) {
if (!OSIsThreadTerminated(pThread)) {
if (isThreadSuspended()) {
resumeThread();
}
OSJoinThread(pThread, nullptr);
} else {
DEBUG_FUNCTION_LINE_WARN("Thread \"%s\" has already been terminated!!!", pThreadName.c_str());
} }
OSJoinThread(pThread, nullptr);
} else {
DEBUG_FUNCTION_LINE_WARN("Thread \"%s\" has already been terminated!!!", pThreadName.c_str());
} }
} }
//! free the thread stack buffer //! free the thread stack buffer
if (pThreadStack) { if (pThreadStack) {
free(pThreadStack); free(pThreadStack);
@ -132,6 +137,7 @@ public:
eAttributeDetach = 0x08, eAttributeDetach = 0x08,
eAttributePinnedAff = 0x10 eAttributePinnedAff = 0x10
}; };
bool skipJoin = false;
private: private:
static int threadCallback(int argc, const char **argv) { static int threadCallback(int argc, const char **argv) {