Use lock_guard and OSMemoryBarrier

This commit is contained in:
Maschell 2022-10-06 18:54:16 +02:00
parent 174d13339f
commit f399e97aa8
2 changed files with 5 additions and 9 deletions

View File

@ -1,34 +1,31 @@
#include "BackgroundThread.hpp" #include "BackgroundThread.hpp"
#include "ftp.h" #include "ftp.h"
#include "net.h" #include "net.h"
#include <cstring>
#include <sys/socket.h> #include <sys/socket.h>
BackgroundThread *BackgroundThread::instance = nullptr; 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");
mutex.lock(); std::lock_guard<std::recursive_mutex> lock(mutex);
this->serverSocket = create_server(PORT); this->serverSocket = create_server(PORT);
DCFlushRange(&(this->serverSocket), 4); OSMemoryBarrier();
mutex.unlock();
DEBUG_FUNCTION_LINE_VERBOSE("Resume Thread"); DEBUG_FUNCTION_LINE_VERBOSE("Resume Thread");
CThread::resumeThread(); CThread::resumeThread();
} }
BackgroundThread::~BackgroundThread() { BackgroundThread::~BackgroundThread() {
DEBUG_FUNCTION_LINE("Shutting down FTP Server"); DEBUG_FUNCTION_LINE("Shutting down FTP Server");
mutex.lock(); 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);
this->serverSocket = -1; this->serverSocket = -1;
} }
mutex.unlock();
} }
BOOL BackgroundThread::whileLoop() { BOOL BackgroundThread::whileLoop() {
mutex.lock(); 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) {
@ -45,7 +42,6 @@ BOOL BackgroundThread::whileLoop() {
OSSleepTicks(OSSecondsToTicks(5)); OSSleepTicks(OSSecondsToTicks(5));
} }
} }
mutex.unlock();
OSSleepTicks(OSMillisecondsToTicks(1)); OSSleepTicks(OSMillisecondsToTicks(1));
return true; return true;
} }

View File

@ -6,7 +6,7 @@ BackgroundThreadWrapper::BackgroundThreadWrapper(int32_t priority) : CThread(CTh
BackgroundThreadWrapper::~BackgroundThreadWrapper() { BackgroundThreadWrapper::~BackgroundThreadWrapper() {
exitThread = 1; exitThread = 1;
DCFlushRange((void *) &exitThread, 4); OSMemoryBarrier();
} }
void BackgroundThreadWrapper::executeThread() { void BackgroundThreadWrapper::executeThread() {