summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/kernel/qsystemsemaphore_unix.cpp2
-rw-r--r--src/corelib/kernel/qsystemsemaphore_win.cpp8
-rw-r--r--tests/auto/qtipc/qsystemsemaphore/tst_qsystemsemaphore.cpp25
3 files changed, 32 insertions, 3 deletions
diff --git a/src/corelib/kernel/qsystemsemaphore_unix.cpp b/src/corelib/kernel/qsystemsemaphore_unix.cpp
index 07e3618..d6c6c37 100644
--- a/src/corelib/kernel/qsystemsemaphore_unix.cpp
+++ b/src/corelib/kernel/qsystemsemaphore_unix.cpp
@@ -145,10 +145,10 @@ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)
// Get semaphore
semaphore = semget(unix_key, 1, 0666 | IPC_CREAT | IPC_EXCL);
if (-1 == semaphore) {
+ setErrorString(QLatin1String("QSystemSemaphore::handle"));
if (errno == EEXIST)
semaphore = semget(unix_key, 1, 0666 | IPC_CREAT);
if (-1 == semaphore) {
- setErrorString(QLatin1String("QSystemSemaphore::handle"));
cleanHandle();
return -1;
}
diff --git a/src/corelib/kernel/qsystemsemaphore_win.cpp b/src/corelib/kernel/qsystemsemaphore_win.cpp
index 96a47f5..74f253a 100644
--- a/src/corelib/kernel/qsystemsemaphore_win.cpp
+++ b/src/corelib/kernel/qsystemsemaphore_win.cpp
@@ -69,6 +69,10 @@ void QSystemSemaphorePrivate::setErrorString(const QString &function)
error = QSystemSemaphore::PermissionDenied;
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function);
break;
+ case ERROR_ALREADY_EXISTS:
+ error = QSystemSemaphore::AlreadyExists;
+ errorString = QCoreApplication::translate("QSystemSemaphore", "%1: already exists").arg(function);
+ break;
default:
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(windowsError);
error = QSystemSemaphore::UnknownError;
@@ -88,8 +92,8 @@ HANDLE QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode)
if (semaphore == 0) {
QString safeName = makeKeyFileName();
semaphore = CreateSemaphore(0, initialValue, MAXLONG, (wchar_t*)safeName.utf16());
- if (semaphore == NULL)
- setErrorString(QLatin1String("QSystemSemaphore::handle"));
+ // If the semaphore exists then the handle is still valid but there is still an error
+ setErrorString(QLatin1String("QSystemSemaphore::handle"));
}
return semaphore;
diff --git a/tests/auto/qtipc/qsystemsemaphore/tst_qsystemsemaphore.cpp b/tests/auto/qtipc/qsystemsemaphore/tst_qsystemsemaphore.cpp
index eb82fd4..0dc9c99 100644
--- a/tests/auto/qtipc/qsystemsemaphore/tst_qsystemsemaphore.cpp
+++ b/tests/auto/qtipc/qsystemsemaphore/tst_qsystemsemaphore.cpp
@@ -76,6 +76,8 @@ private slots:
void undo();
void initialValue();
+ void exists();
+
private:
QSystemSemaphore *existingLock;
@@ -297,6 +299,29 @@ void tst_QSystemSemaphore::initialValue()
release.waitForFinished(LACKYWAITTIME);
QVERIFY(acquire.state()== QProcess::NotRunning);
}
+
+void tst_QSystemSemaphore::exists()
+{
+ QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
+ QVERIFY(sem.error() == QSystemSemaphore::NoError);
+ QVERIFY(sem.acquire());
+ QVERIFY(sem.error() == QSystemSemaphore::NoError);
+
+ {
+ QSystemSemaphore dupSem("store", 1, QSystemSemaphore::Create);
+ QVERIFY(dupSem.error() == QSystemSemaphore::AlreadyExists);
+ }
+#ifndef Q_OS_UNIX
+ // The rest of the test does not make sense on Unix because open will
+ // actually succeed anyway (see QSystemSemaphore docs)
+ QSystemSemaphore anotherSem("store", 1, QSystemSemaphore::Open);
+ QVERIFY(anotherSem.error() == QSystemSemaphore::AlreadyExists);
+ QVERIFY(sem.release());
+ QVERIFY(anotherSem.acquire());
+ QVERIFY(anotherSem.release());
+#endif
+}
+
QTEST_MAIN(tst_QSystemSemaphore)
#include "tst_qsystemsemaphore.moc"