summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/thread')
-rw-r--r--src/corelib/thread/qmutex_p.h11
-rw-r--r--src/corelib/thread/qmutex_unix.cpp48
-rw-r--r--src/corelib/thread/qthread_unix.cpp14
-rw-r--r--src/corelib/thread/qthreadpool.cpp2
4 files changed, 62 insertions, 13 deletions
diff --git a/src/corelib/thread/qmutex_p.h b/src/corelib/thread/qmutex_p.h
index 3ca742b194..191edda6d1 100644
--- a/src/corelib/thread/qmutex_p.h
+++ b/src/corelib/thread/qmutex_p.h
@@ -55,11 +55,14 @@
#if defined(Q_OS_MAC)
# include <mach/semaphore.h>
-#endif
-
-#if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
+#elif defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
// use Linux mutexes everywhere except for LSB builds
# define QT_LINUX_FUTEX
+#elif defined(Q_OS_UNIX)
+# if _POSIX_VERSION-0 >= 200112L || _XOPEN_VERSION-0 >= 600
+# include <semaphore.h>
+# define QT_UNIX_SEMAPHORE
+# endif
#endif
struct timespec;
@@ -120,6 +123,8 @@ public:
//platform specific stuff
#if defined(Q_OS_MAC)
semaphore_t mach_semaphore;
+#elif defined(QT_UNIX_SEMAPHORE)
+ sem_t semaphore;
#elif defined(Q_OS_UNIX)
bool wakeup;
pthread_mutex_t mutex;
diff --git a/src/corelib/thread/qmutex_unix.cpp b/src/corelib/thread/qmutex_unix.cpp
index 74e0d68f94..daa09e962b 100644
--- a/src/corelib/thread/qmutex_unix.cpp
+++ b/src/corelib/thread/qmutex_unix.cpp
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
+** Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -42,6 +43,7 @@
#include <errno.h>
#include <sys/time.h>
#include <time.h>
+#include "private/qcore_unix_p.h"
#if defined(Q_OS_VXWORKS) && defined(wakeup)
#undef wakeup
@@ -55,6 +57,51 @@ static void report_error(int code, const char *where, const char *what)
qWarning("%s: %s failure: %s", where, what, qPrintable(qt_error_string(code)));
}
+#ifdef QT_UNIX_SEMAPHORE
+
+QMutexPrivate::QMutexPrivate()
+{
+ report_error(sem_init(&semaphore, 0, 0), "QMutex", "sem_init");
+}
+
+QMutexPrivate::~QMutexPrivate()
+{
+
+ report_error(sem_destroy(&semaphore), "QMutex", "sem_destroy");
+}
+
+bool QMutexPrivate::wait(int timeout)
+{
+ int errorCode;
+ if (timeout < 0) {
+ do {
+ errorCode = sem_wait(&semaphore);
+ } while (errorCode && errno == EINTR);
+ report_error(errorCode, "QMutex::lock()", "sem_wait");
+ } else {
+ timespec ts;
+ report_error(clock_gettime(CLOCK_REALTIME, &ts), "QMutex::lock()", "clock_gettime");
+ ts.tv_sec += timeout / 1000;
+ ts.tv_nsec += timeout % 1000 * Q_UINT64_C(1000) * 1000;
+ normalizedTimespec(ts);
+ do {
+ errorCode = sem_timedwait(&semaphore, &ts);
+ } while (errorCode && errno == EINTR);
+
+ if (errorCode && errno == ETIMEDOUT)
+ return false;
+ report_error(errorCode, "QMutex::lock()", "sem_timedwait");
+ }
+ return true;
+}
+
+void QMutexPrivate::wakeUp() Q_DECL_NOTHROW
+{
+ report_error(sem_post(&semaphore), "QMutex::unlock", "sem_post");
+}
+
+#else // QT_UNIX_SEMAPHORE
+
QMutexPrivate::QMutexPrivate()
: wakeup(false)
{
@@ -103,6 +150,7 @@ void QMutexPrivate::wakeUp() Q_DECL_NOTHROW
report_error(pthread_mutex_unlock(&mutex), "QMutex::unlock", "mutex unlock");
}
+#endif
QT_END_NAMESPACE
diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp
index af4ce7c59e..9329c515af 100644
--- a/src/corelib/thread/qthread_unix.cpp
+++ b/src/corelib/thread/qthread_unix.cpp
@@ -38,18 +38,16 @@
#include <private/qcoreapplication_p.h>
#include <private/qcore_unix_p.h>
-#if defined(Q_OS_BLACKBERRY)
-# include <private/qeventdispatcher_blackberry_p.h>
-#elif defined(Q_OS_OSX)
+#if defined(Q_OS_OSX)
# include <private/qeventdispatcher_cf_p.h>
-# include <private/qeventdispatcher_unix_p.h>
#else
# if !defined(QT_NO_GLIB)
# include "../kernel/qeventdispatcher_glib_p.h"
# endif
-# include <private/qeventdispatcher_unix_p.h>
#endif
+#include <private/qeventdispatcher_unix_p.h>
+
#include "qthreadstorage.h"
#include "qthread_p.h"
@@ -250,16 +248,14 @@ typedef void*(*QtThreadCallback)(void*);
void QThreadPrivate::createEventDispatcher(QThreadData *data)
{
-#if defined(Q_OS_BLACKBERRY)
- data->eventDispatcher.storeRelease(new QEventDispatcherBlackberry);
-# elif defined(Q_OS_OSX)
+#if defined(Q_OS_OSX)
bool ok = false;
int value = qEnvironmentVariableIntValue("QT_EVENT_DISPATCHER_CORE_FOUNDATION", &ok);
if (ok && value > 0)
data->eventDispatcher.storeRelease(new QEventDispatcherCoreFoundation);
else
data->eventDispatcher.storeRelease(new QEventDispatcherUNIX);
-# elif !defined(QT_NO_GLIB)
+#elif !defined(QT_NO_GLIB)
if (qEnvironmentVariableIsEmpty("QT_NO_GLIB")
&& qEnvironmentVariableIsEmpty("QT_NO_THREADED_GLIB")
&& QEventDispatcherGlib::versionSupported())
diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp
index e4a5368281..69f7572b34 100644
--- a/src/corelib/thread/qthreadpool.cpp
+++ b/src/corelib/thread/qthreadpool.cpp
@@ -263,7 +263,7 @@ void QThreadPoolPrivate::reset()
allThreadsCopy.swap(allThreads);
locker.unlock();
- foreach (QThreadPoolThread *thread, allThreadsCopy) {
+ for (QThreadPoolThread *thread : qAsConst(allThreadsCopy)) {
thread->runnableReady.wakeAll();
thread->wait();
delete thread;