Wait with thread cleanup until the thread has ended

This commit is contained in:
Maschell 2022-10-08 15:43:49 +02:00
parent f5d6c3d508
commit 47b059cb48
4 changed files with 19 additions and 4 deletions

View File

@ -15,6 +15,10 @@ BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread:
BackgroundThread::~BackgroundThread() { BackgroundThread::~BackgroundThread() {
DEBUG_FUNCTION_LINE("Shutting down FTP Server"); DEBUG_FUNCTION_LINE("Shutting down FTP Server");
stopThread();
while (!hasThreadStopped()) {
OSSleepTicks(OSMillisecondsToTicks(10));
}
if (this->serverSocket >= 0) { if (this->serverSocket >= 0) {
cleanup_ftp(); cleanup_ftp();
network_close(this->serverSocket); network_close(this->serverSocket);

View File

@ -121,7 +121,6 @@ void startServer() {
} }
} }
void stopServer() { void stopServer() {
BackgroundThread::destroyInstance(); BackgroundThread::destroyInstance();
if (gMochaPathsWereMounted) { if (gMochaPathsWereMounted) {

View File

@ -6,6 +6,7 @@ BackgroundThreadWrapper::BackgroundThreadWrapper(int32_t priority) : CThread(CTh
BackgroundThreadWrapper::~BackgroundThreadWrapper() { BackgroundThreadWrapper::~BackgroundThreadWrapper() {
exitThread = 1; exitThread = 1;
stopThread();
OSMemoryBarrier(); OSMemoryBarrier();
} }
@ -18,4 +19,6 @@ void BackgroundThreadWrapper::executeThread() {
break; break;
} }
} }
threadEnded = true;
OSMemoryBarrier();
} }

View File

@ -12,17 +12,26 @@ public:
protected: protected:
[[nodiscard]] BOOL shouldExit() const { [[nodiscard]] BOOL shouldExit() const {
return (exitThread == 1); return (exitThread);
} }
void setThreadPriority(int32_t priority) override { void setThreadPriority(int32_t priority) override {
CThread::setThreadPriority(priority); CThread::setThreadPriority(priority);
} }
void stopThread() {
exitThread = true;
}
bool hasThreadStopped() {
return threadEnded;
}
private: private:
volatile bool threadEnded = false;
volatile bool exitThread = false;
void executeThread() override; void executeThread() override;
virtual BOOL whileLoop() = 0; virtual BOOL whileLoop() = 0;
volatile int32_t exitThread = 0;
}; };