⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qthread_unix.cpp

📁 QT 开发环境里面一个很重要的文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtCore module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qthread.h"#include "qplatformdefs.h"#include <private/qcoreapplication_p.h>#if !defined(QT_NO_GLIB)#  include "../kernel/qeventdispatcher_glib_p.h"#endif#include <private/qeventdispatcher_unix_p.h>#include "qthreadstorage.h"#include "qthread_p.h"#include <sched.h>#include <errno.h>#include <string.h>#ifndef QT_NO_THREADstatic pthread_once_t current_thread_data_once = PTHREAD_ONCE_INIT;static pthread_key_t current_thread_data_key;static void destroy_current_thread_data(void *p){    // POSIX says the value in our key is set to zero before calling    // this destructor function, so we need to set it back to the    // right value...    pthread_setspecific(current_thread_data_key, p);    reinterpret_cast<QThreadData *>(p)->deref();    // ... but we must reset it to zero before returning so we aren't    // called again (POSIX allows implementations to call destructor    // functions repeatedly until all values are zero)    pthread_setspecific(current_thread_data_key, 0);}static void create_current_thread_data_key(){    pthread_key_create(&current_thread_data_key, destroy_current_thread_data);}QThreadData *QThreadData::current(){    pthread_once(&current_thread_data_once, create_current_thread_data_key);    QThreadData *data = 0;    QThread *adopted = 0;    if (QInternal::activateCallbacks(QInternal::AdoptCurrentThread, (void **) &adopted)) {        Q_ASSERT(adopted);        data = QThreadData::get2(adopted);        pthread_setspecific(current_thread_data_key, data);        adopted->d_func()->running = true;        adopted->d_func()->finished = false;        static_cast<QAdoptedThread *>(adopted)->init();    } else {        data = reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key));        if (!data) {            data = new QThreadData;            pthread_setspecific(current_thread_data_key, data);            data->thread = new QAdoptedThread(data);            data->deref();            (void) q_atomic_test_and_set_ptr(&QCoreApplicationPrivate::theMainThread, 0, data->thread);        }    }    return data;}void QAdoptedThread::init(){    d_func()->thread_id = pthread_self();}/*   QThreadPrivate*/#if defined(Q_C_CALLBACKS)extern "C" {#endiftypedef void*(*QtThreadCallback)(void*);#if defined(Q_C_CALLBACKS)}#endif#endif // QT_NO_THREADvoid QThreadPrivate::createEventDispatcher(QThreadData *data){#if !defined(QT_NO_GLIB)    if (qgetenv("QT_NO_GLIB").isEmpty())        data->eventDispatcher = new QEventDispatcherGlib;    else#endif        data->eventDispatcher = new QEventDispatcherUNIX;    data->eventDispatcher->startingUp();}#ifndef QT_NO_THREADvoid *QThreadPrivate::start(void *arg){    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);    pthread_cleanup_push(QThreadPrivate::finish, arg);    QThread *thr = reinterpret_cast<QThread *>(arg);    QThreadData *data = QThreadData::get2(thr);    pthread_once(&current_thread_data_once, create_current_thread_data_key);    pthread_setspecific(current_thread_data_key, data);    data->ref();    data->quitNow = false;    // ### TODO: allow the user to create a custom event dispatcher    createEventDispatcher(data);    emit thr->started();    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);    pthread_testcancel();    thr->run();    pthread_cleanup_pop(1);    return 0;}void QThreadPrivate::finish(void *arg){    QThread *thr = reinterpret_cast<QThread *>(arg);    QThreadPrivate *d = thr->d_func();    QMutexLocker locker(&d->mutex);    d->priority = QThread::InheritPriority;    d->running = false;    d->finished = true;    if (d->terminated)        emit thr->terminated();    d->terminated = false;    emit thr->finished();    if (d->data->eventDispatcher) {        d->data->eventDispatcher->closingDown();        QAbstractEventDispatcher *eventDispatcher = d->data->eventDispatcher;        d->data->eventDispatcher = 0;        delete eventDispatcher;    }    QThreadStorageData::finish(d->data->tls);    d->data->tls = 0;    d->thread_id = 0;    d->thread_done.wakeAll();}/************************************************************************** ** QThread *************************************************************************//*!    Returns the thread handle of the currently executing thread.    \warning The handle returned by this function is used for internal    purposes and should not be used in any application code. On    Windows, the returned value is a pseudo-handle for the current    thread that cannot be used for numerical comparison.*/Qt::HANDLE QThread::currentThreadId(){    // requires a C cast here otherwise we run into trouble on AIX    return (Qt::HANDLE)pthread_self();}/*  \internal    helper function to do thread sleeps, since usleep()/nanosleep()    aren't reliable enough (in terms of behavior and availability)*/static void thread_sleep(struct timespec *ti){    pthread_mutex_t mtx;    pthread_cond_t cnd;    pthread_mutex_init(&mtx, 0);    pthread_cond_init(&cnd, 0);    pthread_mutex_lock(&mtx);    (void) pthread_cond_timedwait(&cnd, &mtx, ti);    pthread_mutex_unlock(&mtx);    pthread_cond_destroy(&cnd);    pthread_mutex_destroy(&mtx);}/*!    Forces the current thread to sleep for \a secs seconds.    \sa msleep(), usleep()*/void QThread::sleep(unsigned long secs){    struct timeval tv;    gettimeofday(&tv, 0);    struct timespec ti;    ti.tv_sec = tv.tv_sec + secs;    ti.tv_nsec = (tv.tv_usec * 1000);    thread_sleep(&ti);}/*!    Causes the current thread to sleep for \a msecs milliseconds.    \sa sleep(), usleep()*/void QThread::msleep(unsigned long msecs){    struct timeval tv;    gettimeofday(&tv, 0);    struct timespec ti;    ti.tv_nsec = (tv.tv_usec + (msecs % 1000) * 1000) * 1000;    ti.tv_sec = tv.tv_sec + (msecs / 1000) + (ti.tv_nsec / 1000000000);    ti.tv_nsec %= 1000000000;    thread_sleep(&ti);}/*!    Causes the current thread to sleep for \a usecs microseconds.    \sa sleep(), msleep()*/void QThread::usleep(unsigned long usecs){    struct timeval tv;    gettimeofday(&tv, 0);    struct timespec ti;    ti.tv_nsec = (tv.tv_usec + (usecs % 1000000)) * 1000;    ti.tv_sec = tv.tv_sec + (usecs / 1000000) + (ti.tv_nsec / 1000000000);    ti.tv_nsec %= 1000000000;    thread_sleep(&ti);}/*!    Begins execution of the thread by calling run(), which should be    reimplemented in a QThread subclass to contain your code. The    operating system will schedule the thread according to the \a    priority parameter. If the thread is already running, this    function does nothing.    \sa run(), terminate()*/void QThread::start(Priority priority)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -