mirror of
https://github.com/itay-grudev/SingleApplication.git
synced 2025-01-15 08:52:08 +08:00
Bug Fix: Fixed situation in which the memory block is left unlocked
This commit is contained in:
parent
a136c734c9
commit
81052d9a61
@ -76,7 +76,6 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
|
|||||||
// Initialize the shared memory block
|
// Initialize the shared memory block
|
||||||
d->memory->lock();
|
d->memory->lock();
|
||||||
d->initializeMemoryBlock();
|
d->initializeMemoryBlock();
|
||||||
d->memory->unlock();
|
|
||||||
} else {
|
} else {
|
||||||
// Attempt to attach to the memory segment
|
// Attempt to attach to the memory segment
|
||||||
if( ! d->memory->attach() ){
|
if( ! d->memory->attach() ){
|
||||||
@ -85,6 +84,7 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
|
|||||||
delete d;
|
delete d;
|
||||||
::exit( EXIT_FAILURE );
|
::exit( EXIT_FAILURE );
|
||||||
}
|
}
|
||||||
|
d->memory->lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto *inst = static_cast<InstancesInfo*>( d->memory->data() );
|
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
|
// Make sure the shared memory block is initialised and in consistent state
|
||||||
while( true ){
|
while( true ){
|
||||||
d->memory->lock();
|
// If the shared memory block's checksum is valid continue
|
||||||
|
if( d->blockChecksum() == inst->checksum ) break;
|
||||||
|
|
||||||
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();
|
||||||
|
}
|
||||||
|
|
||||||
if( time.elapsed() > 5000 ){
|
// Otherwise wait for a random period and try again. The random sleep here
|
||||||
qWarning() << "SingleApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure.";
|
// limits the probability of a collision between two racing apps and
|
||||||
d->initializeMemoryBlock();
|
// allows the app to initialise faster
|
||||||
}
|
d->memory->unlock();
|
||||||
|
|
||||||
d->memory->unlock();
|
|
||||||
|
|
||||||
// Random sleep here limits the probability of a collision between two racing apps
|
|
||||||
#if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 )
|
#if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 )
|
||||||
QThread::sleep( QRandomGenerator::global()->bounded( 8u, 18u ));
|
QThread::sleep( QRandomGenerator::global()->bounded( 8u, 18u ));
|
||||||
#else
|
#else
|
||||||
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
|
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
|
||||||
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ));
|
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ));
|
||||||
#endif
|
#endif
|
||||||
|
d->memory->lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
if( inst->primary == false ){
|
if( inst->primary == false ){
|
||||||
|
Loading…
Reference in New Issue
Block a user