summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRitt Konstantin <ritt.ks@gmail.com>2011-06-01 14:49:42 (GMT)
committerHarald Fernengel <harald.fernengel@nokia.com>2011-06-01 14:49:42 (GMT)
commit6896244f266becfb5807985c5628c8f0a775ce76 (patch)
treee6d8195c91ab50c65ab7403b00a5112e86f397f9 /src
parent4134cb7dfc04ca7721277772c41b678bac6b33a0 (diff)
downloadQt-6896244f266becfb5807985c5628c8f0a775ce76.zip
Qt-6896244f266becfb5807985c5628c8f0a775ce76.tar.gz
Qt-6896244f266becfb5807985c5628c8f0a775ce76.tar.bz2
minor refactoring of the QWSLock class
unify both constructors in a single one; make the code cleaner and cheaper Merge-request: 1237 Reviewed-by: Harald Fernengel <harald.fernengel@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/gui/embedded/qwslock.cpp101
-rw-r--r--src/gui/embedded/qwslock_p.h14
2 files changed, 47 insertions, 68 deletions
diff --git a/src/gui/embedded/qwslock.cpp b/src/gui/embedded/qwslock.cpp
index d8996d6..67f8bd8 100644
--- a/src/gui/embedded/qwslock.cpp
+++ b/src/gui/embedded/qwslock.cpp
@@ -45,8 +45,6 @@
#include "qwssignalhandler_p.h"
-#include <qglobal.h>
-#include <qdebug.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
@@ -68,36 +66,30 @@ QT_BEGIN_NAMESPACE
#error QWSLock currently requires semaphores
#endif
-QWSLock::QWSLock()
+QWSLock::QWSLock(int id) : semId(id)
{
static unsigned short initialValues[3] = { 1, 1, 0 };
- semId = semget(IPC_PRIVATE, 3, IPC_CREAT | 0666);
if (semId == -1) {
- perror("QWSLock::QWSLock");
- qFatal("Unable to create semaphore");
- }
-#ifndef QT_NO_QWS_SIGNALHANDLER
- QWSSignalHandler::instance()->addSemaphore(semId);
-#endif
+ semId = semget(IPC_PRIVATE, 3, IPC_CREAT | 0666);
+ if (semId == -1) {
+ perror("QWSLock::QWSLock");
+ qFatal("Unable to create semaphore");
+ }
- qt_semun semval;
- semval.array = initialValues;
- if (semctl(semId, 0, SETALL, semval) == -1) {
- perror("QWSLock::QWSLock");
- qFatal("Unable to initialize semaphores");
+ qt_semun semval;
+ semval.array = initialValues;
+ if (semctl(semId, 0, SETALL, semval) == -1) {
+ perror("QWSLock::QWSLock");
+ qFatal("Unable to initialize semaphores");
+ }
}
lockCount[0] = lockCount[1] = 0;
-}
-QWSLock::QWSLock(int id)
-{
- semId = id;
#ifndef QT_NO_QWS_SIGNALHANDLER
QWSSignalHandler::instance()->addSemaphore(semId);
#endif
- lockCount[0] = lockCount[1] = 0;
}
QWSLock::~QWSLock()
@@ -113,35 +105,20 @@ QWSLock::~QWSLock()
}
}
-static bool forceLock(int semId, unsigned short semNum, int)
+bool QWSLock::up(unsigned short semNum)
{
int ret;
- sembuf sops = { semNum, -1, 0 };
+ sembuf sops = { semNum, 1, 0 };
// As the BackingStore lock is a mutex, and only one process may own
// the lock, it's safe to use SEM_UNDO. On the other hand, the
// Communication lock is locked by the client but unlocked by the
// server and therefore can't use SEM_UNDO.
- if (semNum == QWSLock::BackingStore)
+ if (semNum == BackingStore)
sops.sem_flg |= SEM_UNDO;
EINTR_LOOP(ret, semop(semId, &sops, 1));
if (ret == -1) {
- qDebug("QWSLock::lock(): %s", strerror(errno));
- return false;
- }
-
- return true;
-}
-
-static bool up(int semId, unsigned short semNum)
-{
- int ret;
-
- sembuf sops = { semNum, 1, 0 };
-
- EINTR_LOOP(ret, semop(semId, &sops, 1));
- if (ret == -1) {
qDebug("QWSLock::up(): %s", strerror(errno));
return false;
}
@@ -149,11 +126,17 @@ static bool up(int semId, unsigned short semNum)
return true;
}
-static bool down(int semId, unsigned short semNum)
+bool QWSLock::down(unsigned short semNum, int)
{
int ret;
sembuf sops = { semNum, -1, 0 };
+ // As the BackingStore lock is a mutex, and only one process may own
+ // the lock, it's safe to use SEM_UNDO. On the other hand, the
+ // Communication lock is locked by the client but unlocked by the
+ // server and therefore can't use SEM_UNDO.
+ if (semNum == BackingStore)
+ sops.sem_flg |= SEM_UNDO;
EINTR_LOOP(ret, semop(semId, &sops, 1));
if (ret == -1) {
@@ -164,7 +147,7 @@ static bool down(int semId, unsigned short semNum)
return true;
}
-static int getValue(int semId, unsigned short semNum)
+int QWSLock::getValue(unsigned short semNum) const
{
int ret = semctl(semId, semNum, GETVAL, 0);
if (ret == -1)
@@ -175,56 +158,48 @@ static int getValue(int semId, unsigned short semNum)
bool QWSLock::lock(LockType type, int timeout)
{
if (type == RegionEvent)
- return up(semId, RegionEvent);
+ return up(type);
- if (hasLock(type)) {
+ if (lockCount[type] > 0) {
++lockCount[type];
return true;
}
- if (!forceLock(semId, type, timeout))
- return false;
- ++lockCount[type];
- return true;
+ if (down(type, timeout)) {
+ ++lockCount[type];
+ return true;
+ }
+
+ return false;
}
bool QWSLock::hasLock(LockType type)
{
if (type == RegionEvent)
- return (getValue(semId, RegionEvent) == 0);
+ return getValue(type) == 0;
- return (lockCount[type] > 0);
+ return lockCount[type] > 0;
}
void QWSLock::unlock(LockType type)
{
if (type == RegionEvent) {
- down(semId, RegionEvent);
+ down(type, -1);
return;
}
- if (hasLock(type)) {
+ if (lockCount[type] > 0) {
--lockCount[type];
- if (hasLock(type))
+ if (lockCount[type] > 0)
return;
}
- const unsigned short semNum = type;
- int ret;
- do {
- sembuf sops = {semNum, 1, 0};
- if (semNum == QWSLock::BackingStore)
- sops.sem_flg |= SEM_UNDO;
-
- ret = semop(semId, &sops, 1);
- if (ret == -1 && errno != EINTR)
- qDebug("QWSLock::unlock(): %s", strerror(errno));
- } while (ret == -1 && errno == EINTR);
+ up(type);
}
bool QWSLock::wait(LockType type, int timeout)
{
- bool ok = forceLock(semId, type, timeout);
+ bool ok = down(type, timeout);
if (ok)
unlock(type);
return ok;
diff --git a/src/gui/embedded/qwslock_p.h b/src/gui/embedded/qwslock_p.h
index 9a7f279..d020b22 100644
--- a/src/gui/embedded/qwslock_p.h
+++ b/src/gui/embedded/qwslock_p.h
@@ -55,17 +55,16 @@
#include <qglobal.h>
-QT_BEGIN_NAMESPACE
-
#ifndef QT_NO_QWS_MULTIPROCESS
+QT_BEGIN_NAMESPACE
+
class QWSLock
{
public:
enum LockType { BackingStore, Communication, RegionEvent };
- QWSLock();
- QWSLock(int lockId);
+ QWSLock(int lockId = -1);
~QWSLock();
bool lock(LockType type, int timeout = -1);
@@ -75,11 +74,16 @@ public:
int id() const { return semId; }
private:
+ bool up(unsigned short semNum);
+ bool down(unsigned short semNum, int timeout);
+ int getValue(unsigned short semNum) const;
+
int semId;
int lockCount[2];
};
-
QT_END_NAMESPACE
+
#endif // QT_NO_QWS_MULTIPROCESS
+
#endif // QWSLOCK_P_H