Just Intonation  Version 1.3.1 (19)
Explore key-independent dynamically adapting tuning in just intonation
RunGuard Class Reference

Class for checking whether the application is already running. More...

#include <runguard.h>

Inheritance diagram for RunGuard:
Inheritance graph
Collaboration diagram for RunGuard:
Collaboration graph

Public Member Functions

 RunGuard (const QString &mKey)
 Constructor for a runguard. More...
 
 ~RunGuard ()
 RunGuard destructor. More...
 
bool anotherInstanceIsRunning (bool showMessage=true)
 Check whether another instance of the application is running. More...
 

Private Member Functions

QString generateKeyHash (const QString &mKey, const QString &salt)
 Generate hash string for a given keys. More...
 
void release ()
 Release the running application. More...
 

Private Attributes

const QString mKey
 Name assigned to the instance. More...
 
const QString mMemLockKey
 Key labelling the semaphore lock. More...
 
const QString mSharedMemKey
 Key labelling the shared memory. More...
 
QSharedMemory mSharedMem
 Shared memory labelled by a given key. More...
 
QSystemSemaphore mMemLock
 Lock used when shared memory is accessed. More...
 

Detailed Description

Class for checking whether the application is already running.

Creating an instance of this class prevents the system from starting serveral instances of the application.

Internally the instances communicate via a QSharedMemory labelled by a given key. The access to this memory is mutexted by a QSystemSemaphore.

This mechanism is not needed on tablets and smartphones, where the operating system itself makes sure that only one instance can be started. Therefore, the essential code of this class is only compiled if the Q_OS_DESKTOP label is set in the case of a desktop application.

The runguard is based on a similar one developed by Christoph Wick in the Entropy Piano Tuner.

See also
Q_OS_DESKTOP

Definition at line 52 of file runguard.h.

Constructor & Destructor Documentation

RunGuard::RunGuard ( const QString &  key)

Constructor for a runguard.

The constructor creates a flag in the shared memory indicating that the application is running. This allows other instances to detect an already running application.

Parameters
key: String naming the run guard (e.g. myapp_runguard)

Definition at line 48 of file runguard.cpp.

Here is the call graph for this function:

RunGuard::~RunGuard ( )

RunGuard destructor.

The destructor releases the blocking label. Since the destructor is is called at the end of the main() function, this ensures that the label is automatically removed as soon as the application stops.

Definition at line 80 of file runguard.cpp.

Here is the call graph for this function:

Member Function Documentation

bool RunGuard::anotherInstanceIsRunning ( bool  showMessage = true)

Check whether another instance of the application is running.

This function first checks whether the own instance is already running. Then it checks whether another application has already created the shared memory, indicating that another instance of the same applicaiton is running.

Returns
Boolean indicating whether another instance of the same application is running.

Definition at line 148 of file runguard.cpp.

QString RunGuard::generateKeyHash ( const QString &  key,
const QString &  salt 
)
private

Generate hash string for a given keys.

This function converts a key (entropypianotuner_runguard) and a second key extenstion (e.g._memLockKey) into a hash string.

Parameters
key: String holding the key
salt: String holding the key extension
Returns
: String containg the hash

Definition at line 121 of file runguard.cpp.

void RunGuard::release ( )
private

Release the running application.

This function detaches the application from the shared memory, removing the blocking label. It has to be called when the application terminates.

See also
~RunGuard()

Definition at line 99 of file runguard.cpp.

Member Data Documentation

const QString RunGuard::mKey
private

Name assigned to the instance.

Definition at line 65 of file runguard.h.

QSystemSemaphore RunGuard::mMemLock
private

Lock used when shared memory is accessed.

Definition at line 69 of file runguard.h.

const QString RunGuard::mMemLockKey
private

Key labelling the semaphore lock.

Definition at line 66 of file runguard.h.

QSharedMemory RunGuard::mSharedMem
private

Shared memory labelled by a given key.

Definition at line 68 of file runguard.h.

const QString RunGuard::mSharedMemKey
private

Key labelling the shared memory.

Definition at line 67 of file runguard.h.


The documentation for this class was generated from the following files: