Skip to content

Commit

Permalink
fix: RunGuard not works on Qt6
Browse files Browse the repository at this point in the history
  • Loading branch information
purofle committed Oct 31, 2023
1 parent a6e7dfe commit 32ef100
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions 3rdparty/RunGuard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,34 @@ RunGuard::~RunGuard() {
}

bool RunGuard::isAnotherRunning(quint64 *data_out) {
if (sharedMem.isAttached())
if (sharedMem.isAttached()) {
return false;
}

memLock.acquire();
const bool isRunning = sharedMem.attach();
if (isRunning) {
const bool isRunning = sharedMem.create(sizeof(quint64));if (!isRunning) {
if (data_out != nullptr) {
memcpy(data_out, sharedMem.data(), sizeof(quint64));
}
sharedMem.detach();
}
memLock.release();

return isRunning;
return !isRunning;
}


bool RunGuard::tryToRun(quint64 *data_in) {
if (isAnotherRunning(nullptr)) // Extra check
return false;

memLock.acquire();
const bool result = sharedMem.create(sizeof(quint64));

bool result = sharedMem.attach();
// if success attach, attach return false but the error is NoError, magic, love from qt6
// qt docs: If false is returned, call error() to determine which error occurred.
if (!result) if (sharedMem.error() == QSharedMemory::NoError) result = true;

if (result) memcpy(sharedMem.data(), data_in, sizeof(quint64));
memLock.release();

Expand Down

0 comments on commit 32ef100

Please sign in to comment.