Skip to content

Commit 32ef100

Browse files
committed
fix: RunGuard not works on Qt6
Closes: yhirose#955
1 parent a6e7dfe commit 32ef100

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

3rdparty/RunGuard.hpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,34 @@ RunGuard::~RunGuard() {
5858
}
5959

6060
bool RunGuard::isAnotherRunning(quint64 *data_out) {
61-
if (sharedMem.isAttached())
61+
if (sharedMem.isAttached()) {
6262
return false;
63+
}
6364

6465
memLock.acquire();
65-
const bool isRunning = sharedMem.attach();
66-
if (isRunning) {
66+
const bool isRunning = sharedMem.create(sizeof(quint64));if (!isRunning) {
6767
if (data_out != nullptr) {
6868
memcpy(data_out, sharedMem.data(), sizeof(quint64));
6969
}
7070
sharedMem.detach();
7171
}
7272
memLock.release();
7373

74-
return isRunning;
74+
return !isRunning;
7575
}
7676

77+
7778
bool RunGuard::tryToRun(quint64 *data_in) {
7879
if (isAnotherRunning(nullptr)) // Extra check
7980
return false;
8081

8182
memLock.acquire();
82-
const bool result = sharedMem.create(sizeof(quint64));
83+
84+
bool result = sharedMem.attach();
85+
// if success attach, attach return false but the error is NoError, magic, love from qt6
86+
// qt docs: If false is returned, call error() to determine which error occurred.
87+
if (!result) if (sharedMem.error() == QSharedMemory::NoError) result = true;
88+
8389
if (result) memcpy(sharedMem.data(), data_in, sizeof(quint64));
8490
memLock.release();
8591

0 commit comments

Comments
 (0)