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()) {
DEBUG_FUNCTION_LINE("Start FTP Server");
std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> lock(mutex);
if (this->serverSocket >= 0) {
cleanup_ftp();
network_close(this->serverSocket);
@ -25,15 +23,14 @@ BackgroundThread::~BackgroundThread() {
}
BOOL BackgroundThread::whileLoop() {
std::lock_guard<std::recursive_mutex> 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);

View File

@ -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();

View File

@ -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();
}

View File

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

View File

@ -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