Fix exiting

This commit is contained in:
Maschell 2022-10-06 23:16:31 +02:00
parent 258e93d01e
commit 6e2ea75917
5 changed files with 11 additions and 35 deletions

View File

@ -7,7 +7,6 @@ BackgroundThread *BackgroundThread::instance = nullptr;
BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread::getPriority()) { BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread::getPriority()) {
DEBUG_FUNCTION_LINE("Start FTP Server"); DEBUG_FUNCTION_LINE("Start FTP Server");
std::lock_guard<std::recursive_mutex> lock(mutex);
this->serverSocket = create_server(PORT); this->serverSocket = create_server(PORT);
OSMemoryBarrier(); OSMemoryBarrier();
DEBUG_FUNCTION_LINE_VERBOSE("Resume Thread"); DEBUG_FUNCTION_LINE_VERBOSE("Resume Thread");
@ -16,7 +15,6 @@ BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread:
BackgroundThread::~BackgroundThread() { BackgroundThread::~BackgroundThread() {
DEBUG_FUNCTION_LINE("Shutting down FTP Server"); DEBUG_FUNCTION_LINE("Shutting down FTP Server");
std::lock_guard<std::recursive_mutex> lock(mutex);
if (this->serverSocket >= 0) { if (this->serverSocket >= 0) {
cleanup_ftp(); cleanup_ftp();
network_close(this->serverSocket); network_close(this->serverSocket);
@ -25,15 +23,14 @@ BackgroundThread::~BackgroundThread() {
} }
BOOL BackgroundThread::whileLoop() { BOOL BackgroundThread::whileLoop() {
std::lock_guard<std::recursive_mutex> lock(mutex);
if (this->serverSocket >= 0) { if (this->serverSocket >= 0) {
network_down = process_ftp_events(this->serverSocket); network_down = process_ftp_events(this->serverSocket);
if (network_down) { if (network_down) {
DEBUG_FUNCTION_LINE_VERBOSE("Network is down %d", this->serverSocket); DEBUG_FUNCTION_LINE_WARN("Network is down");
cleanup_ftp(); cleanup_ftp();
network_close(this->serverSocket); network_close(this->serverSocket);
this->serverSocket = -1; this->serverSocket = -1;
DCFlushRange(&(this->serverSocket), 4); OSMemoryBarrier();
} }
} else { } else {
this->serverSocket = create_server(PORT); this->serverSocket = create_server(PORT);

View File

@ -11,7 +11,7 @@ public:
static BackgroundThread *getInstance() { static BackgroundThread *getInstance() {
if (instance == nullptr) { if (instance == nullptr) {
instance = new BackgroundThread(); instance = new BackgroundThread();
DCFlushRange(&instance, 4); OSMemoryBarrier();
} }
return instance; return instance;
} }
@ -20,15 +20,9 @@ public:
if (instance != nullptr) { if (instance != nullptr) {
delete instance; delete instance;
instance = nullptr; instance = nullptr;
DCFlushRange(&instance, 4); OSMemoryBarrier();
} }
} }
static void destroyInstance(bool forceKill) {
if (instance != nullptr) {
instance->skipJoin = true;
}
destroyInstance();
}
BackgroundThread(); BackgroundThread();

View File

@ -55,10 +55,6 @@ ON_APPLICATION_START() {
nn::ac::GetAssignedAddress(&hostIpAddress); nn::ac::GetAssignedAddress(&hostIpAddress);
initLogging(); 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) { if (gFTPServerEnabled) {
startServer(); startServer();
} }

View File

@ -19,8 +19,6 @@ protected:
CThread::setThreadPriority(priority); CThread::setThreadPriority(priority);
} }
std::recursive_mutex mutex;
private: private:
void executeThread() override; void executeThread() override;

View File

@ -101,22 +101,15 @@ public:
//! Shutdown thread //! Shutdown thread
virtual void shutdownThread() { virtual void shutdownThread() {
if (skipJoin) { //! wait for thread to finish
DEBUG_FUNCTION_LINE_WARN("Skip joining the thread \"%s\", it's not running.", pThreadName.c_str()); if (pThread && !(iAttributes & eAttributeDetach)) {
} else { if (isThreadSuspended()) {
//! wait for thread to finish resumeThread();
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());
}
} }
OSJoinThread(pThread, nullptr);
} }
//! free the thread stack buffer //! free the thread stack buffer
if (pThreadStack) { if (pThreadStack) {
free(pThreadStack); free(pThreadStack);
@ -139,8 +132,6 @@ public:
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) {
//! After call to start() continue with the internal function //! After call to start() continue with the internal function