summaryrefslogtreecommitdiffstats
path: root/src/gui/embedded/qwslock.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/embedded/qwslock.cpp')
-rw-r--r--src/gui/embedded/qwslock.cpp185
1 files changed, 80 insertions, 105 deletions
diff --git a/src/gui/embedded/qwslock.cpp b/src/gui/embedded/qwslock.cpp
index 0d65b61..c14f50b 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,164 +66,141 @@ QT_BEGIN_NAMESPACE
#error QWSLock currently requires semaphores
#endif
-QWSLock::QWSLock()
+QWSLock::QWSLock(int id) : semId(id)
{
- semId = semget(IPC_PRIVATE, 3, IPC_CREAT | 0666);
+ static unsigned short initialValues[3] = { 1, 1, 0 };
- if (semId == -1) {
- perror("QWSLock::QWSLock");
- qFatal("Unable to create semaphore");
- }
- QWSSignalHandler::instance()->addSemaphore(semId);
-
- qt_semun semval;
- semval.val = 1;
-
- if (semctl(semId, BackingStore, SETVAL, semval) == -1) {
- perror("QWSLock::QWSLock");
- qFatal("Unable to initialize backingstore semaphore");
- }
- lockCount[BackingStore] = 0;
-
- if (semctl(semId, Communication, SETVAL, semval) == -1) {
- perror("QWSLock::QWSLock");
- qFatal("Unable to initialize communication semaphore");
- }
- lockCount[Communication] = 0;
+#ifndef QT_NO_QWS_SIGNALHANDLER
+ QWSSignalHandler::instance()->addWSLock(this);
+#endif
- semval.val = 0;
- if (semctl(semId, RegionEvent, SETVAL, semval) == -1) {
- perror("QWSLock::QWSLock");
- qFatal("Unable to initialize region event semaphore");
+ if (semId == -1) {
+ 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");
+ }
}
-}
-QWSLock::QWSLock(int id)
-{
- semId = id;
- QWSSignalHandler::instance()->addSemaphore(semId);
lockCount[0] = lockCount[1] = 0;
}
QWSLock::~QWSLock()
{
- if (semId == -1)
- return;
- QWSSignalHandler::instance()->removeSemaphore(semId);
-}
+#ifndef QT_NO_QWS_SIGNALHANDLER
+ QWSSignalHandler::instance()->removeWSLock(this);
+#endif
-static bool forceLock(int semId, int semNum, int)
-{
- int ret;
- do {
- 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)
- sops.sem_flg |= SEM_UNDO;
-
- ret = semop(semId, &sops, 1);
- if (ret == -1 && errno != EINTR)
- qDebug("QWSLock::lock: %s", strerror(errno));
- } while (ret == -1 && errno == EINTR);
-
- return (ret != -1);
+ if (semId != -1) {
+ qt_semun semval;
+ semval.val = 0;
+ semctl(semId, 0, IPC_RMID, semval);
+ semId = -1;
+ }
}
-static bool up(int semId, int semNum)
+bool QWSLock::up(unsigned short semNum)
{
int ret;
- do {
- sembuf sops = { semNum, 1, 0 };
- ret = semop(semId, &sops, 1);
- if (ret == -1 && errno != EINTR)
- qDebug("QWSLock::up: %s", strerror(errno));
- } while (ret == -1 && errno == EINTR);
-
- return (ret != -1);
+
+ 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) {
+ qDebug("QWSLock::up(): %s", strerror(errno));
+ return false;
+ }
+
+ return true;
}
-static bool down(int semId, int semNum)
+bool QWSLock::down(unsigned short semNum, int)
{
int ret;
- do {
- sembuf sops = { semNum, -1, 0 };
- ret = semop(semId, &sops, 1);
- if (ret == -1 && errno != EINTR)
- qDebug("QWSLock::down: %s", strerror(errno));
- } while (ret == -1 && errno == EINTR);
-
- return (ret != -1);
+
+ 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) {
+ qDebug("QWSLock::down(): %s", strerror(errno));
+ return false;
+ }
+
+ return true;
}
-static int getValue(int semId, int semNum)
+int QWSLock::getValue(unsigned short semNum) const
{
- int ret;
- do {
- ret = semctl(semId, semNum, GETVAL, 0);
- if (ret == -1 && errno != EINTR)
- qDebug("QWSLock::getValue: %s", strerror(errno));
- } while (ret == -1 && errno == EINTR);
-
+ int ret = semctl(semId, semNum, GETVAL, 0);
+ if (ret == -1)
+ qDebug("QWSLock::getValue(): %s", strerror(errno));
return ret;
}
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 int 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;