| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qwaitcondition_unix.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 29 | ** | - | ||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include "qplatformdefs.h" | - | ||||||||||||
| 35 | #include "qwaitcondition.h" | - | ||||||||||||
| 36 | #include "qmutex.h" | - | ||||||||||||
| 37 | #include "qreadwritelock.h" | - | ||||||||||||
| 38 | #include "qatomic.h" | - | ||||||||||||
| 39 | #include "qstring.h" | - | ||||||||||||
| 40 | #include "qelapsedtimer.h" | - | ||||||||||||
| 41 | #include "private/qcore_unix_p.h" | - | ||||||||||||
| 42 | - | |||||||||||||
| 43 | #include "qmutex_p.h" | - | ||||||||||||
| 44 | #include "qreadwritelock_p.h" | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | #include <errno.h> | - | ||||||||||||
| 47 | #include <sys/time.h> | - | ||||||||||||
| 48 | #include <time.h> | - | ||||||||||||
| 49 | - | |||||||||||||
| 50 | #ifndef QT_NO_THREAD | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 53 | - | |||||||||||||
| 54 | #ifdef Q_OS_ANDROID | - | ||||||||||||
| 55 | // pthread_condattr_setclock is available only since Android 5.0. On older versions, there's | - | ||||||||||||
| 56 | // a private function for relative waits (hidden in 5.0). | - | ||||||||||||
| 57 | // Use weakref so we can determine at runtime whether each of them is present. | - | ||||||||||||
| 58 | static int local_condattr_setclock(pthread_condattr_t*, clockid_t) | - | ||||||||||||
| 59 | __attribute__((weakref("pthread_condattr_setclock"))); | - | ||||||||||||
| 60 | - | |||||||||||||
| 61 | static int local_cond_timedwait_relative(pthread_cond_t*, pthread_mutex_t *, const timespec *) | - | ||||||||||||
| 62 | __attribute__((weakref("__pthread_cond_timedwait_relative"))); | - | ||||||||||||
| 63 | #endif | - | ||||||||||||
| 64 | - | |||||||||||||
| 65 | static void report_error(int code, const char *where, const char *what) | - | ||||||||||||
| 66 | { | - | ||||||||||||
| 67 | if (code != 0)
| 0-48920372 | ||||||||||||
| 68 | qWarning("%s: %s failure: %s", where, what, qPrintable(qt_error_string(code))); never executed: QMessageLogger(__FILE__, 68, __PRETTY_FUNCTION__).warning("%s: %s failure: %s", where, what, QString(qt_error_string(code)).toLocal8Bit().constData()); | 0 | ||||||||||||
| 69 | } executed 48920372 times by 1080 tests: end of blockExecuted by:
| 48920372 | ||||||||||||
| 70 | - | |||||||||||||
| 71 | void qt_initialize_pthread_cond(pthread_cond_t *cond, const char *where) | - | ||||||||||||
| 72 | { | - | ||||||||||||
| 73 | pthread_condattr_t condattr; | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | pthread_condattr_init(&condattr); | - | ||||||||||||
| 76 | #if (_POSIX_MONOTONIC_CLOCK-0 >= 0) | - | ||||||||||||
| 77 | #if defined(Q_OS_ANDROID) | - | ||||||||||||
| 78 | if (local_condattr_setclock && QElapsedTimer::clockType() == QElapsedTimer::MonotonicClock) | - | ||||||||||||
| 79 | local_condattr_setclock(&condattr, CLOCK_MONOTONIC); | - | ||||||||||||
| 80 | #elif !defined(Q_OS_MAC) | - | ||||||||||||
| 81 | if (QElapsedTimer::clockType() == QElapsedTimer::MonotonicClock)
| 0-2255245 | ||||||||||||
| 82 | pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC); executed 2255245 times by 937 tests: pthread_condattr_setclock(&condattr, 1);Executed by:
| 2255245 | ||||||||||||
| 83 | #endif | - | ||||||||||||
| 84 | #endif | - | ||||||||||||
| 85 | report_error(pthread_cond_init(cond, &condattr), where, "cv init"); | - | ||||||||||||
| 86 | pthread_condattr_destroy(&condattr); | - | ||||||||||||
| 87 | } executed 2255245 times by 937 tests: end of blockExecuted by:
| 2255245 | ||||||||||||
| 88 | - | |||||||||||||
| 89 | void qt_abstime_for_timeout(timespec *ts, int timeout) | - | ||||||||||||
| 90 | { | - | ||||||||||||
| 91 | #ifdef Q_OS_MAC | - | ||||||||||||
| 92 | // on Mac, qt_gettime() (on qelapsedtimer_mac.cpp) returns ticks related to the Mach absolute time | - | ||||||||||||
| 93 | // that doesn't work with pthread | - | ||||||||||||
| 94 | // Mac also doesn't have clock_gettime | - | ||||||||||||
| 95 | struct timeval tv; | - | ||||||||||||
| 96 | gettimeofday(&tv, 0); | - | ||||||||||||
| 97 | ts->tv_sec = tv.tv_sec; | - | ||||||||||||
| 98 | ts->tv_nsec = tv.tv_usec * 1000; | - | ||||||||||||
| 99 | #else | - | ||||||||||||
| 100 | *ts = qt_gettime(); | - | ||||||||||||
| 101 | #endif | - | ||||||||||||
| 102 | - | |||||||||||||
| 103 | ts->tv_sec += timeout / 1000; | - | ||||||||||||
| 104 | ts->tv_nsec += timeout % 1000 * Q_UINT64_C(1000) * 1000; | - | ||||||||||||
| 105 | normalizedTimespec(*ts); | - | ||||||||||||
| 106 | } executed 3091425 times by 441 tests: end of blockExecuted by:
| 3091425 | ||||||||||||
| 107 | - | |||||||||||||
| 108 | class QWaitConditionPrivate { | - | ||||||||||||
| 109 | public: | - | ||||||||||||
| 110 | pthread_mutex_t mutex; | - | ||||||||||||
| 111 | pthread_cond_t cond; | - | ||||||||||||
| 112 | int waiters; | - | ||||||||||||
| 113 | int wakeups; | - | ||||||||||||
| 114 | - | |||||||||||||
| 115 | int wait_relative(unsigned long time) | - | ||||||||||||
| 116 | { | - | ||||||||||||
| 117 | timespec ti; | - | ||||||||||||
| 118 | #ifdef Q_OS_ANDROID | - | ||||||||||||
| 119 | if (local_cond_timedwait_relative) { | - | ||||||||||||
| 120 | ti.tv_sec = time / 1000; | - | ||||||||||||
| 121 | ti.tv_nsec = time % 1000 * Q_UINT64_C(1000) * 1000; | - | ||||||||||||
| 122 | return local_cond_timedwait_relative(&cond, &mutex, &ti); | - | ||||||||||||
| 123 | } | - | ||||||||||||
| 124 | #endif | - | ||||||||||||
| 125 | qt_abstime_for_timeout(&ti, time); | - | ||||||||||||
| 126 | return pthread_cond_timedwait(&cond, &mutex, &ti); executed 3091425 times by 441 tests: return pthread_cond_timedwait(&cond, &mutex, &ti);Executed by:
| 3091425 | ||||||||||||
| 127 | } | - | ||||||||||||
| 128 | - | |||||||||||||
| 129 | bool wait(unsigned long time) | - | ||||||||||||
| 130 | { | - | ||||||||||||
| 131 | int code; | - | ||||||||||||
| 132 | forever { | - | ||||||||||||
| 133 | if (time != ULONG_MAX) {
| 984271-3091425 | ||||||||||||
| 134 | code = wait_relative(time); | - | ||||||||||||
| 135 | } else { executed 3091401 times by 504 tests: end of blockExecuted by:
| 3091401 | ||||||||||||
| 136 | code = pthread_cond_wait(&cond, &mutex); | - | ||||||||||||
| 137 | } executed 984243 times by 1079 tests: end of blockExecuted by:
| 984243 | ||||||||||||
| 138 | if (code == 0 && wakeups == 0) {
| 0-4075317 | ||||||||||||
| 139 | // many vendors warn of spurious wakeups from | - | ||||||||||||
| 140 | // pthread_cond_wait(), especially after signal delivery, | - | ||||||||||||
| 141 | // even though POSIX doesn't allow for it... sigh | - | ||||||||||||
| 142 | continue; never executed: continue; | 0 | ||||||||||||
| 143 | } | - | ||||||||||||
| 144 | break; executed 4075644 times by 1079 tests: break;Executed by:
| 4075644 | ||||||||||||
| 145 | } | - | ||||||||||||
| 146 | - | |||||||||||||
| 147 | Q_ASSERT_X(waiters > 0, "QWaitCondition::wait", "internal error (waiters)"); | - | ||||||||||||
| 148 | --waiters; | - | ||||||||||||
| 149 | if (code == 0) {
| 327-4075317 | ||||||||||||
| 150 | Q_ASSERT_X(wakeups > 0, "QWaitCondition::wait", "internal error (wakeups)"); | - | ||||||||||||
| 151 | --wakeups; | - | ||||||||||||
| 152 | } executed 4075317 times by 1079 tests: end of blockExecuted by:
| 4075317 | ||||||||||||
| 153 | report_error(pthread_mutex_unlock(&mutex), "QWaitCondition::wait()", "mutex unlock"); | - | ||||||||||||
| 154 | - | |||||||||||||
| 155 | if (code && code != ETIMEDOUT)
| 0-4075317 | ||||||||||||
| 156 | report_error(code, "QWaitCondition::wait()", "cv wait"); never executed: report_error(code, "QWaitCondition::wait()", "cv wait"); | 0 | ||||||||||||
| 157 | - | |||||||||||||
| 158 | return (code == 0); executed 4075644 times by 1079 tests: return (code == 0);Executed by:
| 4075644 | ||||||||||||
| 159 | } | - | ||||||||||||
| 160 | }; | - | ||||||||||||
| 161 | - | |||||||||||||
| 162 | - | |||||||||||||
| 163 | QWaitCondition::QWaitCondition() | - | ||||||||||||
| 164 | { | - | ||||||||||||
| 165 | d = new QWaitConditionPrivate; | - | ||||||||||||
| 166 | report_error(pthread_mutex_init(&d->mutex, NULL), "QWaitCondition", "mutex init"); | - | ||||||||||||
| 167 | qt_initialize_pthread_cond(&d->cond, "QWaitCondition"); | - | ||||||||||||
| 168 | d->waiters = d->wakeups = 0; | - | ||||||||||||
| 169 | } executed 2255245 times by 937 tests: end of blockExecuted by:
| 2255245 | ||||||||||||
| 170 | - | |||||||||||||
| 171 | - | |||||||||||||
| 172 | QWaitCondition::~QWaitCondition() | - | ||||||||||||
| 173 | { | - | ||||||||||||
| 174 | report_error(pthread_cond_destroy(&d->cond), "QWaitCondition", "cv destroy"); | - | ||||||||||||
| 175 | report_error(pthread_mutex_destroy(&d->mutex), "QWaitCondition", "mutex destroy"); | - | ||||||||||||
| 176 | delete d; | - | ||||||||||||
| 177 | } executed 2257432 times by 755 tests: end of blockExecuted by:
| 2257432 | ||||||||||||
| 178 | - | |||||||||||||
| 179 | void QWaitCondition::wakeOne() | - | ||||||||||||
| 180 | { | - | ||||||||||||
| 181 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeOne()", "mutex lock"); | - | ||||||||||||
| 182 | d->wakeups = qMin(d->wakeups + 1, d->waiters); | - | ||||||||||||
| 183 | report_error(pthread_cond_signal(&d->cond), "QWaitCondition::wakeOne()", "cv signal"); | - | ||||||||||||
| 184 | report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeOne()", "mutex unlock"); | - | ||||||||||||
| 185 | } executed 2220126 times by 25 tests: end of blockExecuted by:
| 2220126 | ||||||||||||
| 186 | - | |||||||||||||
| 187 | void QWaitCondition::wakeAll() | - | ||||||||||||
| 188 | { | - | ||||||||||||
| 189 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeAll()", "mutex lock"); | - | ||||||||||||
| 190 | d->wakeups = d->waiters; | - | ||||||||||||
| 191 | report_error(pthread_cond_broadcast(&d->cond), "QWaitCondition::wakeAll()", "cv broadcast"); | - | ||||||||||||
| 192 | report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeAll()", "mutex unlock"); | - | ||||||||||||
| 193 | } executed 8361100 times by 1079 tests: end of blockExecuted by:
| 8361100 | ||||||||||||
| 194 | - | |||||||||||||
| 195 | bool QWaitCondition::wait(QMutex *mutex, unsigned long time) | - | ||||||||||||
| 196 | { | - | ||||||||||||
| 197 | if (! mutex)
| 0-4075422 | ||||||||||||
| 198 | return false; never executed: return false; | 0 | ||||||||||||
| 199 | if (mutex->isRecursive()) {
| 0-4075422 | ||||||||||||
| 200 | qWarning("QWaitCondition: cannot wait on recursive mutexes"); | - | ||||||||||||
| 201 | return false; never executed: return false; | 0 | ||||||||||||
| 202 | } | - | ||||||||||||
| 203 | - | |||||||||||||
| 204 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wait()", "mutex lock"); | - | ||||||||||||
| 205 | ++d->waiters; | - | ||||||||||||
| 206 | mutex->unlock(); | - | ||||||||||||
| 207 | - | |||||||||||||
| 208 | bool returnValue = d->wait(time); | - | ||||||||||||
| 209 | - | |||||||||||||
| 210 | mutex->lock(); | - | ||||||||||||
| 211 | - | |||||||||||||
| 212 | return returnValue; executed 4075370 times by 1079 tests: return returnValue;Executed by:
| 4075370 | ||||||||||||
| 213 | } | - | ||||||||||||
| 214 | - | |||||||||||||
| 215 | bool QWaitCondition::wait(QReadWriteLock *readWriteLock, unsigned long time) | - | ||||||||||||
| 216 | { | - | ||||||||||||
| 217 | if (!readWriteLock || readWriteLock->d->accessCount == 0)
| 0-274 | ||||||||||||
| 218 | return false; never executed: return false; | 0 | ||||||||||||
| 219 | if (readWriteLock->d->accessCount < -1) {
| 0-274 | ||||||||||||
| 220 | qWarning("QWaitCondition: cannot wait on QReadWriteLocks with recursive lockForWrite()"); | - | ||||||||||||
| 221 | return false; never executed: return false; | 0 | ||||||||||||
| 222 | } | - | ||||||||||||
| 223 | - | |||||||||||||
| 224 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wait()", "mutex lock"); | - | ||||||||||||
| 225 | ++d->waiters; | - | ||||||||||||
| 226 | - | |||||||||||||
| 227 | int previousAccessCount = readWriteLock->d->accessCount; | - | ||||||||||||
| 228 | readWriteLock->unlock(); | - | ||||||||||||
| 229 | - | |||||||||||||
| 230 | bool returnValue = d->wait(time); | - | ||||||||||||
| 231 | - | |||||||||||||
| 232 | if (previousAccessCount < 0)
| 43-231 | ||||||||||||
| 233 | readWriteLock->lockForWrite(); executed 231 times by 1 test: readWriteLock->lockForWrite();Executed by:
| 231 | ||||||||||||
| 234 | else | - | ||||||||||||
| 235 | readWriteLock->lockForRead(); executed 43 times by 1 test: readWriteLock->lockForRead();Executed by:
| 43 | ||||||||||||
| 236 | - | |||||||||||||
| 237 | return returnValue; executed 274 times by 1 test: return returnValue;Executed by:
| 274 | ||||||||||||
| 238 | } | - | ||||||||||||
| 239 | - | |||||||||||||
| 240 | QT_END_NAMESPACE | - | ||||||||||||
| 241 | - | |||||||||||||
| 242 | #endif // QT_NO_THREAD | - | ||||||||||||
| Source code | Switch to Preprocessed file |