34 #include <QCryptographicHash>
38 QString generateKeyHash(
const QString &key,
const QString &salt)
42 data.append(key.toUtf8());
43 data.append(salt.toUtf8());
44 data = QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex();
50 RunGuard::RunGuard(
const QString &key)
52 , memLockKey(generateKeyHash(key,
"_memLockKey"))
53 , sharedmemKey(generateKeyHash(key,
"_sharedmemKey"))
54 , sharedMem(sharedmemKey)
55 , memLock(memLockKey, 1)
59 QSharedMemory fix(sharedmemKey);
76 bool RunGuard::isAnotherRunning()
78 if (sharedMem.isAttached())
82 const bool isRunning = sharedMem.attach();
84 *
static_cast<quint64 *
>(sharedMem.data()) += 1;
94 if (isAnotherRunning())
98 const bool result = sharedMem.create(
sizeof(quint64));
100 *
static_cast<quint64 *
>(sharedMem.data()) = 0;
114 if (!sharedMem.isAttached() && sharedMem.attach(QSharedMemory::ReadOnly)) {
115 ret = *
static_cast<quint64 *
>(sharedMem.data());
118 ret = *
static_cast<quint64 *
>(sharedMem.data());
124 void RunGuard::release()
127 if (sharedMem.isAttached())
bool tryToRun()
Attempt to run and fail if another instance is running.
quint64 secondaryAttempts()
Number of secondary instances attempted to start.
static RunGuard & instance(const QString &key)
Get instance of this singleton.