From 7b60b6621e8a3fce144b8fe5edb07bcf9cc331d7 Mon Sep 17 00:00:00 2001 From: Ritt Konstantin Date: Mon, 6 Jun 2011 14:56:46 +0200 Subject: code clean-up for QSystemSemaphore Merge-request: 1248 Reviewed-by: Harald Fernengel --- src/corelib/kernel/qsystemsemaphore_p.h | 8 ++-- src/corelib/kernel/qsystemsemaphore_symbian.cpp | 49 ++++++++++++----------- src/corelib/kernel/qsystemsemaphore_unix.cpp | 52 ++++++++++++------------- src/corelib/kernel/qsystemsemaphore_win.cpp | 17 ++++---- 4 files changed, 65 insertions(+), 61 deletions(-) (limited to 'src') 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 +# include #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 -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 #include #include -#include #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 -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 -- cgit v1.2.3