forked from mavlink/qgroundcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Replace RunGuard with SingleApplication"
This reverts commit d105ff6.
- Loading branch information
Showing
6 changed files
with
151 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/**************************************************************************** | ||
* | ||
* (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org> | ||
* | ||
* QGroundControl is licensed according to the terms in the file | ||
* COPYING.md in the root of the source code directory. | ||
* | ||
****************************************************************************/ | ||
|
||
#include "RunGuard.h" | ||
|
||
#include <QtCore/QCryptographicHash> | ||
|
||
namespace | ||
{ | ||
|
||
QString generateKeyHash( const QString& key, const QString& salt ) | ||
{ | ||
QByteArray data; | ||
|
||
data.append( key.toUtf8() ); | ||
data.append( salt.toUtf8() ); | ||
data = QCryptographicHash::hash( data, QCryptographicHash::Sha1 ).toHex(); | ||
|
||
return data; | ||
} | ||
|
||
} | ||
|
||
RunGuard::RunGuard( const QString& key ) | ||
: key( key ) | ||
, memLockKey( generateKeyHash( key, "_memLockKey" ) ) | ||
, sharedmemKey( generateKeyHash( key, "_sharedmemKey" ) ) | ||
, sharedMem( sharedmemKey ) | ||
, memLock( memLockKey, 1 ) | ||
{ | ||
memLock.acquire(); | ||
{ | ||
QSharedMemory fix( sharedmemKey ); // Fix for *nix: http://habrahabr.ru/post/173281/ | ||
fix.attach(); | ||
} | ||
memLock.release(); | ||
} | ||
|
||
RunGuard::~RunGuard() | ||
{ | ||
release(); | ||
} | ||
|
||
bool RunGuard::isAnotherRunning() | ||
{ | ||
if ( sharedMem.isAttached() ) | ||
return false; | ||
|
||
memLock.acquire(); | ||
const bool isRunning = sharedMem.attach(); | ||
if ( isRunning ) | ||
sharedMem.detach(); | ||
memLock.release(); | ||
|
||
return isRunning; | ||
} | ||
|
||
bool RunGuard::tryToRun() | ||
{ | ||
if ( isAnotherRunning() ) // Extra check | ||
return false; | ||
|
||
memLock.acquire(); | ||
const bool result = sharedMem.create( sizeof( quint64 ) ); | ||
memLock.release(); | ||
if ( !result ) | ||
{ | ||
release(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void RunGuard::release() | ||
{ | ||
memLock.acquire(); | ||
if ( sharedMem.isAttached() ) | ||
sharedMem.detach(); | ||
memLock.release(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/**************************************************************************** | ||
* | ||
* (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org> | ||
* | ||
* QGroundControl is licensed according to the terms in the file | ||
* COPYING.md in the root of the source code directory. | ||
* | ||
****************************************************************************/ | ||
|
||
#pragma once | ||
|
||
|
||
#include <QtCore/QObject> | ||
#include <QtCore/QSharedMemory> | ||
#include <QtCore/QSystemSemaphore> | ||
|
||
class RunGuard | ||
{ | ||
public: | ||
RunGuard( const QString& key ); | ||
~RunGuard(); | ||
|
||
bool isAnotherRunning(); | ||
bool tryToRun(); | ||
void release(); | ||
|
||
private: | ||
const QString key; | ||
const QString memLockKey; | ||
const QString sharedmemKey; | ||
|
||
QSharedMemory sharedMem; | ||
QSystemSemaphore memLock; | ||
|
||
Q_DISABLE_COPY( RunGuard ) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters