summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRitt Konstantin <ritt.ks@gmail.com>2011-06-06 14:56:46 +0200
committerHarald Fernengel <harald.fernengel@nokia.com>2011-06-06 14:56:46 +0200
commit7b60b6621e8a3fce144b8fe5edb07bcf9cc331d7 (patch)
tree8c32315648462f65c1c3855d4117afe7ac66e6a5
parent8eaf39f705295f3072e9243f55063e92af40489c (diff)
code clean-up for QSystemSemaphore
Merge-request: 1248 Reviewed-by: Harald Fernengel <harald.fernengel@nokia.com>
-rw-r--r--src/corelib/kernel/qsystemsemaphore_p.h8
-rw-r--r--src/corelib/kernel/qsystemsemaphore_symbian.cpp49
-rw-r--r--src/corelib/kernel/qsystemsemaphore_unix.cpp52
-rw-r--r--src/corelib/kernel/qsystemsemaphore_win.cpp17
4 files changed, 65 insertions, 61 deletions
diff --git a/src/corelib/kernel/qsystemsemaphore_p.h b/src/corelib/kernel/qsystemsemaphore_p.h
index d4e86e8866..3e5f737667 100644
--- a/src/corelib/kernel/qsystemsemaphore_p.h
+++ b/src/corelib/kernel/qsystemsemaphore_p.h
@@ -59,7 +59,7 @@
#include "qsharedmemory_p.h"
#ifndef Q_OS_WINCE
-# include <sys/types.h>
+# include <sys/types.h>
#endif
#ifdef Q_OS_SYMBIAN
@@ -70,11 +70,10 @@ QT_BEGIN_NAMESPACE
class QSystemSemaphorePrivate
{
-
public:
QSystemSemaphorePrivate();
- QString makeKeyFileName()
+ inline QString makeKeyFileName() const
{
return QSharedMemoryPrivate::makePlatformSafeKey(key, QLatin1String("qipc_systemsem_"));
}
@@ -101,10 +100,10 @@ public:
#elif defined(Q_OS_SYMBIAN)
RSemaphore semaphore;
#else
+ key_t unix_key;
int semaphore;
bool createdFile;
bool createdSemaphore;
- key_t unix_key;
#endif
QString errorString;
QSystemSemaphore::SystemSemaphoreError error;
@@ -115,4 +114,3 @@ QT_END_NAMESPACE
#endif // QT_NO_SYSTEMSEMAPHORE
#endif // QSYSTEMSEMAPHORE_P_H
-
diff --git a/src/corelib/kernel/qsystemsemaphore_symbian.cpp b/src/corelib/kernel/qsystemsemaphore_symbian.cpp
index 96c19afcfc..bb60df7819 100644
--- a/src/corelib/kernel/qsystemsemaphore_symbian.cpp
+++ b/src/corelib/kernel/qsystemsemaphore_symbian.cpp
@@ -46,29 +46,32 @@
#include "qcore_symbian_p.h"
#include <e32cmn.h>
-QT_BEGIN_NAMESPACE
#ifndef QT_NO_SYSTEMSEMAPHORE
+//#define QSYSTEMSEMAPHORE_DEBUG
+
+QT_BEGIN_NAMESPACE
+
QSystemSemaphorePrivate::QSystemSemaphorePrivate() :
- error(QSystemSemaphore::NoError)
+ error(QSystemSemaphore::NoError)
{
}
void QSystemSemaphorePrivate::setErrorString(const QString &function, int err)
{
- if (err == KErrNone){
+ if (err == KErrNone)
return;
- }
+
switch(err){
case KErrAlreadyExists:
errorString = QCoreApplication::tr("%1: already exists", "QSystemSemaphore").arg(function);
error = QSystemSemaphore::AlreadyExists;
- break;
+ break;
case KErrNotFound:
errorString = QCoreApplication::tr("%1: does not exist", "QSystemSemaphore").arg(function);
error = QSystemSemaphore::NotFound;
- break;
+ break;
case KErrNoMemory:
case KErrInUse:
errorString = QCoreApplication::tr("%1: out of resources", "QSystemSemaphore").arg(function);
@@ -77,22 +80,21 @@ void QSystemSemaphorePrivate::setErrorString(const QString &function, int err)
case KErrPermissionDenied:
errorString = QCoreApplication::tr("%1: permission denied", "QSystemSemaphore").arg(function);
error = QSystemSemaphore::PermissionDenied;
- break;
-default:
- errorString = QCoreApplication::tr("%1: unknown error %2", "QSystemSemaphore").arg(function).arg(err);
- error = QSystemSemaphore::UnknownError;
- }
-
-#if defined QSYSTEMSEMAPHORE_DEBUG
+ break;
+ default:
+ errorString = QCoreApplication::tr("%1: unknown error %2", "QSystemSemaphore").arg(function).arg(err);
+ error = QSystemSemaphore::UnknownError;
+#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << errorString << "key" << key;
#endif
+ break;
+ }
}
int QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode)
{
- if (semaphore.Handle()) {
+ if (semaphore.Handle())
return semaphore.Handle();
- }
// don't allow making handles on empty keys
if (key.isEmpty())
@@ -106,12 +108,13 @@ int QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode)
while (err != KErrNoMemory && err != KErrNone && tryCount-- >= 0) {
err = semaphore.CreateGlobal(name, initialValue, EOwnerProcess);
if (err != KErrNoMemory && err != KErrNone)
- err = semaphore.OpenGlobal(name,EOwnerProcess);
+ err = semaphore.OpenGlobal(name, EOwnerProcess);
}
- if (err){
- setErrorString(QLatin1String("QSystemSemaphore::handle"),err);
+ if (err) {
+ setErrorString(QLatin1String("QSystemSemaphore::handle"), err);
return 0;
}
+
return semaphore.Handle();
}
@@ -125,14 +128,14 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count)
if (0 == handle())
return false;
- if (count > 0) {
+ if (count > 0)
semaphore.Signal(count);
- } else {
+ else
semaphore.Wait();
- }
+
return true;
}
-#endif //QT_NO_SYSTEMSEMAPHORE
-
QT_END_NAMESPACE
+
+#endif // QT_NO_SYSTEMSEMAPHORE
diff --git a/src/corelib/kernel/qsystemsemaphore_unix.cpp b/src/corelib/kernel/qsystemsemaphore_unix.cpp
index 5e533e7f0e..704afaf1e4 100644
--- a/src/corelib/kernel/qsystemsemaphore_unix.cpp
+++ b/src/corelib/kernel/qsystemsemaphore_unix.cpp
@@ -42,9 +42,9 @@
#include "qsystemsemaphore.h"
#include "qsystemsemaphore_p.h"
+#include <qcoreapplication.h>
#include <qdebug.h>
#include <qfile.h>
-#include <qcoreapplication.h>
#ifndef QT_NO_SYSTEMSEMAPHORE
@@ -62,11 +62,13 @@
#define EIDRM EINVAL
#endif
+//#define QSYSTEMSEMAPHORE_DEBUG
+
QT_BEGIN_NAMESPACE
QSystemSemaphorePrivate::QSystemSemaphorePrivate() :
- semaphore(-1), createdFile(false),
- createdSemaphore(false), unix_key(-1), error(QSystemSemaphore::NoError)
+ unix_key(-1), semaphore(-1), createdFile(false),
+ createdSemaphore(false), error(QSystemSemaphore::NoError)
{
}
@@ -95,33 +97,33 @@ void QSystemSemaphorePrivate::setErrorString(const QString &function)
default:
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(errno);
error = QSystemSemaphore::UnknownError;
-#if defined QSYSTEMSEMAPHORE_DEBUG
+#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << errorString << "key" << key << "errno" << errno << EINVAL;
#endif
+ break;
}
}
/*!
\internal
- Setup unix_key
- */
+ Initialise the semaphore
+*/
key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)
{
- if (key.isEmpty()){
- errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:"));
+ if (-1 != unix_key)
+ return unix_key;
+
+ if (key.isEmpty()) {
+ errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
error = QSystemSemaphore::KeyError;
return -1;
}
// ftok requires that an actual file exists somewhere
- if (-1 != unix_key)
- return unix_key;
-
- // Create the file needed for ftok
int built = QSharedMemoryPrivate::createUnixKeyFile(fileName);
if (-1 == built) {
- errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:"));
+ errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
error = QSystemSemaphore::KeyError;
return -1;
}
@@ -130,7 +132,7 @@ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)
// Get the unix key for the created file
unix_key = ftok(QFile::encodeName(fileName).constData(), 'Q');
if (-1 == unix_key) {
- errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle:"));
+ errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
error = QSystemSemaphore::KeyError;
return -1;
}
@@ -145,17 +147,16 @@ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)
cleanHandle();
return -1;
}
+ if (mode == QSystemSemaphore::Create) {
+ createdSemaphore = true;
+ createdFile = true;
+ }
} else {
createdSemaphore = true;
// Force cleanup of file, it is possible that it can be left over from a crash
createdFile = true;
}
- if (mode == QSystemSemaphore::Create) {
- createdSemaphore = true;
- createdFile = true;
- }
-
// Created semaphore so initialize its value.
if (createdSemaphore && initialValue >= 0) {
qt_semun init_op;
@@ -173,8 +174,8 @@ key_t QSystemSemaphorePrivate::handle(QSystemSemaphore::AccessMode mode)
/*!
\internal
- Cleanup the unix_key
- */
+ Clean up the semaphore
+*/
void QSystemSemaphorePrivate::cleanHandle()
{
unix_key = -1;
@@ -189,8 +190,8 @@ void QSystemSemaphorePrivate::cleanHandle()
if (-1 != semaphore) {
if (-1 == semctl(semaphore, 0, IPC_RMID, 0)) {
setErrorString(QLatin1String("QSystemSemaphore::cleanHandle"));
-#if defined QSYSTEMSEMAPHORE_DEBUG
- qDebug() << QLatin1String("QSystemSemaphore::cleanHandle semctl failed.");
+#ifdef QSYSTEMSEMAPHORE_DEBUG
+ qDebug("QSystemSemaphore::cleanHandle semctl failed.");
#endif
}
semaphore = -1;
@@ -201,7 +202,7 @@ void QSystemSemaphorePrivate::cleanHandle()
/*!
\internal
- */
+*/
bool QSystemSemaphorePrivate::modifySemaphore(int count)
{
if (-1 == handle())
@@ -223,7 +224,7 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count)
return modifySemaphore(count);
}
setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));
-#if defined QSYSTEMSEMAPHORE_DEBUG
+#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << QLatin1String("QSystemSemaphore::modify failed") << count << semctl(semaphore, 0, GETVAL) << errno << EIDRM << EINVAL;
#endif
return false;
@@ -232,7 +233,6 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count)
return true;
}
-
QT_END_NAMESPACE
#endif // QT_NO_SYSTEMSEMAPHORE
diff --git a/src/corelib/kernel/qsystemsemaphore_win.cpp b/src/corelib/kernel/qsystemsemaphore_win.cpp
index 30cab7ebfe..0e9d645edd 100644
--- a/src/corelib/kernel/qsystemsemaphore_win.cpp
+++ b/src/corelib/kernel/qsystemsemaphore_win.cpp
@@ -44,18 +44,20 @@
#include "qcoreapplication.h"
#include <qdebug.h>
-QT_BEGIN_NAMESPACE
-
#ifndef QT_NO_SYSTEMSEMAPHORE
+//#define QSYSTEMSEMAPHORE_DEBUG
+
+QT_BEGIN_NAMESPACE
+
QSystemSemaphorePrivate::QSystemSemaphorePrivate() :
- semaphore(0), error(QSystemSemaphore::NoError)
+ semaphore(0), error(QSystemSemaphore::NoError)
{
}
void QSystemSemaphorePrivate::setErrorString(const QString &function)
{
- BOOL windowsError = GetLastError();
+ DWORD windowsError = GetLastError();
if (windowsError == 0)
return;
@@ -72,9 +74,10 @@ void QSystemSemaphorePrivate::setErrorString(const QString &function)
default:
errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(windowsError);
error = QSystemSemaphore::UnknownError;
-#if defined QSYSTEMSEMAPHORE_DEBUG
+#ifdef QSYSTEMSEMAPHORE_DEBUG
qDebug() << errorString << "key" << key;
#endif
+ break;
}
}
@@ -110,7 +113,7 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count)
return false;
if (count > 0) {
- if (0 == ReleaseSemaphore(semaphore, count, 0)) {
+ if (0 == ReleaseSemaphore(semaphore, count, 0)) {
setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));
#if defined QSYSTEMSEMAPHORE_DEBUG
qDebug() << QLatin1String("QSystemSemaphore::modifySemaphore ReleaseSemaphore failed");
@@ -130,6 +133,6 @@ bool QSystemSemaphorePrivate::modifySemaphore(int count)
return true;
}
-#endif //QT_NO_SYSTEMSEMAPHORE
+#endif // QT_NO_SYSTEMSEMAPHORE
QT_END_NAMESPACE