-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IPC: split the backend implementations of QSystemSemaphore...
Into separate classes. This temporarily compiles QSystemSemaphorePosix on most Unix systems despite defaulting to SystemV. Change-Id: I12a088d1ae424825abd3fffd171d23191fbb6db3 Reviewed-by: Fabian Kosmale <[email protected]>
- Loading branch information
1 parent
8b92d5a
commit 75e73ea
Showing
9 changed files
with
157 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (C) 2015 Konstantin Ritt <[email protected]> | ||
// Copyright (C) 2016 The Qt Company Ltd. | ||
// Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Tobias Koenig <[email protected]> | ||
// Copyright (C) 2022 Intel Corporation. | ||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | ||
|
||
#include "qsystemsemaphore.h" | ||
|
@@ -10,9 +11,7 @@ | |
#include <qfile.h> | ||
#include <qcoreapplication.h> | ||
|
||
#ifdef QT_POSIX_IPC | ||
|
||
#if QT_CONFIG(systemsemaphore) | ||
#if QT_CONFIG(posix_sem) | ||
|
||
#include <sys/types.h> | ||
#include <fcntl.h> | ||
|
@@ -30,29 +29,30 @@ QT_BEGIN_NAMESPACE | |
|
||
using namespace Qt::StringLiterals; | ||
|
||
bool QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) | ||
bool QSystemSemaphorePosix::handle(QSystemSemaphorePrivate *self, QSystemSemaphore::AccessMode mode) | ||
{ | ||
if (semaphore != SEM_FAILED) | ||
return true; // we already have a semaphore | ||
|
||
if (fileName.isEmpty()) { | ||
errorString = QSystemSemaphore::tr("%1: key is empty").arg("QSystemSemaphore::handle"_L1); | ||
error = QSystemSemaphore::KeyError; | ||
if (self->fileName.isEmpty()) { | ||
self->setError(QSystemSemaphore::KeyError, | ||
QSystemSemaphore::tr("%1: key is empty") | ||
.arg("QSystemSemaphore::handle"_L1)); | ||
return false; | ||
} | ||
|
||
const QByteArray semName = QFile::encodeName(fileName); | ||
const QByteArray semName = QFile::encodeName(self->fileName); | ||
|
||
// Always try with O_EXCL so we know whether we created the semaphore. | ||
int oflag = O_CREAT | O_EXCL; | ||
for (int tryNum = 0, maxTries = 1; tryNum < maxTries; ++tryNum) { | ||
do { | ||
semaphore = ::sem_open(semName.constData(), oflag, 0600, initialValue); | ||
semaphore = ::sem_open(semName.constData(), oflag, 0600, self->initialValue); | ||
} while (semaphore == SEM_FAILED && errno == EINTR); | ||
if (semaphore == SEM_FAILED && errno == EEXIST) { | ||
if (mode == QSystemSemaphore::Create) { | ||
if (::sem_unlink(semName.constData()) == -1 && errno != ENOENT) { | ||
setErrorString("QSystemSemaphore::handle (sem_unlink)"_L1); | ||
self->setUnixErrorString("QSystemSemaphore::handle (sem_unlink)"_L1); | ||
return false; | ||
} | ||
// Race condition: the semaphore might be recreated before | ||
|
@@ -70,7 +70,7 @@ bool QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) | |
} | ||
|
||
if (semaphore == SEM_FAILED) { | ||
setErrorString("QSystemSemaphore::handle"_L1); | ||
self->setUnixErrorString("QSystemSemaphore::handle"_L1); | ||
return false; | ||
} | ||
|
||
|
@@ -79,11 +79,11 @@ bool QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode) | |
return true; | ||
} | ||
|
||
void QSystemSemaphorePrivate::cleanHandle() | ||
void QSystemSemaphorePosix::cleanHandle(QSystemSemaphorePrivate *self) | ||
{ | ||
if (semaphore != SEM_FAILED) { | ||
if (::sem_close(semaphore) == -1) { | ||
setErrorString("QSystemSemaphore::cleanHandle (sem_close)"_L1); | ||
self->setUnixErrorString("QSystemSemaphore::cleanHandle (sem_close)"_L1); | ||
#if defined QSYSTEMSEMAPHORE_DEBUG | ||
qDebug("QSystemSemaphore::cleanHandle sem_close failed."); | ||
#endif | ||
|
@@ -92,28 +92,29 @@ void QSystemSemaphorePrivate::cleanHandle() | |
} | ||
|
||
if (createdSemaphore) { | ||
if (::sem_unlink(QFile::encodeName(fileName).constData()) == -1 && errno != ENOENT) { | ||
setErrorString("QSystemSemaphore::cleanHandle (sem_unlink)"_L1); | ||
if (::sem_unlink(QFile::encodeName(self->fileName).constData()) == -1 | ||
&& errno != ENOENT) { | ||
self->setUnixErrorString("QSystemSemaphore::cleanHandle (sem_unlink)"_L1); | ||
#if defined QSYSTEMSEMAPHORE_DEBUG | ||
qDebug("QSystemSemaphore::cleanHandle sem_unlink failed."); | ||
qDebug("QSystemSemaphorePosix::cleanHandle sem_unlink failed."); | ||
#endif | ||
} | ||
createdSemaphore = false; | ||
} | ||
} | ||
|
||
bool QSystemSemaphorePrivate::modifySemaphore(int count) | ||
bool QSystemSemaphorePosix::modifySemaphore(QSystemSemaphorePrivate *self, int count) | ||
{ | ||
if (!handle()) | ||
if (!handle(self, QSystemSemaphore::Open)) | ||
return false; | ||
|
||
if (count > 0) { | ||
int cnt = count; | ||
do { | ||
if (::sem_post(semaphore) == -1) { | ||
setErrorString("QSystemSemaphore::modifySemaphore (sem_post)"_L1); | ||
self->setUnixErrorString("QSystemSemaphore::modifySemaphore (sem_post)"_L1); | ||
#if defined QSYSTEMSEMAPHORE_DEBUG | ||
qDebug("QSystemSemaphore::modify sem_post failed %d %d", count, errno); | ||
qDebug("QSystemSemaphorePosix::modify sem_post failed %d %d", count, errno); | ||
#endif | ||
// rollback changes to preserve the SysV semaphore behavior | ||
for ( ; cnt < count; ++cnt) { | ||
|
@@ -131,22 +132,20 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count) | |
// If the semaphore was removed be nice and create it and then modifySemaphore again | ||
if (errno == EINVAL || errno == EIDRM) { | ||
semaphore = SEM_FAILED; | ||
return modifySemaphore(count); | ||
return modifySemaphore(self, count); | ||
} | ||
setErrorString("QSystemSemaphore::modifySemaphore (sem_wait)"_L1); | ||
self->setUnixErrorString("QSystemSemaphore::modifySemaphore (sem_wait)"_L1); | ||
#if defined QSYSTEMSEMAPHORE_DEBUG | ||
qDebug("QSystemSemaphore::modify sem_wait failed %d %d", count, errno); | ||
qDebug("QSystemSemaphorePosix::modify sem_wait failed %d %d", count, errno); | ||
#endif | ||
return false; | ||
} | ||
} | ||
|
||
clearError(); | ||
self->clearError(); | ||
return true; | ||
} | ||
|
||
QT_END_NAMESPACE | ||
|
||
#endif // QT_CONFIG(systemsemaphore) | ||
|
||
#endif // QT_POSIX_IPC | ||
#endif // QT_CONFIG(posix_sem) |
Oops, something went wrong.