| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qthread_unix.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 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 "qthread.h" | - | ||||||
| 42 | - | |||||||
| 43 | #include "qplatformdefs.h" | - | ||||||
| 44 | - | |||||||
| 45 | #include <private/qcoreapplication_p.h> | - | ||||||
| 46 | #include <private/qcore_unix_p.h> | - | ||||||
| 47 | - | |||||||
| 48 | #if defined(Q_OS_BLACKBERRY) | - | ||||||
| # include <private/qeventdispatcher_blackberry_p.h> | ||||||||
| #elifdefined(Q_OS_OSX) | ||||||||
| 49 | # include <private/qeventdispatcher_cf_p.h> | - | ||||||
| # include <private/qeventdispatcher_unix_p.h> | ||||||||
| 50 | #else | - | ||||||
| 51 | # if !defined(QT_NO_GLIB) | - | ||||||
| 52 | # include "../kernel/qeventdispatcher_glib_p.h" | - | ||||||
| 53 | # endif | - | ||||||
| # include#endif | ||||||||
| 55 | - | |||||||
| 56 | #include <private/qeventdispatcher_unix_p.h> | - | ||||||
| 57 | - | |||||||
| #endif#include "qthreadstorage.h" | ||||||||
| 59 | - | |||||||
| 60 | #include "qthread_p.h" | - | ||||||
| 61 | - | |||||||
| 62 | #include "qdebug.h" | - | ||||||
| 63 | - | |||||||
| 64 | #include <sched.h> | - | ||||||
| 65 | #include <errno.h> | - | ||||||
| 66 | - | |||||||
| 67 | #ifdef Q_OS_BSD4 | - | ||||||
| 68 | #include <sys/sysctl.h> | - | ||||||
| 69 | #endif | - | ||||||
| 70 | #ifdef Q_OS_VXWORKS | - | ||||||
| 71 | # if (_WRS_VXWORKS_MAJOR > 6) || ((_WRS_VXWORKS_MAJOR == 6) && (_WRS_VXWORKS_MINOR >= 6)) | - | ||||||
| 72 | # include <vxCpuLib.h> | - | ||||||
| 73 | # include <cpuset.h> | - | ||||||
| 74 | # define QT_VXWORKS_HAS_CPUSET | - | ||||||
| 75 | # endif | - | ||||||
| 76 | #endif | - | ||||||
| 77 | - | |||||||
| 78 | #ifdef Q_OS_HPUX | - | ||||||
| 79 | #include <sys/pstat.h> | - | ||||||
| 80 | #endif | - | ||||||
| 81 | - | |||||||
| 82 | #if defined(Q_OS_MAC) | - | ||||||
| 83 | # ifdef qDebug | - | ||||||
| 84 | # define old_qDebug qDebug | - | ||||||
| 85 | # undef qDebug | - | ||||||
| 86 | # endif | - | ||||||
| 87 | - | |||||||
| 88 | # ifdef old_qDebug | - | ||||||
| 89 | # undef qDebug | - | ||||||
| 90 | # define qDebug QT_NO_QDEBUG_MACRO | - | ||||||
| 91 | # undef old_qDebug | - | ||||||
| 92 | # endif | - | ||||||
| 93 | #endif | - | ||||||
| 94 | - | |||||||
| 95 | #if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) | - | ||||||
| 96 | #include <sys/prctl.h> | - | ||||||
| 97 | #endif | - | ||||||
| 98 | - | |||||||
| 99 | #if defined(Q_OS_LINUX) && !defined(SCHED_IDLE) | - | ||||||
| 100 | // from linux/sched.h | - | ||||||
| 101 | # define SCHED_IDLE 5 | - | ||||||
| 102 | #endif | - | ||||||
| 103 | - | |||||||
| 104 | #if defined(Q_OS_DARWIN) || !defined(Q_OS_OPENBSD) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) | - | ||||||
| 105 | #define QT_HAS_THREAD_PRIORITY_SCHEDULING | - | ||||||
| 106 | #endif | - | ||||||
| 107 | - | |||||||
| 108 | - | |||||||
| 109 | QT_BEGIN_NAMESPACE | - | ||||||
| 110 | - | |||||||
| 111 | #ifndef QT_NO_THREAD | - | ||||||
| 112 | - | |||||||
| 113 | Q_STATIC_ASSERT(sizeof(pthread_t) <= sizeof(Qt::HANDLE)); | - | ||||||
| 114 | - | |||||||
| 115 | enum { ThreadPriorityResetFlag = 0x80000000 }; | - | ||||||
| 116 | - | |||||||
| 117 | #if defined(Q_OS_LINUX) && defined(__GLIBC__) && (defined(Q_CC_GNU) || defined(Q_CC_INTEL)) && !defined(QT_LINUXBASE) | - | ||||||
| 118 | /* LSB doesn't have __thread, https://lsbbugs.linuxfoundation.org/show_bug.cgi?id=993 */ | - | ||||||
| 119 | #define HAVE_TLS | - | ||||||
| 120 | #endif | - | ||||||
| 121 | #if defined(Q_CC_XLC) || defined (Q_CC_SUN) | - | ||||||
| 122 | #define HAVE_TLS | - | ||||||
| 123 | #endif | - | ||||||
| 124 | - | |||||||
| 125 | #ifdef HAVE_TLS | - | ||||||
| 126 | static __thread QThreadData *currentThreadData = 0; | - | ||||||
| 127 | #endif | - | ||||||
| 128 | - | |||||||
| 129 | static pthread_once_t current_thread_data_once = PTHREAD_ONCE_INIT; | - | ||||||
| 130 | static pthread_key_t current_thread_data_key; | - | ||||||
| 131 | - | |||||||
| 132 | static void destroy_current_thread_data(void *p) | - | ||||||
| 133 | { | - | ||||||
| 134 | #if defined(Q_OS_VXWORKS) | - | ||||||
| 135 | // Calling setspecific(..., 0) sets the value to 0 for ALL threads. | - | ||||||
| 136 | // The 'set to 1' workaround adds a bit of an overhead though, | - | ||||||
| 137 | // since this function is called twice now. | - | ||||||
| 138 | if (p == (void *)1) | - | ||||||
| 139 | return; | - | ||||||
| 140 | #endif | - | ||||||
| 141 | // POSIX says the value in our key is set to zero before calling | - | ||||||
| 142 | // this destructor function, so we need to set it back to the | - | ||||||
| 143 | // right value... | - | ||||||
| 144 | pthread_setspecific(current_thread_data_key, p); | - | ||||||
| 145 | QThreadData *data = static_cast<QThreadData *>(p); | - | ||||||
| 146 | if (data->isAdopted) { | - | ||||||
| 147 | QThread *thread = data->thread; | - | ||||||
| 148 | Q_ASSERT(thread); | - | ||||||
| 149 | QThreadPrivate *thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread)); | - | ||||||
| 150 | Q_ASSERT(!thread_p->finished); | - | ||||||
| 151 | thread_p->finish(thread); | - | ||||||
| 152 | } | - | ||||||
| 153 | data->deref(); | - | ||||||
| 154 | - | |||||||
| 155 | // ... but we must reset it to zero before returning so we aren't | - | ||||||
| 156 | // called again (POSIX allows implementations to call destructor | - | ||||||
| 157 | // functions repeatedly until all values are zero) | - | ||||||
| 158 | pthread_setspecific(current_thread_data_key, | - | ||||||
| 159 | #if defined(Q_OS_VXWORKS) | - | ||||||
| 160 | (void *)1); | - | ||||||
| 161 | #else | - | ||||||
| 162 | 0); | - | ||||||
| 163 | #endif | - | ||||||
| 164 | } | - | ||||||
| 165 | - | |||||||
| 166 | static void create_current_thread_data_key() | - | ||||||
| 167 | { | - | ||||||
| 168 | pthread_key_create(¤t_thread_data_key, destroy_current_thread_data); | - | ||||||
| 169 | } | - | ||||||
| 170 | - | |||||||
| 171 | static void destroy_current_thread_data_key() | - | ||||||
| 172 | { | - | ||||||
| 173 | pthread_once(¤t_thread_data_once, create_current_thread_data_key); | - | ||||||
| 174 | pthread_key_delete(current_thread_data_key); | - | ||||||
| 175 | - | |||||||
| 176 | // Reset current_thread_data_once in case we end up recreating | - | ||||||
| 177 | // the thread-data in the rare case of QObject construction | - | ||||||
| 178 | // after destroying the QThreadData. | - | ||||||
| 179 | pthread_once_t pthread_once_init = PTHREAD_ONCE_INIT; | - | ||||||
| 180 | current_thread_data_once = pthread_once_init; | - | ||||||
| 181 | } | - | ||||||
| 182 | Q_DESTRUCTOR_FUNCTION(destroy_current_thread_data_key) | - | ||||||
| 183 | - | |||||||
| 184 | - | |||||||
| 185 | // Utility functions for getting, setting and clearing thread specific data. | - | ||||||
| 186 | static QThreadData *get_thread_data() | - | ||||||
| 187 | { | - | ||||||
| 188 | #ifdef HAVE_TLS | - | ||||||
| 189 | return currentThreadData; | - | ||||||
| 190 | #else | - | ||||||
| 191 | pthread_once(¤t_thread_data_once, create_current_thread_data_key); | - | ||||||
| 192 | return reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key)); | - | ||||||
| 193 | #endif | - | ||||||
| 194 | } | - | ||||||
| 195 | - | |||||||
| 196 | static void set_thread_data(QThreadData *data) | - | ||||||
| 197 | { | - | ||||||
| 198 | #ifdef HAVE_TLS | - | ||||||
| 199 | currentThreadData = data; | - | ||||||
| 200 | #endif | - | ||||||
| 201 | pthread_once(¤t_thread_data_once, create_current_thread_data_key); | - | ||||||
| 202 | pthread_setspecific(current_thread_data_key, data); | - | ||||||
| 203 | } | - | ||||||
| 204 | - | |||||||
| 205 | static void clear_thread_data() | - | ||||||
| 206 | { | - | ||||||
| 207 | #ifdef HAVE_TLS | - | ||||||
| 208 | currentThreadData = 0; | - | ||||||
| 209 | #endif | - | ||||||
| 210 | pthread_setspecific(current_thread_data_key, 0); | - | ||||||
| 211 | } | - | ||||||
| 212 | - | |||||||
| 213 | template <typename T> | - | ||||||
| 214 | static typename QtPrivate::QEnableIf<QTypeInfo<T>::isIntegral, Qt::HANDLE>::Type to_HANDLE(T id) | - | ||||||
| 215 | { | - | ||||||
| 216 | return reinterpret_cast<Qt::HANDLE>(static_cast<intptr_t>(id)); | - | ||||||
| 217 | } | - | ||||||
| 218 | - | |||||||
| 219 | template <typename T> | - | ||||||
| 220 | static typename QtPrivate::QEnableIf<QTypeInfo<T>::isIntegral, T>::Type from_HANDLE(Qt::HANDLE id) | - | ||||||
| 221 | { | - | ||||||
| 222 | return static_cast<T>(reinterpret_cast<intptr_t>(id)); | - | ||||||
| 223 | } | - | ||||||
| 224 | - | |||||||
| 225 | template <typename T> | - | ||||||
| 226 | static typename QtPrivate::QEnableIf<QTypeInfo<T>::isPointer, Qt::HANDLE>::Type to_HANDLE(T id) | - | ||||||
| 227 | { | - | ||||||
| 228 | return id; | - | ||||||
| 229 | } | - | ||||||
| 230 | - | |||||||
| 231 | template <typename T> | - | ||||||
| 232 | static typename QtPrivate::QEnableIf<QTypeInfo<T>::isPointer, T>::Type from_HANDLE(Qt::HANDLE id) | - | ||||||
| 233 | { | - | ||||||
| 234 | return static_cast<T>(id); | - | ||||||
| 235 | } | - | ||||||
| 236 | - | |||||||
| 237 | void QThreadData::clearCurrentThreadData() | - | ||||||
| 238 | { | - | ||||||
| 239 | clear_thread_data(); | - | ||||||
| 240 | } | - | ||||||
| 241 | - | |||||||
| 242 | QThreadData *QThreadData::current(bool createIfNecessary) | - | ||||||
| 243 | { | - | ||||||
| 244 | QThreadData *data = get_thread_data(); | - | ||||||
| 245 | if (!data && createIfNecessary) { | - | ||||||
| 246 | data = new QThreadData; | - | ||||||
| 247 | QT_TRY { | - | ||||||
| 248 | set_thread_data(data); | - | ||||||
| 249 | data->thread = new QAdoptedThread(data); | - | ||||||
| 250 | } QT_CATCH(...) { | - | ||||||
| 251 | clear_thread_data(); | - | ||||||
| 252 | data->deref(); | - | ||||||
| 253 | data = 0; | - | ||||||
| 254 | QT_RETHROW; | - | ||||||
| 255 | } | - | ||||||
| 256 | data->deref(); | - | ||||||
| 257 | data->isAdopted = true; | - | ||||||
| 258 | data->threadId = to_HANDLE(pthread_self()); | - | ||||||
| 259 | if (!QCoreApplicationPrivate::theMainThread) | - | ||||||
| 260 | QCoreApplicationPrivate::theMainThread = data->thread.load(); | - | ||||||
| 261 | } | - | ||||||
| 262 | return data; | - | ||||||
| 263 | } | - | ||||||
| 264 | - | |||||||
| 265 | - | |||||||
| 266 | void QAdoptedThread::init() | - | ||||||
| 267 | { | - | ||||||
| 268 | } | - | ||||||
| 269 | - | |||||||
| 270 | /* | - | ||||||
| 271 | QThreadPrivate | - | ||||||
| 272 | */ | - | ||||||
| 273 | - | |||||||
| 274 | extern "C" { | - | ||||||
| 275 | typedef void*(*QtThreadCallback)(void*); | - | ||||||
| 276 | } | - | ||||||
| 277 | - | |||||||
| 278 | #endif // QT_NO_THREAD | - | ||||||
| 279 | - | |||||||
| 280 | void QThreadPrivate::createEventDispatcher(QThreadData *data) | - | ||||||
| 281 | { | - | ||||||
| 282 | #if defined(Q_OS_BLACKBERRY) | - | ||||||
| data->eventDispatcher.storeRelease(new QEventDispatcherBlackberry); | ||||||||
| # elifdefined(Q_OS_OSX) | ||||||||
| 283 | bool ok = false; | - | ||||||
| 284 | int value = qEnvironmentVariableIntValue("QT_EVENT_DISPATCHER_CORE_FOUNDATION", &ok); | - | ||||||
| 285 | if (ok && value > 0) | - | ||||||
| 286 | data->eventDispatcher.storeRelease(new QEventDispatcherCoreFoundation); | - | ||||||
| 287 | else | - | ||||||
| 288 | data->eventDispatcher.storeRelease(new QEventDispatcherUNIX); | - | ||||||
| # elif#elif !defined(QT_NO_GLIB) | ||||||||
| 290 | if (qEnvironmentVariableIsEmpty("QT_NO_GLIB")
| 0-620684 | ||||||
| 291 | && qEnvironmentVariableIsEmpty("QT_NO_THREADED_GLIB")
| 0-620684 | ||||||
| 292 | && QEventDispatcherGlib::versionSupported())
| 0-620684 | ||||||
| 293 | data->eventDispatcher.storeRelease(new QEventDispatcherGlib); executed 620684 times by 512 tests: data->eventDispatcher.storeRelease(new QEventDispatcherGlib);Executed by:
| 620684 | ||||||
| 294 | else | - | ||||||
| 295 | data->eventDispatcher.storeRelease(new QEventDispatcherUNIX); never executed: data->eventDispatcher.storeRelease(new QEventDispatcherUNIX); | 0 | ||||||
| 296 | #else | - | ||||||
| 297 | data->eventDispatcher.storeRelease(new QEventDispatcherUNIX); | - | ||||||
| 298 | #endif | - | ||||||
| 299 | - | |||||||
| 300 | data->eventDispatcher.load()->startingUp(); | - | ||||||
| 301 | } executed 620684 times by 512 tests: end of blockExecuted by:
| 620684 | ||||||
| 302 | - | |||||||
| 303 | #ifndef QT_NO_THREAD | - | ||||||
| 304 | - | |||||||
| 305 | #if (defined(Q_OS_LINUX) || defined(Q_OS_MAC) || defined(Q_OS_QNX)) | - | ||||||
| 306 | static void setCurrentThreadName(pthread_t threadId, const char *name) | - | ||||||
| 307 | { | - | ||||||
| 308 | # if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) | - | ||||||
| 309 | Q_UNUSED(threadId); | - | ||||||
| 310 | prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); | - | ||||||
| 311 | # elif defined(Q_OS_MAC) | - | ||||||
| 312 | Q_UNUSED(threadId); | - | ||||||
| 313 | pthread_setname_np(name); | - | ||||||
| 314 | # elif defined(Q_OS_QNX) | - | ||||||
| 315 | pthread_setname_np(threadId, name); | - | ||||||
| 316 | # endif | - | ||||||
| 317 | } | - | ||||||
| 318 | #endif | - | ||||||
| 319 | - | |||||||
| 320 | void *QThreadPrivate::start(void *arg) | - | ||||||
| 321 | { | - | ||||||
| 322 | #if !defined(Q_OS_ANDROID) | - | ||||||
| 323 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); | - | ||||||
| 324 | #endif | - | ||||||
| 325 | pthread_cleanup_push(QThreadPrivate::finish, arg); | - | ||||||
| 326 | - | |||||||
| 327 | QThread *thr = reinterpret_cast<QThread *>(arg); | - | ||||||
| 328 | QThreadData *data = QThreadData::get2(thr); | - | ||||||
| 329 | - | |||||||
| 330 | { | - | ||||||
| 331 | QMutexLocker locker(&thr->d_func()->mutex); | - | ||||||
| 332 | - | |||||||
| 333 | // do we need to reset the thread priority? | - | ||||||
| 334 | if (int(thr->d_func()->priority) & ThreadPriorityResetFlag) { | - | ||||||
| 335 | thr->d_func()->setPriority(QThread::Priority(thr->d_func()->priority & ~ThreadPriorityResetFlag)); | - | ||||||
| 336 | } | - | ||||||
| 337 | - | |||||||
| 338 | data->threadId = to_HANDLE(pthread_self()); | - | ||||||
| 339 | set_thread_data(data); | - | ||||||
| 340 | - | |||||||
| 341 | data->ref(); | - | ||||||
| 342 | data->quitNow = thr->d_func()->exited; | - | ||||||
| 343 | } | - | ||||||
| 344 | - | |||||||
| 345 | if (data->eventDispatcher.load()) // custom event dispatcher set? | - | ||||||
| 346 | data->eventDispatcher.load()->startingUp(); | - | ||||||
| 347 | else | - | ||||||
| 348 | createEventDispatcher(data); | - | ||||||
| 349 | - | |||||||
| 350 | #if (defined(Q_OS_LINUX) || defined(Q_OS_MAC) || defined(Q_OS_QNX)) | - | ||||||
| 351 | { | - | ||||||
| 352 | // sets the name of the current thread. | - | ||||||
| 353 | QString objectName = thr->objectName(); | - | ||||||
| 354 | - | |||||||
| 355 | pthread_t thread_id = from_HANDLE<pthread_t>(data->threadId); | - | ||||||
| 356 | if (Q_LIKELY(objectName.isEmpty())) | - | ||||||
| 357 | setCurrentThreadName(thread_id, thr->metaObject()->className()); | - | ||||||
| 358 | else | - | ||||||
| 359 | setCurrentThreadName(thread_id, objectName.toLocal8Bit()); | - | ||||||
| 360 | } | - | ||||||
| 361 | #endif | - | ||||||
| 362 | - | |||||||
| 363 | emit thr->started(QThread::QPrivateSignal()); | - | ||||||
| 364 | #if !defined(Q_OS_ANDROID) | - | ||||||
| 365 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); | - | ||||||
| 366 | pthread_testcancel(); | - | ||||||
| 367 | #endif | - | ||||||
| 368 | thr->run(); | - | ||||||
| 369 | - | |||||||
| 370 | pthread_cleanup_pop(1); | - | ||||||
| 371 | - | |||||||
| 372 | return 0; | - | ||||||
| 373 | } | - | ||||||
| 374 | - | |||||||
| 375 | void QThreadPrivate::finish(void *arg) | - | ||||||
| 376 | { | - | ||||||
| 377 | QThread *thr = reinterpret_cast<QThread *>(arg); | - | ||||||
| 378 | QThreadPrivate *d = thr->d_func(); | - | ||||||
| 379 | - | |||||||
| 380 | QMutexLocker locker(&d->mutex); | - | ||||||
| 381 | - | |||||||
| 382 | d->isInFinish = true; | - | ||||||
| 383 | d->priority = QThread::InheritPriority; | - | ||||||
| 384 | void *data = &d->data->tls; | - | ||||||
| 385 | locker.unlock(); | - | ||||||
| 386 | emit thr->finished(QThread::QPrivateSignal()); | - | ||||||
| 387 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); | - | ||||||
| 388 | QThreadStorageData::finish((void **)data); | - | ||||||
| 389 | locker.relock(); | - | ||||||
| 390 | - | |||||||
| 391 | QAbstractEventDispatcher *eventDispatcher = d->data->eventDispatcher.load(); | - | ||||||
| 392 | if (eventDispatcher) { | - | ||||||
| 393 | d->data->eventDispatcher = 0; | - | ||||||
| 394 | locker.unlock(); | - | ||||||
| 395 | eventDispatcher->closingDown(); | - | ||||||
| 396 | delete eventDispatcher; | - | ||||||
| 397 | locker.relock(); | - | ||||||
| 398 | } | - | ||||||
| 399 | - | |||||||
| 400 | d->running = false; | - | ||||||
| 401 | d->finished = true; | - | ||||||
| 402 | d->interruptionRequested = false; | - | ||||||
| 403 | - | |||||||
| 404 | d->isInFinish = false; | - | ||||||
| 405 | d->thread_done.wakeAll(); | - | ||||||
| 406 | } | - | ||||||
| 407 | - | |||||||
| 408 | - | |||||||
| 409 | - | |||||||
| 410 | - | |||||||
| 411 | /************************************************************************** | - | ||||||
| 412 | ** QThread | - | ||||||
| 413 | *************************************************************************/ | - | ||||||
| 414 | - | |||||||
| 415 | Qt::HANDLE QThread::currentThreadId() Q_DECL_NOTHROW | - | ||||||
| 416 | { | - | ||||||
| 417 | // requires a C cast here otherwise we run into trouble on AIX | - | ||||||
| 418 | return to_HANDLE(pthread_self()); executed 13345982 times by 599 tests: return to_HANDLE(pthread_self());Executed by:
| 13345982 | ||||||
| 419 | } | - | ||||||
| 420 | - | |||||||
| 421 | #if defined(QT_LINUXBASE) && !defined(_SC_NPROCESSORS_ONLN) | - | ||||||
| 422 | // LSB doesn't define _SC_NPROCESSORS_ONLN. | - | ||||||
| 423 | # define _SC_NPROCESSORS_ONLN 84 | - | ||||||
| 424 | #endif | - | ||||||
| 425 | - | |||||||
| 426 | int QThread::idealThreadCount() Q_DECL_NOTHROW | - | ||||||
| 427 | { | - | ||||||
| 428 | int cores = 1; | - | ||||||
| 429 | - | |||||||
| 430 | #if defined(Q_OS_HPUX) | - | ||||||
| 431 | // HP-UX | - | ||||||
| 432 | struct pst_dynamic psd; | - | ||||||
| 433 | if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) == -1) { | - | ||||||
| 434 | perror("pstat_getdynamic"); | - | ||||||
| 435 | } else { | - | ||||||
| 436 | cores = (int)psd.psd_proc_cnt; | - | ||||||
| 437 | } | - | ||||||
| 438 | #elif defined(Q_OS_BSD4) | - | ||||||
| 439 | // FreeBSD, OpenBSD, NetBSD, BSD/OS, OS X, iOS | - | ||||||
| 440 | size_t len = sizeof(cores); | - | ||||||
| 441 | int mib[2]; | - | ||||||
| 442 | mib[0] = CTL_HW; | - | ||||||
| 443 | mib[1] = HW_NCPU; | - | ||||||
| 444 | if (sysctl(mib, 2, &cores, &len, NULL, 0) != 0) { | - | ||||||
| 445 | perror("sysctl"); | - | ||||||
| 446 | } | - | ||||||
| 447 | #elif defined(Q_OS_IRIX) | - | ||||||
| 448 | // IRIX | - | ||||||
| 449 | cores = (int)sysconf(_SC_NPROC_ONLN); | - | ||||||
| 450 | #elif defined(Q_OS_INTEGRITY) | - | ||||||
| 451 | #if (__INTEGRITY_MAJOR_VERSION >= 10) | - | ||||||
| 452 | // Integrity V10+ does support multicore CPUs | - | ||||||
| 453 | Value processorCount; | - | ||||||
| 454 | if (GetProcessorCount(CurrentTask(), &processorCount) == 0) | - | ||||||
| 455 | cores = processorCount; | - | ||||||
| 456 | else | - | ||||||
| 457 | #endif | - | ||||||
| 458 | // as of aug 2008 Integrity only supports one single core CPU | - | ||||||
| 459 | cores = 1; | - | ||||||
| 460 | #elif defined(Q_OS_VXWORKS) | - | ||||||
| 461 | // VxWorks | - | ||||||
| 462 | # if defined(QT_VXWORKS_HAS_CPUSET) | - | ||||||
| 463 | cpuset_t cpus = vxCpuEnabledGet(); | - | ||||||
| 464 | cores = 0; | - | ||||||
| 465 | - | |||||||
| 466 | // 128 cores should be enough for everyone ;) | - | ||||||
| 467 | for (int i = 0; i < 128 && !CPUSET_ISZERO(cpus); ++i) { | - | ||||||
| 468 | if (CPUSET_ISSET(cpus, i)) { | - | ||||||
| 469 | CPUSET_CLR(cpus, i); | - | ||||||
| 470 | cores++; | - | ||||||
| 471 | } | - | ||||||
| 472 | } | - | ||||||
| 473 | # else | - | ||||||
| 474 | // as of aug 2008 VxWorks < 6.6 only supports one single core CPU | - | ||||||
| 475 | cores = 1; | - | ||||||
| 476 | # endif | - | ||||||
| 477 | #else | - | ||||||
| 478 | // the rest: Linux, Solaris, AIX, Tru64 | - | ||||||
| 479 | cores = (int)sysconf(_SC_NPROCESSORS_ONLN); | - | ||||||
| 480 | if (cores == -1)
| 0-1903 | ||||||
| 481 | return 1; never executed: return 1; | 0 | ||||||
| 482 | #endif | - | ||||||
| 483 | return cores; executed 1903 times by 447 tests: return cores;Executed by:
| 1903 | ||||||
| 484 | } | - | ||||||
| 485 | - | |||||||
| 486 | void QThread::yieldCurrentThread() | - | ||||||
| 487 | { | - | ||||||
| 488 | sched_yield(); | - | ||||||
| 489 | } | - | ||||||
| 490 | - | |||||||
| 491 | static timespec makeTimespec(time_t secs, long nsecs) | - | ||||||
| 492 | { | - | ||||||
| 493 | struct timespec ts; | - | ||||||
| 494 | ts.tv_sec = secs; | - | ||||||
| 495 | ts.tv_nsec = nsecs; | - | ||||||
| 496 | return ts; | - | ||||||
| 497 | } | - | ||||||
| 498 | - | |||||||
| 499 | void QThread::sleep(unsigned long secs) | - | ||||||
| 500 | { | - | ||||||
| 501 | qt_nanosleep(makeTimespec(secs, 0)); | - | ||||||
| 502 | } | - | ||||||
| 503 | - | |||||||
| 504 | void QThread::msleep(unsigned long msecs) | - | ||||||
| 505 | { | - | ||||||
| 506 | qt_nanosleep(makeTimespec(msecs / 1000, msecs % 1000 * 1000 * 1000)); | - | ||||||
| 507 | } | - | ||||||
| 508 | - | |||||||
| 509 | void QThread::usleep(unsigned long usecs) | - | ||||||
| 510 | { | - | ||||||
| 511 | qt_nanosleep(makeTimespec(usecs / 1000 / 1000, usecs % (1000*1000) * 1000)); | - | ||||||
| 512 | } | - | ||||||
| 513 | - | |||||||
| 514 | #ifdef QT_HAS_THREAD_PRIORITY_SCHEDULING | - | ||||||
| 515 | // Does some magic and calculate the Unix scheduler priorities | - | ||||||
| 516 | // sched_policy is IN/OUT: it must be set to a valid policy before calling this function | - | ||||||
| 517 | // sched_priority is OUT only | - | ||||||
| 518 | static bool calculateUnixPriority(int priority, int *sched_policy, int *sched_priority) | - | ||||||
| 519 | { | - | ||||||
| 520 | #ifdef SCHED_IDLE | - | ||||||
| 521 | if (priority == QThread::IdlePriority) { | - | ||||||
| 522 | *sched_policy = SCHED_IDLE; | - | ||||||
| 523 | *sched_priority = 0; | - | ||||||
| 524 | return true; | - | ||||||
| 525 | } | - | ||||||
| 526 | const int lowestPriority = QThread::LowestPriority; | - | ||||||
| 527 | #else | - | ||||||
| 528 | const int lowestPriority = QThread::IdlePriority; | - | ||||||
| 529 | #endif | - | ||||||
| 530 | const int highestPriority = QThread::TimeCriticalPriority; | - | ||||||
| 531 | - | |||||||
| 532 | int prio_min; | - | ||||||
| 533 | int prio_max; | - | ||||||
| 534 | #if defined(Q_OS_VXWORKS) && defined(VXWORKS_DKM) | - | ||||||
| 535 | // for other scheduling policies than SCHED_RR or SCHED_FIFO | - | ||||||
| 536 | prio_min = SCHED_FIFO_LOW_PRI; | - | ||||||
| 537 | prio_max = SCHED_FIFO_HIGH_PRI; | - | ||||||
| 538 | - | |||||||
| 539 | if ((*sched_policy == SCHED_RR) || (*sched_policy == SCHED_FIFO)) | - | ||||||
| 540 | #endif | - | ||||||
| 541 | { | - | ||||||
| 542 | prio_min = sched_get_priority_min(*sched_policy); | - | ||||||
| 543 | prio_max = sched_get_priority_max(*sched_policy); | - | ||||||
| 544 | } | - | ||||||
| 545 | - | |||||||
| 546 | if (prio_min == -1 || prio_max == -1) | - | ||||||
| 547 | return false; | - | ||||||
| 548 | - | |||||||
| 549 | int prio; | - | ||||||
| 550 | // crudely scale our priority enum values to the prio_min/prio_max | - | ||||||
| 551 | prio = ((priority - lowestPriority) * (prio_max - prio_min) / highestPriority) + prio_min; | - | ||||||
| 552 | prio = qMax(prio_min, qMin(prio_max, prio)); | - | ||||||
| 553 | - | |||||||
| 554 | *sched_priority = prio; | - | ||||||
| 555 | return true; | - | ||||||
| 556 | } | - | ||||||
| 557 | #endif | - | ||||||
| 558 | - | |||||||
| 559 | void QThread::start(Priority priority) | - | ||||||
| 560 | { | - | ||||||
| 561 | Q_D(QThread); | - | ||||||
| 562 | QMutexLocker locker(&d->mutex); | - | ||||||
| 563 | - | |||||||
| 564 | if (d->isInFinish) | - | ||||||
| 565 | d->thread_done.wait(locker.mutex()); | - | ||||||
| 566 | - | |||||||
| 567 | if (d->running) | - | ||||||
| 568 | return; | - | ||||||
| 569 | - | |||||||
| 570 | d->running = true; | - | ||||||
| 571 | d->finished = false; | - | ||||||
| 572 | d->returnCode = 0; | - | ||||||
| 573 | d->exited = false; | - | ||||||
| 574 | d->interruptionRequested = false; | - | ||||||
| 575 | - | |||||||
| 576 | pthread_attr_t attr; | - | ||||||
| 577 | pthread_attr_init(&attr); | - | ||||||
| 578 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | - | ||||||
| 579 | - | |||||||
| 580 | d->priority = priority; | - | ||||||
| 581 | - | |||||||
| 582 | #if defined(QT_HAS_THREAD_PRIORITY_SCHEDULING) | - | ||||||
| 583 | switch (priority) { | - | ||||||
| 584 | case InheritPriority: | - | ||||||
| 585 | { | - | ||||||
| 586 | pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); | - | ||||||
| 587 | break; | - | ||||||
| 588 | } | - | ||||||
| 589 | - | |||||||
| 590 | default: | - | ||||||
| 591 | { | - | ||||||
| 592 | int sched_policy; | - | ||||||
| 593 | if (pthread_attr_getschedpolicy(&attr, &sched_policy) != 0) { | - | ||||||
| 594 | // failed to get the scheduling policy, don't bother | - | ||||||
| 595 | // setting the priority | - | ||||||
| 596 | qWarning("QThread::start: Cannot determine default scheduler policy"); | - | ||||||
| 597 | break; | - | ||||||
| 598 | } | - | ||||||
| 599 | - | |||||||
| 600 | int prio; | - | ||||||
| 601 | if (!calculateUnixPriority(priority, &sched_policy, &prio)) { | - | ||||||
| 602 | // failed to get the scheduling parameters, don't | - | ||||||
| 603 | // bother setting the priority | - | ||||||
| 604 | qWarning("QThread::start: Cannot determine scheduler priority range"); | - | ||||||
| 605 | break; | - | ||||||
| 606 | } | - | ||||||
| 607 | - | |||||||
| 608 | sched_param sp; | - | ||||||
| 609 | sp.sched_priority = prio; | - | ||||||
| 610 | - | |||||||
| 611 | if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0 | - | ||||||
| 612 | || pthread_attr_setschedpolicy(&attr, sched_policy) != 0 | - | ||||||
| 613 | || pthread_attr_setschedparam(&attr, &sp) != 0) { | - | ||||||
| 614 | // could not set scheduling hints, fallback to inheriting them | - | ||||||
| 615 | // we'll try again from inside the thread | - | ||||||
| 616 | pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); | - | ||||||
| 617 | d->priority = Priority(priority | ThreadPriorityResetFlag); | - | ||||||
| 618 | } | - | ||||||
| 619 | break; | - | ||||||
| 620 | } | - | ||||||
| 621 | } | - | ||||||
| 622 | #endif // QT_HAS_THREAD_PRIORITY_SCHEDULING | - | ||||||
| 623 | - | |||||||
| 624 | - | |||||||
| 625 | if (d->stackSize > 0) { | - | ||||||
| 626 | #if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0 > 0) | - | ||||||
| 627 | int code = pthread_attr_setstacksize(&attr, d->stackSize); | - | ||||||
| 628 | #else | - | ||||||
| 629 | int code = ENOSYS; // stack size not supported, automatically fail | - | ||||||
| 630 | #endif // _POSIX_THREAD_ATTR_STACKSIZE | - | ||||||
| 631 | - | |||||||
| 632 | if (code) { | - | ||||||
| 633 | qWarning("QThread::start: Thread stack size error: %s", | - | ||||||
| 634 | qPrintable(qt_error_string(code))); | - | ||||||
| 635 | - | |||||||
| 636 | // we failed to set the stacksize, and as the documentation states, | - | ||||||
| 637 | // the thread will fail to run... | - | ||||||
| 638 | d->running = false; | - | ||||||
| 639 | d->finished = false; | - | ||||||
| 640 | return; | - | ||||||
| 641 | } | - | ||||||
| 642 | } | - | ||||||
| 643 | - | |||||||
| 644 | pthread_t threadId; | - | ||||||
| 645 | int code = pthread_create(&threadId, &attr, QThreadPrivate::start, this); | - | ||||||
| 646 | if (code == EPERM) { | - | ||||||
| 647 | // caller does not have permission to set the scheduling | - | ||||||
| 648 | // parameters/policy | - | ||||||
| 649 | #if defined(QT_HAS_THREAD_PRIORITY_SCHEDULING) | - | ||||||
| 650 | pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); | - | ||||||
| 651 | #endif | - | ||||||
| 652 | code = pthread_create(&threadId, &attr, QThreadPrivate::start, this); | - | ||||||
| 653 | } | - | ||||||
| 654 | d->data->threadId = to_HANDLE(threadId); | - | ||||||
| 655 | - | |||||||
| 656 | pthread_attr_destroy(&attr); | - | ||||||
| 657 | - | |||||||
| 658 | if (code) { | - | ||||||
| 659 | qWarning("QThread::start: Thread creation error: %s", qPrintable(qt_error_string(code))); | - | ||||||
| 660 | - | |||||||
| 661 | d->running = false; | - | ||||||
| 662 | d->finished = false; | - | ||||||
| 663 | d->data->threadId = 0; | - | ||||||
| 664 | } | - | ||||||
| 665 | } | - | ||||||
| 666 | - | |||||||
| 667 | void QThread::terminate() | - | ||||||
| 668 | { | - | ||||||
| 669 | #if !defined(Q_OS_ANDROID) | - | ||||||
| 670 | Q_D(QThread); | - | ||||||
| 671 | QMutexLocker locker(&d->mutex); | - | ||||||
| 672 | - | |||||||
| 673 | if (!d->data->threadId) | - | ||||||
| 674 | return; | - | ||||||
| 675 | - | |||||||
| 676 | int code = pthread_cancel(from_HANDLE<pthread_t>(d->data->threadId)); | - | ||||||
| 677 | if (code) { | - | ||||||
| 678 | qWarning("QThread::start: Thread termination error: %s", | - | ||||||
| 679 | qPrintable(qt_error_string((code)))); | - | ||||||
| 680 | } | - | ||||||
| 681 | #endif | - | ||||||
| 682 | } | - | ||||||
| 683 | - | |||||||
| 684 | bool QThread::wait(unsigned long time) | - | ||||||
| 685 | { | - | ||||||
| 686 | Q_D(QThread); | - | ||||||
| 687 | QMutexLocker locker(&d->mutex); | - | ||||||
| 688 | - | |||||||
| 689 | if (from_HANDLE<pthread_t>(d->data->threadId) == pthread_self()) { | - | ||||||
| 690 | qWarning("QThread::wait: Thread tried to wait on itself"); | - | ||||||
| 691 | return false; | - | ||||||
| 692 | } | - | ||||||
| 693 | - | |||||||
| 694 | if (d->finished || !d->running) | - | ||||||
| 695 | return true; | - | ||||||
| 696 | - | |||||||
| 697 | while (d->running) { | - | ||||||
| 698 | if (!d->thread_done.wait(locker.mutex(), time)) | - | ||||||
| 699 | return false; | - | ||||||
| 700 | } | - | ||||||
| 701 | return true; | - | ||||||
| 702 | } | - | ||||||
| 703 | - | |||||||
| 704 | void QThread::setTerminationEnabled(bool enabled) | - | ||||||
| 705 | { | - | ||||||
| 706 | QThread *thr = currentThread(); | - | ||||||
| 707 | Q_ASSERT_X(thr != 0, "QThread::setTerminationEnabled()", | - | ||||||
| 708 | "Current thread was not started with QThread."); | - | ||||||
| 709 | - | |||||||
| 710 | Q_UNUSED(thr) | - | ||||||
| 711 | #if defined(Q_OS_ANDROID) | - | ||||||
| 712 | Q_UNUSED(enabled); | - | ||||||
| 713 | #else | - | ||||||
| 714 | pthread_setcancelstate(enabled ? PTHREAD_CANCEL_ENABLE : PTHREAD_CANCEL_DISABLE, NULL); | - | ||||||
| 715 | if (enabled) | - | ||||||
| 716 | pthread_testcancel(); | - | ||||||
| 717 | #endif | - | ||||||
| 718 | } | - | ||||||
| 719 | - | |||||||
| 720 | // Caller must lock the mutex | - | ||||||
| 721 | void QThreadPrivate::setPriority(QThread::Priority threadPriority) | - | ||||||
| 722 | { | - | ||||||
| 723 | priority = threadPriority; | - | ||||||
| 724 | - | |||||||
| 725 | // copied from start() with a few modifications: | - | ||||||
| 726 | - | |||||||
| 727 | #ifdef QT_HAS_THREAD_PRIORITY_SCHEDULING | - | ||||||
| 728 | int sched_policy; | - | ||||||
| 729 | sched_param param; | - | ||||||
| 730 | - | |||||||
| 731 | if (pthread_getschedparam(from_HANDLE<pthread_t>(data->threadId), &sched_policy, ¶m) != 0) { | - | ||||||
| 732 | // failed to get the scheduling policy, don't bother setting | - | ||||||
| 733 | // the priority | - | ||||||
| 734 | qWarning("QThread::setPriority: Cannot get scheduler parameters"); | - | ||||||
| 735 | return; | - | ||||||
| 736 | } | - | ||||||
| 737 | - | |||||||
| 738 | int prio; | - | ||||||
| 739 | if (!calculateUnixPriority(priority, &sched_policy, &prio)) { | - | ||||||
| 740 | // failed to get the scheduling parameters, don't | - | ||||||
| 741 | // bother setting the priority | - | ||||||
| 742 | qWarning("QThread::setPriority: Cannot determine scheduler priority range"); | - | ||||||
| 743 | return; | - | ||||||
| 744 | } | - | ||||||
| 745 | - | |||||||
| 746 | param.sched_priority = prio; | - | ||||||
| 747 | int status = pthread_setschedparam(from_HANDLE<pthread_t>(data->threadId), sched_policy, ¶m); | - | ||||||
| 748 | - | |||||||
| 749 | # ifdef SCHED_IDLE | - | ||||||
| 750 | // were we trying to set to idle priority and failed? | - | ||||||
| 751 | if (status == -1 && sched_policy == SCHED_IDLE && errno == EINVAL) { | - | ||||||
| 752 | // reset to lowest priority possible | - | ||||||
| 753 | pthread_getschedparam(from_HANDLE<pthread_t>(data->threadId), &sched_policy, ¶m); | - | ||||||
| 754 | param.sched_priority = sched_get_priority_min(sched_policy); | - | ||||||
| 755 | pthread_setschedparam(from_HANDLE<pthread_t>(data->threadId), sched_policy, ¶m); | - | ||||||
| 756 | } | - | ||||||
| 757 | # else | - | ||||||
| 758 | Q_UNUSED(status); | - | ||||||
| 759 | # endif // SCHED_IDLE | - | ||||||
| 760 | #endif | - | ||||||
| 761 | } | - | ||||||
| 762 | - | |||||||
| 763 | #endif // QT_NO_THREAD | - | ||||||
| 764 | - | |||||||
| 765 | QT_END_NAMESPACE | - | ||||||
| 766 | - | |||||||
| Source code | Switch to Preprocessed file |