Skip to content

Commit

Permalink
IPC/QSharedMemory: call QSystemSemaphore::setKey only once (not 3)
Browse files Browse the repository at this point in the history
And we particularly want to avoid calling with Create twice.

Change-Id: I12a088d1ae424825abd3fffd171d6ab1fb87221f
Reviewed-by: Mårten Nordheim <[email protected]>
  • Loading branch information
thiagomacieira committed Jan 22, 2023
1 parent 2c28656 commit b330f21
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
27 changes: 11 additions & 16 deletions src/corelib/ipc/qsharedmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,12 @@ void QSharedMemory::setNativeKey(const QNativeIpcKey &key)
d->nativeKey = key;
}

bool QSharedMemoryPrivate::initKey()
bool QSharedMemoryPrivate::initKey(SemaphoreAccessMode mode)
{
if (!cleanHandle())
return false;
#if QT_CONFIG(systemsemaphore)
systemSemaphore.setNativeKey(QNativeIpcKey(), 1);
systemSemaphore.setNativeKey(semaphoreNativeKey(), 1);
systemSemaphore.setNativeKey(semaphoreNativeKey(), 1, mode);
if (systemSemaphore.error() != QSystemSemaphore::NoError) {
QString function = "QSharedMemoryPrivate::initKey"_L1;
errorString = QSharedMemory::tr("%1: unable to set key on lock").arg(function);
Expand All @@ -260,6 +259,8 @@ bool QSharedMemoryPrivate::initKey()
}
return false;
}
#else
Q_UNUSED(mode);
#endif
errorString = QString();
error = QSharedMemory::NoError;
Expand Down Expand Up @@ -335,23 +336,17 @@ QNativeIpcKey QSharedMemory::nativeIpcKey() const
bool QSharedMemory::create(qsizetype size, AccessMode mode)
{
Q_D(QSharedMemory);
QLatin1StringView function = "QSharedMemory::create"_L1;

if (!d->initKey())
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)
if (!d->initKey(QSystemSemaphore::Create))
return false;
QSharedMemoryLocker lock(this);
if (!d->nativeKey.isEmpty() && !d->tryLocker(&lock, function))
return false;
#else
if (!d->initKey({}))
return false;
#endif

if (size <= 0) {
Expand Down Expand Up @@ -410,7 +405,7 @@ bool QSharedMemory::attach(AccessMode mode)
{
Q_D(QSharedMemory);

if (isAttached() || !d->initKey())
if (isAttached() || !d->initKey({}))
return false;
#if QT_CONFIG(systemsemaphore)
QSharedMemoryLocker lock(this);
Expand Down
5 changes: 4 additions & 1 deletion src/corelib/ipc/qsharedmemory_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,11 @@ class Q_AUTOTEST_EXPORT QSharedMemoryPrivate : public QObjectPrivate
QNativeIpcKey nativeKey;
QString errorString;
#if QT_CONFIG(systemsemaphore)
using SemaphoreAccessMode = QSystemSemaphore::AccessMode;
QSystemSemaphore systemSemaphore{ QNativeIpcKey() };
bool lockedByMe = false;
#else
enum SemaphoreAccessMode {};
#endif
QSharedMemory::SharedMemoryError error = QSharedMemory::NoError;

Expand All @@ -141,7 +144,7 @@ class Q_AUTOTEST_EXPORT QSharedMemoryPrivate : public QObjectPrivate
#endif
DefaultBackend backend;

bool initKey();
bool initKey(SemaphoreAccessMode mode);

bool handle()
{
Expand Down

0 comments on commit b330f21

Please sign in to comment.