| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Intel Corporation. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #include "qplatformdefs.h" |
| 42 | #include "qwaitcondition.h" |
| 43 | #include "qmutex.h" |
| 44 | #include "qreadwritelock.h" |
| 45 | #include "qatomic.h" |
| 46 | #include "qstring.h" |
| 47 | #include "qdeadlinetimer.h" |
| 48 | #include "private/qdeadlinetimer_p.h" |
| 49 | #include "qelapsedtimer.h" |
| 50 | #include "private/qcore_unix_p.h" |
| 51 | |
| 52 | #include "qmutex_p.h" |
| 53 | #include "qreadwritelock_p.h" |
| 54 | |
| 55 | #include <errno.h> |
| 56 | #include <sys/time.h> |
| 57 | #include <time.h> |
| 58 | |
| 59 | QT_BEGIN_NAMESPACE |
| 60 | |
| 61 | #ifdef Q_OS_ANDROID |
| 62 | // pthread_condattr_setclock is available only since Android 5.0. On older versions, there's |
| 63 | // a private function for relative waits (hidden in 5.0). |
| 64 | // Use weakref so we can determine at runtime whether each of them is present. |
| 65 | static int local_condattr_setclock(pthread_condattr_t*, clockid_t) |
| 66 | __attribute__((weakref("pthread_condattr_setclock" ))); |
| 67 | |
| 68 | static int local_cond_timedwait_relative(pthread_cond_t*, pthread_mutex_t *, const timespec *) |
| 69 | __attribute__((weakref("__pthread_cond_timedwait_relative" ))); |
| 70 | #endif |
| 71 | |
| 72 | static void report_error(int code, const char *where, const char *what) |
| 73 | { |
| 74 | if (code != 0) |
| 75 | qErrnoWarning(code, "%s: %s failure" , where, what); |
| 76 | } |
| 77 | |
| 78 | void qt_initialize_pthread_cond(pthread_cond_t *cond, const char *where) |
| 79 | { |
| 80 | pthread_condattr_t condattr; |
| 81 | |
| 82 | pthread_condattr_init(&condattr); |
| 83 | #if (_POSIX_MONOTONIC_CLOCK-0 >= 0) |
| 84 | #if defined(Q_OS_ANDROID) |
| 85 | if (local_condattr_setclock && QElapsedTimer::clockType() == QElapsedTimer::MonotonicClock) |
| 86 | local_condattr_setclock(&condattr, CLOCK_MONOTONIC); |
| 87 | #elif !defined(Q_OS_MAC) |
| 88 | if (QElapsedTimer::clockType() == QElapsedTimer::MonotonicClock) |
| 89 | pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC); |
| 90 | #endif |
| 91 | #endif |
| 92 | report_error(pthread_cond_init(cond, &condattr), where, "cv init" ); |
| 93 | pthread_condattr_destroy(&condattr); |
| 94 | } |
| 95 | |
| 96 | void qt_abstime_for_timeout(timespec *ts, QDeadlineTimer deadline) |
| 97 | { |
| 98 | #ifdef Q_OS_MAC |
| 99 | // on Mac, qt_gettime() (on qelapsedtimer_mac.cpp) returns ticks related to the Mach absolute time |
| 100 | // that doesn't work with pthread |
| 101 | // Mac also doesn't have clock_gettime |
| 102 | struct timeval tv; |
| 103 | qint64 nsec = deadline.remainingTimeNSecs(); |
| 104 | gettimeofday(&tv, 0); |
| 105 | ts->tv_sec = tv.tv_sec + nsec / (1000 * 1000 * 1000); |
| 106 | ts->tv_nsec = tv.tv_usec * 1000 + nsec % (1000 * 1000 * 1000); |
| 107 | |
| 108 | normalizedTimespec(*ts); |
| 109 | #else |
| 110 | // depends on QDeadlineTimer's internals!! |
| 111 | static_assert(QDeadlineTimerNanosecondsInT2); |
| 112 | ts->tv_sec = deadline._q_data().first; |
| 113 | ts->tv_nsec = deadline._q_data().second; |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | class QWaitConditionPrivate |
| 118 | { |
| 119 | public: |
| 120 | pthread_mutex_t mutex; |
| 121 | pthread_cond_t cond; |
| 122 | int waiters; |
| 123 | int wakeups; |
| 124 | |
| 125 | int wait_relative(QDeadlineTimer deadline) |
| 126 | { |
| 127 | timespec ti; |
| 128 | #ifdef Q_OS_ANDROID |
| 129 | if (!local_condattr_setclock && local_cond_timedwait_relative) { |
| 130 | qint64 nsec = deadline.remainingTimeNSecs(); |
| 131 | ti.tv_sec = nsec / (1000 * 1000 * 1000); |
| 132 | ti.tv_nsec = nsec - ti.tv_sec * 1000 * 1000 * 1000; |
| 133 | return local_cond_timedwait_relative(&cond, &mutex, &ti); |
| 134 | } |
| 135 | #endif |
| 136 | qt_abstime_for_timeout(&ti, deadline); |
| 137 | return pthread_cond_timedwait(&cond, &mutex, &ti); |
| 138 | } |
| 139 | |
| 140 | bool wait(QDeadlineTimer deadline) |
| 141 | { |
| 142 | int code; |
| 143 | forever { |
| 144 | if (!deadline.isForever()) { |
| 145 | code = wait_relative(deadline); |
| 146 | } else { |
| 147 | code = pthread_cond_wait(&cond, &mutex); |
| 148 | } |
| 149 | if (code == 0 && wakeups == 0) { |
| 150 | // many vendors warn of spurious wakeups from |
| 151 | // pthread_cond_wait(), especially after signal delivery, |
| 152 | // even though POSIX doesn't allow for it... sigh |
| 153 | continue; |
| 154 | } |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | Q_ASSERT_X(waiters > 0, "QWaitCondition::wait" , "internal error (waiters)" ); |
| 159 | --waiters; |
| 160 | if (code == 0) { |
| 161 | Q_ASSERT_X(wakeups > 0, "QWaitCondition::wait" , "internal error (wakeups)" ); |
| 162 | --wakeups; |
| 163 | } |
| 164 | report_error(pthread_mutex_unlock(&mutex), "QWaitCondition::wait()" , "mutex unlock" ); |
| 165 | |
| 166 | if (code && code != ETIMEDOUT) |
| 167 | report_error(code, "QWaitCondition::wait()" , "cv wait" ); |
| 168 | |
| 169 | return (code == 0); |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | QWaitCondition::QWaitCondition() |
| 174 | { |
| 175 | d = new QWaitConditionPrivate; |
| 176 | report_error(pthread_mutex_init(&d->mutex, nullptr), "QWaitCondition" , "mutex init" ); |
| 177 | qt_initialize_pthread_cond(&d->cond, "QWaitCondition" ); |
| 178 | d->waiters = d->wakeups = 0; |
| 179 | } |
| 180 | |
| 181 | QWaitCondition::~QWaitCondition() |
| 182 | { |
| 183 | report_error(pthread_cond_destroy(&d->cond), "QWaitCondition" , "cv destroy" ); |
| 184 | report_error(pthread_mutex_destroy(&d->mutex), "QWaitCondition" , "mutex destroy" ); |
| 185 | delete d; |
| 186 | } |
| 187 | |
| 188 | void QWaitCondition::wakeOne() |
| 189 | { |
| 190 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeOne()" , "mutex lock" ); |
| 191 | d->wakeups = qMin(d->wakeups + 1, d->waiters); |
| 192 | report_error(pthread_cond_signal(&d->cond), "QWaitCondition::wakeOne()" , "cv signal" ); |
| 193 | report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeOne()" , "mutex unlock" ); |
| 194 | } |
| 195 | |
| 196 | void QWaitCondition::wakeAll() |
| 197 | { |
| 198 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeAll()" , "mutex lock" ); |
| 199 | d->wakeups = d->waiters; |
| 200 | report_error(pthread_cond_broadcast(&d->cond), "QWaitCondition::wakeAll()" , "cv broadcast" ); |
| 201 | report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeAll()" , "mutex unlock" ); |
| 202 | } |
| 203 | |
| 204 | bool QWaitCondition::wait(QMutex *mutex, unsigned long time) |
| 205 | { |
| 206 | if (time == std::numeric_limits<unsigned long>::max()) |
| 207 | return wait(mutex, QDeadlineTimer(QDeadlineTimer::Forever)); |
| 208 | return wait(mutex, QDeadlineTimer(time)); |
| 209 | } |
| 210 | |
| 211 | bool QWaitCondition::wait(QMutex *mutex, QDeadlineTimer deadline) |
| 212 | { |
| 213 | if (!mutex) |
| 214 | return false; |
| 215 | |
| 216 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wait()" , "mutex lock" ); |
| 217 | ++d->waiters; |
| 218 | mutex->unlock(); |
| 219 | |
| 220 | bool returnValue = d->wait(deadline); |
| 221 | |
| 222 | mutex->lock(); |
| 223 | |
| 224 | return returnValue; |
| 225 | } |
| 226 | |
| 227 | bool QWaitCondition::wait(QReadWriteLock *readWriteLock, unsigned long time) |
| 228 | { |
| 229 | if (time == std::numeric_limits<unsigned long>::max()) |
| 230 | return wait(readWriteLock, QDeadlineTimer(QDeadlineTimer::Forever)); |
| 231 | return wait(readWriteLock, QDeadlineTimer(time)); |
| 232 | } |
| 233 | |
| 234 | bool QWaitCondition::wait(QReadWriteLock *readWriteLock, QDeadlineTimer deadline) |
| 235 | { |
| 236 | if (!readWriteLock) |
| 237 | return false; |
| 238 | auto previousState = readWriteLock->stateForWaitCondition(); |
| 239 | if (previousState == QReadWriteLock::Unlocked) |
| 240 | return false; |
| 241 | if (previousState == QReadWriteLock::RecursivelyLocked) { |
| 242 | qWarning("QWaitCondition: cannot wait on QReadWriteLocks with recursive lockForWrite()" ); |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wait()" , "mutex lock" ); |
| 247 | ++d->waiters; |
| 248 | |
| 249 | readWriteLock->unlock(); |
| 250 | |
| 251 | bool returnValue = d->wait(deadline); |
| 252 | |
| 253 | if (previousState == QReadWriteLock::LockedForWrite) |
| 254 | readWriteLock->lockForWrite(); |
| 255 | else |
| 256 | readWriteLock->lockForRead(); |
| 257 | |
| 258 | return returnValue; |
| 259 | } |
| 260 | |
| 261 | QT_END_NAMESPACE |
| 262 | |