| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qwaitcondition_unix.cpp |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||||||||
| 2 | - | |||||||||||||||||||
| 3 | - | |||||||||||||||||||
| 4 | - | |||||||||||||||||||
| 5 | - | |||||||||||||||||||
| 6 | - | |||||||||||||||||||
| 7 | - | |||||||||||||||||||
| 8 | - | |||||||||||||||||||
| 9 | static void report_error(int code, const char *where, const char *what) | - | ||||||||||||||||||
| 10 | { | - | ||||||||||||||||||
| 11 | if (code != 0) | - | ||||||||||||||||||
| 12 | QMessageLogger(__FILE__, 6875, __PRETTY_FUNCTION__).warning("%s: %s failure: %s", where, what, QString(qt_error_string(code)).toLocal8Bit().constData()); | - | ||||||||||||||||||
| 13 | } | - | ||||||||||||||||||
| 14 | - | |||||||||||||||||||
| 15 | void qt_initialize_pthread_cond(pthread_cond_t *cond, const char *where) | - | ||||||||||||||||||
| 16 | { | - | ||||||||||||||||||
| 17 | pthread_condattr_t condattr; | - | ||||||||||||||||||
| 18 | - | |||||||||||||||||||
| 19 | pthread_condattr_init(&condattr); | - | ||||||||||||||||||
| 20 | - | |||||||||||||||||||
| 21 | - | |||||||||||||||||||
| 22 | - | |||||||||||||||||||
| 23 | - | |||||||||||||||||||
| 24 | - | |||||||||||||||||||
| 25 | if (QElapsedTimer::clockType() == QElapsedTimer::MonotonicClock) | - | ||||||||||||||||||
| 26 | pthread_condattr_setclock(&condattr, 1); | - | ||||||||||||||||||
| 27 | - | |||||||||||||||||||
| 28 | - | |||||||||||||||||||
| 29 | report_error(pthread_cond_init(cond, &condattr), where, "cv init"); | - | ||||||||||||||||||
| 30 | pthread_condattr_destroy(&condattr); | - | ||||||||||||||||||
| 31 | } | - | ||||||||||||||||||
| 32 | - | |||||||||||||||||||
| 33 | void qt_abstime_for_timeout(timespec *ts, int timeout) | - | ||||||||||||||||||
| 34 | { | - | ||||||||||||||||||
| 35 | *ts = qt_gettime(); | - | ||||||||||||||||||
| 36 | - | |||||||||||||||||||
| 37 | - | |||||||||||||||||||
| 38 | ts->tv_sec += timeout / 1000; | - | ||||||||||||||||||
| 39 | ts->tv_nsec += timeout % 1000 * static_cast<unsigned long long>(1000ULL) * 1000; | - | ||||||||||||||||||
| 40 | normalizedTimespec(*ts); | - | ||||||||||||||||||
| 41 | } | - | ||||||||||||||||||
| 42 | - | |||||||||||||||||||
| 43 | class QWaitConditionPrivate { | - | ||||||||||||||||||
| 44 | public: | - | ||||||||||||||||||
| 45 | pthread_mutex_t mutex; | - | ||||||||||||||||||
| 46 | pthread_cond_t cond; | - | ||||||||||||||||||
| 47 | int waiters; | - | ||||||||||||||||||
| 48 | int wakeups; | - | ||||||||||||||||||
| 49 | - | |||||||||||||||||||
| 50 | int wait_relative(unsigned long time) | - | ||||||||||||||||||
| 51 | { | - | ||||||||||||||||||
| 52 | timespec ti; | - | ||||||||||||||||||
| 53 | - | |||||||||||||||||||
| 54 | - | |||||||||||||||||||
| 55 | - | |||||||||||||||||||
| 56 | - | |||||||||||||||||||
| 57 | - | |||||||||||||||||||
| 58 | - | |||||||||||||||||||
| 59 | - | |||||||||||||||||||
| 60 | qt_abstime_for_timeout(&ti, time); | - | ||||||||||||||||||
| 61 | return pthread_cond_timedwait(&cond, &mutex, &ti); | - | ||||||||||||||||||
| 62 | } | - | ||||||||||||||||||
| 63 | - | |||||||||||||||||||
| 64 | bool wait(unsigned long time) | - | ||||||||||||||||||
| 65 | { | - | ||||||||||||||||||
| 66 | int code; | - | ||||||||||||||||||
| 67 | for(;;) { | - | ||||||||||||||||||
| 68 | if (time != (9223372036854775807L * 2UL + 1UL)) { | - | ||||||||||||||||||
| 69 | code = wait_relative(time); | - | ||||||||||||||||||
| 70 | } else { | - | ||||||||||||||||||
| 71 | code = pthread_cond_wait(&cond, &mutex); | - | ||||||||||||||||||
| 72 | } | - | ||||||||||||||||||
| 73 | if (code == 0 && wakeups == 0) { | - | ||||||||||||||||||
| 74 | - | |||||||||||||||||||
| 75 | - | |||||||||||||||||||
| 76 | - | |||||||||||||||||||
| 77 | continue; | - | ||||||||||||||||||
| 78 | } | - | ||||||||||||||||||
| 79 | break; | - | ||||||||||||||||||
| 80 | } | - | ||||||||||||||||||
| 81 | - | |||||||||||||||||||
| 82 | ((!(waiters > 0)) ? qt_assert_x("QWaitCondition::wait", "internal error (waiters)",__FILE__,147154) : qt_noop()); | - | ||||||||||||||||||
| 83 | --waiters; | - | ||||||||||||||||||
| 84 | if (code == 0) { | - | ||||||||||||||||||
| 85 | ((!(wakeups > 0)) ? qt_assert_x("QWaitCondition::wait", "internal error (wakeups)",__FILE__,150157) : qt_noop()); | - | ||||||||||||||||||
| 86 | --wakeups; | - | ||||||||||||||||||
| 87 | } | - | ||||||||||||||||||
| 88 | report_error(pthread_mutex_unlock(&mutex), "QWaitCondition::wait()", "mutex unlock"); | - | ||||||||||||||||||
| 89 | - | |||||||||||||||||||
| 90 | if (code && code != 110) | - | ||||||||||||||||||
| 91 | report_error(code, "QWaitCondition::wait()", "cv wait"); | - | ||||||||||||||||||
| 92 | - | |||||||||||||||||||
| 93 | return (code == 0); | - | ||||||||||||||||||
| 94 | } | - | ||||||||||||||||||
| 95 | }; | - | ||||||||||||||||||
| 96 | - | |||||||||||||||||||
| 97 | - | |||||||||||||||||||
| 98 | QWaitCondition::QWaitCondition() | - | ||||||||||||||||||
| 99 | { | - | ||||||||||||||||||
| 100 | d = new QWaitConditionPrivate; | - | ||||||||||||||||||
| 101 | report_error(pthread_mutex_init(&d->mutex, __null), "QWaitCondition", "mutex init"); | - | ||||||||||||||||||
| 102 | qt_initialize_pthread_cond(&d->cond, "QWaitCondition"); | - | ||||||||||||||||||
| 103 | d->waiters = d->wakeups = 0; | - | ||||||||||||||||||
| 104 | } | - | ||||||||||||||||||
| 105 | - | |||||||||||||||||||
| 106 | - | |||||||||||||||||||
| 107 | QWaitCondition::~QWaitCondition() | - | ||||||||||||||||||
| 108 | { | - | ||||||||||||||||||
| 109 | report_error(pthread_cond_destroy(&d->cond), "QWaitCondition", "cv destroy"); | - | ||||||||||||||||||
| 110 | report_error(pthread_mutex_destroy(&d->mutex), "QWaitCondition", "mutex destroy"); | - | ||||||||||||||||||
| 111 | delete d; | - | ||||||||||||||||||
| 112 | } | - | ||||||||||||||||||
| 113 | - | |||||||||||||||||||
| 114 | void QWaitCondition::wakeOne() | - | ||||||||||||||||||
| 115 | { | - | ||||||||||||||||||
| 116 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeOne()", "mutex lock"); | - | ||||||||||||||||||
| 117 | d->wakeups = qMin(d->wakeups + 1, d->waiters); | - | ||||||||||||||||||
| 118 | report_error(pthread_cond_signal(&d->cond), "QWaitCondition::wakeOne()", "cv signal"); | - | ||||||||||||||||||
| 119 | report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeOne()", "mutex unlock"); | - | ||||||||||||||||||
| 120 | } | - | ||||||||||||||||||
| 121 | - | |||||||||||||||||||
| 122 | void QWaitCondition::wakeAll() | - | ||||||||||||||||||
| 123 | { | - | ||||||||||||||||||
| 124 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wakeAll()", "mutex lock"); | - | ||||||||||||||||||
| 125 | d->wakeups = d->waiters; | - | ||||||||||||||||||
| 126 | report_error(pthread_cond_broadcast(&d->cond), "QWaitCondition::wakeAll()", "cv broadcast"); | - | ||||||||||||||||||
| 127 | report_error(pthread_mutex_unlock(&d->mutex), "QWaitCondition::wakeAll()", "mutex unlock"); | - | ||||||||||||||||||
| 128 | } | - | ||||||||||||||||||
| 129 | - | |||||||||||||||||||
| 130 | bool QWaitCondition::wait(QMutex *mutex, unsigned long time) | - | ||||||||||||||||||
| 131 | { | - | ||||||||||||||||||
| 132 | if (! mutex) | - | ||||||||||||||||||
| 133 | return false; | - | ||||||||||||||||||
| 134 | if (mutex->isRecursive()) { | - | ||||||||||||||||||
| 135 | QMessageLogger(__FILE__, 200207, __PRETTY_FUNCTION__).warning("QWaitCondition: cannot wait on recursive mutexes"); | - | ||||||||||||||||||
| 136 | return false; | - | ||||||||||||||||||
| 137 | } | - | ||||||||||||||||||
| 138 | - | |||||||||||||||||||
| 139 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wait()", "mutex lock"); | - | ||||||||||||||||||
| 140 | ++d->waiters; | - | ||||||||||||||||||
| 141 | mutex->unlock(); | - | ||||||||||||||||||
| 142 | - | |||||||||||||||||||
| 143 | bool returnValue = d->wait(time); | - | ||||||||||||||||||
| 144 | - | |||||||||||||||||||
| 145 | mutex->lock(); | - | ||||||||||||||||||
| 146 | - | |||||||||||||||||||
| 147 | return returnValue; | - | ||||||||||||||||||
| 148 | } | - | ||||||||||||||||||
| 149 | - | |||||||||||||||||||
| 150 | bool QWaitCondition::wait(QReadWriteLock *readWriteLock, unsigned long time) | - | ||||||||||||||||||
| 151 | { | - | ||||||||||||||||||
| 152 | if (!readWriteLock||
| 0-274 | ||||||||||||||||||
| 153 | return never executed: false;return false;never executed: return false; | 0 | ||||||||||||||||||
| 154 | auto previousState = readWriteLock->d->accessCountstateForWaitCondition(); | - | ||||||||||||||||||
| 155 | if (previousState
| 0-274 | ||||||||||||||||||
| 156 | return never executed: false;return false;never executed: return false; | 0 | ||||||||||||||||||
| 157 | if (readWriteLock->d->accessCount < -1previousState == QReadWriteLock::RecursivelyLocked
| 0-274 | ||||||||||||||||||
| 158 | QMessageLogger(__FILE__, 220230, __PRETTY_FUNCTION__).warning("QWaitCondition: cannot wait on QReadWriteLocks with recursive lockForWrite()"); | - | ||||||||||||||||||
| 159 | return never executed: false;return false;never executed: return false; | 0 | ||||||||||||||||||
| 160 | } | - | ||||||||||||||||||
| 161 | - | |||||||||||||||||||
| 162 | report_error(pthread_mutex_lock(&d->mutex), "QWaitCondition::wait()", "mutex lock"); | - | ||||||||||||||||||
| 163 | ++d->waiters; | - | ||||||||||||||||||
| int previousAccessCount = readWriteLock->d->accessCount; | ||||||||||||||||||||
| 164 | - | |||||||||||||||||||
| 165 | readWriteLock->unlock(); | - | ||||||||||||||||||
| 166 | - | |||||||||||||||||||
| 167 | bool returnValue = d->wait(time); | - | ||||||||||||||||||
| 168 | - | |||||||||||||||||||
| 169 | if (previousAccessCount < 0previousState == QReadWriteLock::LockedForWrite
| 43-231 | ||||||||||||||||||
| 170 | readWriteLock->lockForWrite(); executed 231 times by 1 test: readWriteLock->lockForWrite();Executed by:
| 231 | ||||||||||||||||||
| 171 | else | - | ||||||||||||||||||
| 172 | readWriteLock->lockForRead(); executed 43 times by 1 test: readWriteLock->lockForRead();Executed by:
| 43 | ||||||||||||||||||
| 173 | - | |||||||||||||||||||
| 174 | return executed 274 times by 1 test: returnValue;return returnValue;Executed by:
executed 274 times by 1 test: return returnValue;Executed by:
| 274 | ||||||||||||||||||
| 175 | } | - | ||||||||||||||||||
| 176 | - | |||||||||||||||||||
| 177 | - | |||||||||||||||||||
| Switch to Source code | Preprocessed file |