From 6e2ea759171af82f686870e2713e41273d48d281 Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 6 Oct 2022 23:16:31 +0200 Subject: [PATCH] Fix exiting --- src/BackgroundThread.cpp | 7 ++----- src/BackgroundThread.hpp | 10 ++-------- src/main.cpp | 4 ---- src/utils/BackgroundThreadWrapper.hpp | 2 -- src/utils/CThread.h | 23 +++++++---------------- 5 files changed, 11 insertions(+), 35 deletions(-) diff --git a/src/BackgroundThread.cpp b/src/BackgroundThread.cpp index 322929e..ebbceea 100644 --- a/src/BackgroundThread.cpp +++ b/src/BackgroundThread.cpp @@ -7,7 +7,6 @@ BackgroundThread *BackgroundThread::instance = nullptr; BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread::getPriority()) { DEBUG_FUNCTION_LINE("Start FTP Server"); - std::lock_guard lock(mutex); this->serverSocket = create_server(PORT); OSMemoryBarrier(); DEBUG_FUNCTION_LINE_VERBOSE("Resume Thread"); @@ -16,7 +15,6 @@ BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread: BackgroundThread::~BackgroundThread() { DEBUG_FUNCTION_LINE("Shutting down FTP Server"); - std::lock_guard lock(mutex); if (this->serverSocket >= 0) { cleanup_ftp(); network_close(this->serverSocket); @@ -25,15 +23,14 @@ BackgroundThread::~BackgroundThread() { } BOOL BackgroundThread::whileLoop() { - std::lock_guard lock(mutex); if (this->serverSocket >= 0) { network_down = process_ftp_events(this->serverSocket); if (network_down) { - DEBUG_FUNCTION_LINE_VERBOSE("Network is down %d", this->serverSocket); + DEBUG_FUNCTION_LINE_WARN("Network is down"); cleanup_ftp(); network_close(this->serverSocket); this->serverSocket = -1; - DCFlushRange(&(this->serverSocket), 4); + OSMemoryBarrier(); } } else { this->serverSocket = create_server(PORT); diff --git a/src/BackgroundThread.hpp b/src/BackgroundThread.hpp index e229107..3459b6a 100644 --- a/src/BackgroundThread.hpp +++ b/src/BackgroundThread.hpp @@ -11,7 +11,7 @@ public: static BackgroundThread *getInstance() { if (instance == nullptr) { instance = new BackgroundThread(); - DCFlushRange(&instance, 4); + OSMemoryBarrier(); } return instance; } @@ -20,15 +20,9 @@ public: if (instance != nullptr) { delete instance; instance = nullptr; - DCFlushRange(&instance, 4); + OSMemoryBarrier(); } } - static void destroyInstance(bool forceKill) { - if (instance != nullptr) { - instance->skipJoin = true; - } - destroyInstance(); - } BackgroundThread(); diff --git a/src/main.cpp b/src/main.cpp index 823cf7f..e90b0af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,10 +55,6 @@ ON_APPLICATION_START() { nn::ac::GetAssignedAddress(&hostIpAddress); initLogging(); - //Make sure the server instance is destroyed. - // Skipping joining the thread as it's not even running at this point but still may be allocated. - BackgroundThread::destroyInstance(true); - thread = nullptr; if (gFTPServerEnabled) { startServer(); } diff --git a/src/utils/BackgroundThreadWrapper.hpp b/src/utils/BackgroundThreadWrapper.hpp index 5f32336..44c0f29 100644 --- a/src/utils/BackgroundThreadWrapper.hpp +++ b/src/utils/BackgroundThreadWrapper.hpp @@ -19,8 +19,6 @@ protected: CThread::setThreadPriority(priority); } - std::recursive_mutex mutex; - private: void executeThread() override; diff --git a/src/utils/CThread.h b/src/utils/CThread.h index 8e8c742..2d89ccd 100644 --- a/src/utils/CThread.h +++ b/src/utils/CThread.h @@ -101,22 +101,15 @@ public: //! Shutdown thread virtual void shutdownThread() { - if (skipJoin) { - DEBUG_FUNCTION_LINE_WARN("Skip joining the thread \"%s\", it's not running.", pThreadName.c_str()); - } else { - //! wait for thread to finish - if (pThread && !(iAttributes & eAttributeDetach)) { - if (!isThreadTerminated()) { - if (isThreadSuspended()) { - resumeThread(); - } - - OSJoinThread(pThread, nullptr); - } else { - DEBUG_FUNCTION_LINE_WARN("Thread \"%s\" has already been terminated!!!", pThreadName.c_str()); - } + //! wait for thread to finish + if (pThread && !(iAttributes & eAttributeDetach)) { + if (isThreadSuspended()) { + resumeThread(); } + + OSJoinThread(pThread, nullptr); } + //! free the thread stack buffer if (pThreadStack) { free(pThreadStack); @@ -139,8 +132,6 @@ public: eAttributePinnedAff = 0x10 }; - bool skipJoin = false; - private: static int threadCallback(int argc, const char **argv) { //! After call to start() continue with the internal function