diff options
author | Itay Grudev <itay+git2020@grudev.com> | 2020-09-08 23:15:17 +0100 |
---|---|---|
committer | Itay Grudev <itay+git2020@grudev.com> | 2020-09-08 23:15:17 +0100 |
commit | 81052d9a61a6fcaaef7dcbd72834b4b44bed3c7e (patch) | |
tree | b75a75c407c531419318d77f9dd8cf42845b73cc | |
parent | Updated code style and added inline documentation to public methods (diff) | |
download | singleapplication-81052d9a61a6fcaaef7dcbd72834b4b44bed3c7e.tar.xz |
Bug Fix: Fixed situation in which the memory block is left unlocked
-rw-r--r-- | singleapplication.cpp | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/singleapplication.cpp b/singleapplication.cpp index e70cbe8..2ba3e3d 100644 --- a/singleapplication.cpp +++ b/singleapplication.cpp @@ -76,7 +76,6 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda // Initialize the shared memory block d->memory->lock(); d->initializeMemoryBlock(); - d->memory->unlock(); } else { // Attempt to attach to the memory segment if( ! d->memory->attach() ){ @@ -85,6 +84,7 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda delete d; ::exit( EXIT_FAILURE ); } + d->memory->lock(); } auto *inst = static_cast<InstancesInfo*>( d->memory->data() ); @@ -93,24 +93,27 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda // Make sure the shared memory block is initialised and in consistent state while( true ){ - d->memory->lock(); - - if( d->blockChecksum() == inst->checksum ) break; - - if( time.elapsed() > 5000 ){ - qWarning() << "SingleApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure."; - d->initializeMemoryBlock(); - } - - d->memory->unlock(); - - // Random sleep here limits the probability of a collision between two racing apps + // If the shared memory block's checksum is valid continue + if( d->blockChecksum() == inst->checksum ) break; + + // If more than 5s have elapsed, assume the primary instance crashed and + // assume it's position + if( time.elapsed() > 5000 ){ + qWarning() << "SingleApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure."; + d->initializeMemoryBlock(); + } + + // Otherwise wait for a random period and try again. The random sleep here + // limits the probability of a collision between two racing apps and + // allows the app to initialise faster + d->memory->unlock(); #if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 ) - QThread::sleep( QRandomGenerator::global()->bounded( 8u, 18u )); + QThread::sleep( QRandomGenerator::global()->bounded( 8u, 18u )); #else - qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() ); - QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 )); + qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() ); + QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 )); #endif + d->memory->lock(); } if( inst->primary == false ){ |