IPC/QSharedMemory: call QSystemSemaphore::setKey only once (not 3)
And we particularly want to avoid calling with Create twice. Change-Id: I12a088d1ae424825abd3fffd171d6ab1fb87221f Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
parent
2c286561bb
commit
b330f212a9
@ -227,13 +227,12 @@ void QSharedMemory::setNativeKey(const QNativeIpcKey &key)
|
|||||||
d->nativeKey = key;
|
d->nativeKey = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QSharedMemoryPrivate::initKey()
|
bool QSharedMemoryPrivate::initKey(SemaphoreAccessMode mode)
|
||||||
{
|
{
|
||||||
if (!cleanHandle())
|
if (!cleanHandle())
|
||||||
return false;
|
return false;
|
||||||
#if QT_CONFIG(systemsemaphore)
|
#if QT_CONFIG(systemsemaphore)
|
||||||
systemSemaphore.setNativeKey(QNativeIpcKey(), 1);
|
systemSemaphore.setNativeKey(semaphoreNativeKey(), 1, mode);
|
||||||
systemSemaphore.setNativeKey(semaphoreNativeKey(), 1);
|
|
||||||
if (systemSemaphore.error() != QSystemSemaphore::NoError) {
|
if (systemSemaphore.error() != QSystemSemaphore::NoError) {
|
||||||
QString function = "QSharedMemoryPrivate::initKey"_L1;
|
QString function = "QSharedMemoryPrivate::initKey"_L1;
|
||||||
errorString = QSharedMemory::tr("%1: unable to set key on lock").arg(function);
|
errorString = QSharedMemory::tr("%1: unable to set key on lock").arg(function);
|
||||||
@ -260,6 +259,8 @@ bool QSharedMemoryPrivate::initKey()
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
Q_UNUSED(mode);
|
||||||
#endif
|
#endif
|
||||||
errorString = QString();
|
errorString = QString();
|
||||||
error = QSharedMemory::NoError;
|
error = QSharedMemory::NoError;
|
||||||
@ -335,23 +336,17 @@ QNativeIpcKey QSharedMemory::nativeIpcKey() const
|
|||||||
bool QSharedMemory::create(qsizetype size, AccessMode mode)
|
bool QSharedMemory::create(qsizetype size, AccessMode mode)
|
||||||
{
|
{
|
||||||
Q_D(QSharedMemory);
|
Q_D(QSharedMemory);
|
||||||
|
QLatin1StringView function = "QSharedMemory::create"_L1;
|
||||||
|
|
||||||
if (!d->initKey())
|
#if QT_CONFIG(systemsemaphore)
|
||||||
|
if (!d->initKey(QSystemSemaphore::Create))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
#if QT_CONFIG(systemsemaphore)
|
|
||||||
#ifndef Q_OS_WIN
|
|
||||||
// Take ownership and force set initialValue because the semaphore
|
|
||||||
// might have already existed from a previous crash.
|
|
||||||
d->systemSemaphore.setKey(d->semaphoreNativeKey(), 1, QSystemSemaphore::Create);
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
QString function = "QSharedMemory::create"_L1;
|
|
||||||
#if QT_CONFIG(systemsemaphore)
|
|
||||||
QSharedMemoryLocker lock(this);
|
QSharedMemoryLocker lock(this);
|
||||||
if (!d->nativeKey.isEmpty() && !d->tryLocker(&lock, function))
|
if (!d->nativeKey.isEmpty() && !d->tryLocker(&lock, function))
|
||||||
return false;
|
return false;
|
||||||
|
#else
|
||||||
|
if (!d->initKey({}))
|
||||||
|
return false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (size <= 0) {
|
if (size <= 0) {
|
||||||
@ -410,7 +405,7 @@ bool QSharedMemory::attach(AccessMode mode)
|
|||||||
{
|
{
|
||||||
Q_D(QSharedMemory);
|
Q_D(QSharedMemory);
|
||||||
|
|
||||||
if (isAttached() || !d->initKey())
|
if (isAttached() || !d->initKey({}))
|
||||||
return false;
|
return false;
|
||||||
#if QT_CONFIG(systemsemaphore)
|
#if QT_CONFIG(systemsemaphore)
|
||||||
QSharedMemoryLocker lock(this);
|
QSharedMemoryLocker lock(this);
|
||||||
|
@ -127,8 +127,11 @@ public:
|
|||||||
QNativeIpcKey nativeKey;
|
QNativeIpcKey nativeKey;
|
||||||
QString errorString;
|
QString errorString;
|
||||||
#if QT_CONFIG(systemsemaphore)
|
#if QT_CONFIG(systemsemaphore)
|
||||||
|
using SemaphoreAccessMode = QSystemSemaphore::AccessMode;
|
||||||
QSystemSemaphore systemSemaphore{ QNativeIpcKey() };
|
QSystemSemaphore systemSemaphore{ QNativeIpcKey() };
|
||||||
bool lockedByMe = false;
|
bool lockedByMe = false;
|
||||||
|
#else
|
||||||
|
enum SemaphoreAccessMode {};
|
||||||
#endif
|
#endif
|
||||||
QSharedMemory::SharedMemoryError error = QSharedMemory::NoError;
|
QSharedMemory::SharedMemoryError error = QSharedMemory::NoError;
|
||||||
|
|
||||||
@ -141,7 +144,7 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
DefaultBackend backend;
|
DefaultBackend backend;
|
||||||
|
|
||||||
bool initKey();
|
bool initKey(SemaphoreAccessMode mode);
|
||||||
|
|
||||||
bool handle()
|
bool handle()
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user