summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRitt Konstantin <qnx@ics.com>2011-06-21 13:51:09 +0200
committerHarald Fernengel <harald.fernengel@nokia.com>2011-06-21 14:05:42 +0200
commit7657f6c1d9ee8b3c55eb291dda123565d45d645a (patch)
tree64b920a9da063e408bdde385f26b0167adb5f3f8
parentd869e1ad4b0007757e97046609de2097cd9e9c5d (diff)
implement POSIX IPC based QLock, QWSLock and QWSSharedMemory backends
Merge-request: 1259 Reviewed-by: Harald Fernengel <harald.fernengel@nokia.com>
-rw-r--r--src/corelib/global/qglobal.h7
-rw-r--r--src/gui/embedded/qlock.cpp128
-rw-r--r--src/gui/embedded/qwslock.cpp67
-rw-r--r--src/gui/embedded/qwslock_p.h8
-rw-r--r--src/gui/embedded/qwssharedmemory.cpp89
-rw-r--r--src/gui/embedded/qwssharedmemory_p.h5
-rw-r--r--src/gui/painting/qwindowsurface_qws.cpp8
-rw-r--r--src/gui/text/qfontdatabase_qws.cpp5
8 files changed, 287 insertions, 30 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h
index c927556e41..c7b0b505bf 100644
--- a/src/corelib/global/qglobal.h
+++ b/src/corelib/global/qglobal.h
@@ -2746,13 +2746,6 @@ QT_LICENSED_MODULE(DBus)
# define QT_NO_CONCURRENT_FILTER
#endif
-#ifdef Q_OS_QNX
-// QNX doesn't have SYSV style shared memory. Multiprocess QWS apps
-// and shared fonts are not available
-# define QT_NO_QWS_MULTIPROCESS
-# define QT_NO_QWS_SHARE_FONTS
-#endif
-
#if defined (__ELF__)
# if defined (Q_OS_LINUX) || defined (Q_OS_SOLARIS) || defined (Q_OS_FREEBSD) || defined (Q_OS_OPENBSD) || defined (Q_OS_IRIX)
# define Q_OF_ELF
diff --git a/src/gui/embedded/qlock.cpp b/src/gui/embedded/qlock.cpp
index ac154313cf..eaad15c40c 100644
--- a/src/gui/embedded/qlock.cpp
+++ b/src/gui/embedded/qlock.cpp
@@ -41,7 +41,6 @@
#include "qlock_p.h"
-
#ifdef QT_NO_QWS_MULTIPROCESS
QT_BEGIN_NAMESPACE
@@ -83,7 +82,7 @@ QT_END_NAMESPACE
#else // QT_NO_QWS_MULTIPROCESS
#if defined(Q_OS_DARWIN)
-# define Q_NO_SEMAPHORE
+# define QT_NO_SEMAPHORE
#endif
#include "qwssignalhandler_p.h"
@@ -91,11 +90,13 @@ QT_END_NAMESPACE
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
-#if defined(Q_NO_SEMAPHORE)
+#if defined(QT_NO_SEMAPHORE)
# include <sys/stat.h>
# include <sys/file.h>
-#else
+#elif !defined(QT_POSIX_IPC)
# include <sys/sem.h>
+#else
+# include <semaphore.h>
#endif
#include <string.h>
#include <errno.h>
@@ -109,17 +110,24 @@ QT_BEGIN_NAMESPACE
class QLockData
{
public:
-#ifdef Q_NO_SEMAPHORE
+#if defined(QT_NO_SEMAPHORE) || defined(QT_POSIX_IPC)
QByteArray file;
-#endif // Q_NO_SEMAPHORE
+#endif
+#if !defined(QT_POSIX_IPC)
int id;
+#else
+ sem_t *id; // Read mode resource counter
+ sem_t *rsem; // Read mode lock
+ sem_t *wsem; // Write mode lock
+#endif
int count;
bool owned;
};
+
/*!
\class QLock
- \brief The QLock class is a wrapper for a System V shared semaphore.
+ \brief The QLock class is a wrapper for a system shared semaphore.
\ingroup qws
@@ -148,7 +156,7 @@ QLock::QLock(const QString &filename, char id, bool create)
{
data = new QLockData;
data->count = 0;
-#ifdef Q_NO_SEMAPHORE
+#if defined(QT_NO_SEMAPHORE)
data->file = filename.toLocal8Bit() + id;
for (int x = 0; x < 2; ++x) {
data->id = QT_OPEN(data->file.constData(), O_RDWR | (x ? O_CREAT : 0), S_IRWXU);
@@ -157,7 +165,7 @@ QLock::QLock(const QString &filename, char id, bool create)
break;
}
}
-#else
+#elif !defined(QT_POSIX_IPC)
key_t semkey = ftok(filename.toLocal8Bit().constData(), id);
data->id = semget(semkey, 0, 0);
data->owned = create;
@@ -170,6 +178,28 @@ QLock::QLock(const QString &filename, char id, bool create)
arg.val = MAX_LOCKS;
semctl(data->id, 0, SETVAL, arg);
}
+#else
+ data->file = filename.toLocal8Bit() + id;
+ data->owned = create;
+
+ char ids[3] = { 'c', 'r', 'w' };
+ sem_t **sems[3] = { &data->id, &data->rsem, &data->wsem };
+ unsigned short initialValues[3] = { MAX_LOCKS, 1, 1 };
+ for (int i = 0; i < 3; ++i) {
+ QByteArray file = data->file + ids[i];
+ do {
+ *sems[i] = sem_open(file.constData(), 0, 0666, 0);
+ } while (*sems[i] == SEM_FAILED && errno == EINTR);
+ if (create) {
+ if (*sems[i] != SEM_FAILED) {
+ sem_close(*sems[i]);
+ sem_unlink(file.constData());
+ }
+ do {
+ *sems[i] = sem_open(file.constData(), O_CREAT, 0666, initialValues[i]);
+ } while (*sems[i] == SEM_FAILED && errno == EINTR);
+ }
+ }
#endif
if (!isValid()) {
qWarning("QLock::QLock: Cannot %s semaphore %s '%c' (%d, %s)",
@@ -193,17 +223,32 @@ QLock::~QLock()
while (locked())
unlock();
-#ifdef Q_NO_SEMAPHORE
+
+#if defined(QT_NO_SEMAPHORE)
if (isValid())
QT_CLOSE(data->id);
+#elif defined(QT_POSIX_IPC)
+ if (data->id != SEM_FAILED)
+ sem_close(data->id);
+ if (data->rsem != SEM_FAILED)
+ sem_close(data->rsem);
+ if (data->wsem != SEM_FAILED)
+ sem_close(data->wsem);
#endif
+
if (data->owned) {
-#ifdef Q_NO_SEMAPHORE
+#if defined(QT_NO_SEMAPHORE)
unlink(data->file.constData());
-#else
+#elif !defined(QT_POSIX_IPC)
qt_semun semval;
semval.val = 0;
semctl(data->id, 0, IPC_RMID, semval);
+#else
+ char ids[3] = { 'c', 'r', 'w' };
+ for (int i = 0; i < 3; ++i) {
+ QByteArray file = data->file + ids[i];
+ sem_unlink(file.constData());
+ }
#endif
}
delete data;
@@ -216,7 +261,11 @@ QLock::~QLock()
*/
bool QLock::isValid() const
{
+#if !defined(QT_POSIX_IPC)
return data && data->id != -1;
+#else
+ return data && data->id != SEM_FAILED && data->rsem != SEM_FAILED && data->wsem != SEM_FAILED;
+#endif
}
/*!
@@ -232,21 +281,48 @@ bool QLock::isValid() const
*/
void QLock::lock(Type t)
{
+ if (!isValid())
+ return;
+
if (!data->count) {
type = t;
int rv;
-#ifdef Q_NO_SEMAPHORE
+#if defined(QT_NO_SEMAPHORE)
int op = type == Write ? LOCK_EX : LOCK_SH;
EINTR_LOOP(rv, flock(data->id, op));
-#else
+#elif !defined(QT_POSIX_IPC)
sembuf sops;
sops.sem_num = 0;
sops.sem_op = type == Write ? -MAX_LOCKS : -1;
sops.sem_flg = SEM_UNDO;
EINTR_LOOP(rv, semop(data->id, &sops, 1));
+#else
+ if (type == Write) {
+ EINTR_LOOP(rv, sem_wait(data->rsem));
+ if (rv != -1) {
+ EINTR_LOOP(rv, sem_wait(data->wsem));
+ if (rv == -1)
+ sem_post(data->rsem);
+ }
+ } else {
+ EINTR_LOOP(rv, sem_wait(data->wsem));
+ if (rv != -1) {
+ EINTR_LOOP(rv, sem_trywait(data->rsem));
+ if (rv != -1 || errno == EAGAIN) {
+ EINTR_LOOP(rv, sem_wait(data->id));
+ if (rv == -1) {
+ int semval;
+ sem_getvalue(data->id, &semval);
+ if (semval == MAX_LOCKS)
+ sem_post(data->rsem);
+ }
+ }
+ rv = sem_post(data->wsem);
+ }
+ }
#endif
if (rv == -1) {
qDebug("QLock::lock(): %s", strerror(errno));
@@ -265,19 +341,37 @@ void QLock::lock(Type t)
*/
void QLock::unlock()
{
- if (data->count) {
+ if (!isValid())
+ return;
+
+ if (data->count > 0) {
data->count--;
if (!data->count) {
int rv;
-#ifdef Q_NO_SEMAPHORE
+#if defined(QT_NO_SEMAPHORE)
EINTR_LOOP(rv, flock(data->id, LOCK_UN));
-#else
+#elif !defined(QT_POSIX_IPC)
sembuf sops;
sops.sem_num = 0;
sops.sem_op = type == Write ? MAX_LOCKS : 1;
sops.sem_flg = SEM_UNDO;
EINTR_LOOP(rv, semop(data->id, &sops, 1));
+#else
+ if (type == Write) {
+ sem_post(data->wsem);
+ rv = sem_post(data->rsem);
+ } else {
+ EINTR_LOOP(rv, sem_wait(data->wsem));
+ if (rv != -1) {
+ sem_post(data->id);
+ int semval;
+ sem_getvalue(data->id, &semval);
+ if (semval == MAX_LOCKS)
+ sem_post(data->rsem);
+ rv = sem_post(data->wsem);
+ }
+ }
#endif
if (rv == -1)
qDebug("QLock::unlock(): %s", strerror(errno));
diff --git a/src/gui/embedded/qwslock.cpp b/src/gui/embedded/qwslock.cpp
index c14f50b1ae..f9ea0002f4 100644
--- a/src/gui/embedded/qwslock.cpp
+++ b/src/gui/embedded/qwslock.cpp
@@ -45,12 +45,15 @@
#include "qwssignalhandler_p.h"
+#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
+#ifndef QT_POSIX_IPC
#include <sys/sem.h>
+#endif
#include <sys/time.h>
#include <time.h>
#ifdef Q_OS_LINUX
@@ -66,6 +69,12 @@ QT_BEGIN_NAMESPACE
#error QWSLock currently requires semaphores
#endif
+#ifdef QT_POSIX_IPC
+#include <QtCore/QAtomicInt>
+
+static QBasicAtomicInt localUniqueId = Q_BASIC_ATOMIC_INITIALIZER(1);
+#endif
+
QWSLock::QWSLock(int id) : semId(id)
{
static unsigned short initialValues[3] = { 1, 1, 0 };
@@ -74,6 +83,7 @@ QWSLock::QWSLock(int id) : semId(id)
QWSSignalHandler::instance()->addWSLock(this);
#endif
+#ifndef QT_POSIX_IPC
if (semId == -1) {
semId = semget(IPC_PRIVATE, 3, IPC_CREAT | 0666);
if (semId == -1) {
@@ -88,6 +98,30 @@ QWSLock::QWSLock(int id) : semId(id)
qFatal("Unable to initialize semaphores");
}
}
+#else
+ sems[0] = sems[1] = sems[2] = SEM_FAILED;
+ owned = false;
+
+ if (semId == -1) {
+ // ### generate really unique IDs
+ semId = (getpid() << 16) + (localUniqueId.fetchAndAddRelaxed(1) % ushort(-1));
+ owned = true;
+ }
+
+ QByteArray pfx = "/qwslock_" + QByteArray::number(semId, 16) + '_';
+ QByteArray keys[3] = { pfx + "BackingStore", pfx + "Communication", pfx + "RegionEvent" };
+ for (int i = 0; i < 3; ++i) {
+ if (owned)
+ sem_unlink(keys[i].constData());
+ do {
+ sems[i] = sem_open(keys[i].constData(), (owned ? O_CREAT : 0), 0666, initialValues[i]);
+ } while (sems[i] == SEM_FAILED && errno == EINTR);
+ if (sems[i] == SEM_FAILED) {
+ perror("QWSLock::QWSLock");
+ qFatal("Unable to %s semaphore", (owned ? "create" : "open"));
+ }
+ }
+#endif
lockCount[0] = lockCount[1] = 0;
}
@@ -99,10 +133,27 @@ QWSLock::~QWSLock()
#endif
if (semId != -1) {
+#ifndef QT_POSIX_IPC
qt_semun semval;
semval.val = 0;
semctl(semId, 0, IPC_RMID, semval);
semId = -1;
+#else
+ // emulate the SEM_UNDO behavior for the BackingStore lock
+ while (hasLock(BackingStore))
+ unlock(BackingStore);
+
+ QByteArray pfx = "/qwslock_" + QByteArray::number(semId, 16) + '_';
+ QByteArray keys[3] = { pfx + "BackingStore", pfx + "Communication", pfx + "RegionEvent" };
+ for (int i = 0; i < 3; ++i) {
+ if (sems[i] != SEM_FAILED) {
+ sem_close(sems[i]);
+ sems[i] = SEM_FAILED;
+ }
+ if (owned)
+ sem_unlink(keys[i].constData());
+ }
+#endif
}
}
@@ -110,6 +161,7 @@ bool QWSLock::up(unsigned short semNum)
{
int ret;
+#ifndef QT_POSIX_IPC
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
@@ -119,6 +171,9 @@ bool QWSLock::up(unsigned short semNum)
sops.sem_flg |= SEM_UNDO;
EINTR_LOOP(ret, semop(semId, &sops, 1));
+#else
+ ret = sem_post(sems[semNum]);
+#endif
if (ret == -1) {
qDebug("QWSLock::up(): %s", strerror(errno));
return false;
@@ -131,6 +186,7 @@ bool QWSLock::down(unsigned short semNum, int)
{
int ret;
+#ifndef QT_POSIX_IPC
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
@@ -140,6 +196,9 @@ bool QWSLock::down(unsigned short semNum, int)
sops.sem_flg |= SEM_UNDO;
EINTR_LOOP(ret, semop(semId, &sops, 1));
+#else
+ EINTR_LOOP(ret, sem_wait(sems[semNum]));
+#endif
if (ret == -1) {
qDebug("QWSLock::down(): %s", strerror(errno));
return false;
@@ -150,7 +209,13 @@ bool QWSLock::down(unsigned short semNum, int)
int QWSLock::getValue(unsigned short semNum) const
{
- int ret = semctl(semId, semNum, GETVAL, 0);
+ int ret;
+#ifndef QT_POSIX_IPC
+ ret = semctl(semId, semNum, GETVAL, 0);
+#else
+ if (sem_getvalue(sems[semNum], &ret) == -1)
+ ret = -1;
+#endif
if (ret == -1)
qDebug("QWSLock::getValue(): %s", strerror(errno));
return ret;
diff --git a/src/gui/embedded/qwslock_p.h b/src/gui/embedded/qwslock_p.h
index d020b22cb4..71a4cca9c4 100644
--- a/src/gui/embedded/qwslock_p.h
+++ b/src/gui/embedded/qwslock_p.h
@@ -57,6 +57,10 @@
#ifndef QT_NO_QWS_MULTIPROCESS
+#ifdef QT_POSIX_IPC
+# include <semaphore.h>
+#endif
+
QT_BEGIN_NAMESPACE
class QWSLock
@@ -80,6 +84,10 @@ private:
int semId;
int lockCount[2];
+#ifdef QT_POSIX_IPC
+ sem_t *sems[3];
+ bool owned;
+#endif
};
QT_END_NAMESPACE
diff --git a/src/gui/embedded/qwssharedmemory.cpp b/src/gui/embedded/qwssharedmemory.cpp
index a677626678..853de615dc 100644
--- a/src/gui/embedded/qwssharedmemory.cpp
+++ b/src/gui/embedded/qwssharedmemory.cpp
@@ -45,14 +45,37 @@
#include <sys/types.h>
#include <sys/ipc.h>
+#ifndef QT_POSIX_IPC
#include <sys/shm.h>
+#else
+#include <sys/mman.h>
+#include <sys/stat.h>
+#endif
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <private/qcore_unix_p.h>
//#define QT_SHM_DEBUG
QT_BEGIN_NAMESPACE
+#ifdef QT_POSIX_IPC
+#include <QtCore/QAtomicInt>
+
+static QBasicAtomicInt localUniqueId = Q_BASIC_ATOMIC_INITIALIZER(1);
+
+static inline QByteArray makeKey(int id)
+{
+ return "/qwsshm_" + QByteArray::number(id, 16);
+}
+#endif
+
QWSSharedMemory::QWSSharedMemory()
: shmId(-1), shmBase(0), shmSize(0)
+#ifdef QT_POSIX_IPC
+ , hand(-1)
+#endif
{
}
@@ -66,19 +89,47 @@ bool QWSSharedMemory::create(int size)
if (shmId != -1)
detach();
+#ifndef QT_POSIX_IPC
shmId = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
+#else
+ // ### generate really unique IDs
+ shmId = (getpid() << 16) + (localUniqueId.fetchAndAddRelaxed(1) % ushort(-1));
+ QByteArray shmName = makeKey(shmId);
+ EINTR_LOOP(hand, shm_open(shmName.constData(), O_RDWR | O_CREAT, 0660));
+ if (hand != -1) {
+ // the size may only be set once; ignore errors
+ int ret;
+ EINTR_LOOP(ret, ftruncate(hand, size));
+ if (ret == -1)
+ shmId = -1;
+ } else {
+ shmId = -1;
+ }
+#endif
if (shmId == -1) {
#ifdef QT_SHM_DEBUG
perror("QWSSharedMemory::create():");
qWarning("Error allocating shared memory of size %d", size);
#endif
+ detach();
return false;
}
+
+#ifndef QT_POSIX_IPC
shmBase = shmat(shmId, 0, 0);
// On Linux, it is possible to attach a shared memory segment even if it
// is already marked to be deleted. However, POSIX.1-2001 does not specify
// this behaviour and many other implementations do not support it.
shmctl(shmId, IPC_RMID, 0);
+#else
+ // grab the size
+ QT_STATBUF st;
+ if (QT_FSTAT(hand, &st) != -1) {
+ shmSize = st.st_size;
+ // grab the memory
+ shmBase = mmap(0, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, hand, 0);
+ }
+#endif
if (shmBase == (void*)-1 || !shmBase) {
#ifdef QT_SHM_DEBUG
perror("QWSSharedMemory::create():");
@@ -102,7 +153,21 @@ bool QWSSharedMemory::attach(int id)
return false;
shmId = id;
+#ifndef QT_POSIX_IPC
shmBase = shmat(shmId, 0, 0);
+#else
+ QByteArray shmName = makeKey(shmId);
+ EINTR_LOOP(hand, shm_open(shmName.constData(), O_RDWR, 0660));
+ if (hand != -1) {
+ // grab the size
+ QT_STATBUF st;
+ if (QT_FSTAT(hand, &st) != -1) {
+ shmSize = st.st_size;
+ // grab the memory
+ shmBase = mmap(0, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, hand, 0);
+ }
+ }
+#endif
if (shmBase == (void*)-1 || !shmBase) {
#ifdef QT_SHM_DEBUG
perror("QWSSharedMemory::attach():");
@@ -117,8 +182,28 @@ bool QWSSharedMemory::attach(int id)
void QWSSharedMemory::detach()
{
+#ifndef QT_POSIX_IPC
if (shmBase && shmBase != (void*)-1)
shmdt(shmBase);
+#else
+ if (shmBase && shmBase != (void*)-1)
+ munmap(shmBase, shmSize);
+ if (hand > 0) {
+ // get the number of current attachments
+ int shm_nattch = 0;
+ QT_STATBUF st;
+ if (QT_FSTAT(hand, &st) == 0) {
+ // subtract 2 from linkcount: one for our own open and one for the dir entry
+ shm_nattch = st.st_nlink - 2;
+ }
+ qt_safe_close(hand);
+ // if there are no attachments then unlink the shared memory
+ if (shm_nattch == 0) {
+ QByteArray shmName = makeKey(shmId);
+ shm_unlink(shmName.constData());
+ }
+ }
+#endif
shmBase = 0;
shmSize = 0;
shmId = -1;
@@ -129,11 +214,13 @@ int QWSSharedMemory::size() const
if (shmId == -1)
return 0;
+#ifndef QT_POSIX_IPC
if (!shmSize) {
struct shmid_ds shm;
shmctl(shmId, IPC_STAT, &shm);
- const_cast<QWSSharedMemory *>(this)->shmSize = shm.shm_segsz;
+ shmSize = shm.shm_segsz;
}
+#endif
return shmSize;
}
diff --git a/src/gui/embedded/qwssharedmemory_p.h b/src/gui/embedded/qwssharedmemory_p.h
index f3ce24101b..42ef6c8cef 100644
--- a/src/gui/embedded/qwssharedmemory_p.h
+++ b/src/gui/embedded/qwssharedmemory_p.h
@@ -77,7 +77,10 @@ public:
private:
int shmId;
void *shmBase;
- int shmSize;
+ mutable int shmSize;
+#ifdef QT_POSIX_IPC
+ int hand;
+#endif
};
#endif // QT_NO_QWS_MULTIPROCESS
diff --git a/src/gui/painting/qwindowsurface_qws.cpp b/src/gui/painting/qwindowsurface_qws.cpp
index 7e8cf9bbb1..3789a33110 100644
--- a/src/gui/painting/qwindowsurface_qws.cpp
+++ b/src/gui/painting/qwindowsurface_qws.cpp
@@ -1065,10 +1065,12 @@ bool QWSSharedMemSurface::setMemory(int memId)
return true;
mem.detach();
- if (!mem.attach(memId)) {
+
+ if (memId != -1 && !mem.attach(memId)) {
+#ifndef QT_NO_DEBUG
perror("QWSSharedMemSurface: attaching to shared memory");
- qCritical("QWSSharedMemSurface: Error attaching to"
- " shared memory 0x%x", memId);
+ qCritical("QWSSharedMemSurface: Error attaching to shared memory 0x%x", memId);
+#endif
return false;
}
diff --git a/src/gui/text/qfontdatabase_qws.cpp b/src/gui/text/qfontdatabase_qws.cpp
index c5a04f1700..87862e7183 100644
--- a/src/gui/text/qfontdatabase_qws.cpp
+++ b/src/gui/text/qfontdatabase_qws.cpp
@@ -75,6 +75,11 @@
#include <qresource.h>
#endif
+#ifdef Q_OS_QNX
+// ### using QFontEngineQPF leads to artifacts on QNX
+# define QT_NO_QWS_SHARE_FONTS
+#endif
+
QT_BEGIN_NAMESPACE
#ifndef QT_NO_LIBRARY