diff --git a/src/BackgroundThread.cpp b/src/BackgroundThread.cpp index 3fb3fa6..54adcc3 100644 --- a/src/BackgroundThread.cpp +++ b/src/BackgroundThread.cpp @@ -15,6 +15,10 @@ BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread: BackgroundThread::~BackgroundThread() { DEBUG_FUNCTION_LINE("Shutting down FTP Server"); + stopThread(); + while (!hasThreadStopped()) { + OSSleepTicks(OSMillisecondsToTicks(10)); + } if (this->serverSocket >= 0) { cleanup_ftp(); network_close(this->serverSocket); diff --git a/src/main.cpp b/src/main.cpp index 656f563..29ca0bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -121,7 +121,6 @@ void startServer() { } } - void stopServer() { BackgroundThread::destroyInstance(); if (gMochaPathsWereMounted) { diff --git a/src/utils/BackgroundThreadWrapper.cpp b/src/utils/BackgroundThreadWrapper.cpp index d20662e..980d865 100644 --- a/src/utils/BackgroundThreadWrapper.cpp +++ b/src/utils/BackgroundThreadWrapper.cpp @@ -6,6 +6,7 @@ BackgroundThreadWrapper::BackgroundThreadWrapper(int32_t priority) : CThread(CTh BackgroundThreadWrapper::~BackgroundThreadWrapper() { exitThread = 1; + stopThread(); OSMemoryBarrier(); } @@ -18,4 +19,6 @@ void BackgroundThreadWrapper::executeThread() { break; } } + threadEnded = true; + OSMemoryBarrier(); } diff --git a/src/utils/BackgroundThreadWrapper.hpp b/src/utils/BackgroundThreadWrapper.hpp index 44c0f29..7e7331c 100644 --- a/src/utils/BackgroundThreadWrapper.hpp +++ b/src/utils/BackgroundThreadWrapper.hpp @@ -12,17 +12,26 @@ public: protected: [[nodiscard]] BOOL shouldExit() const { - return (exitThread == 1); + return (exitThread); } void setThreadPriority(int32_t priority) override { CThread::setThreadPriority(priority); } + void stopThread() { + exitThread = true; + } + + bool hasThreadStopped() { + return threadEnded; + } + private: + volatile bool threadEnded = false; + volatile bool exitThread = false; + void executeThread() override; virtual BOOL whileLoop() = 0; - - volatile int32_t exitThread = 0; };