44 #include "qplatformdefs.h" 46 #include <private/qcoreapplication_p.h> 48 #if defined(Q_OS_BLACKBERRY) 49 # include <private/qeventdispatcher_blackberry_p.h> 51 # if !defined(QT_NO_GLIB) 52 # include "../kernel/qeventdispatcher_glib_p.h" 54 # include <private/qeventdispatcher_unix_p.h> 67 #include <sys/sysctl.h> 70 # if (_WRS_VXWORKS_MAJOR > 6) || ((_WRS_VXWORKS_MAJOR == 6) && (_WRS_VXWORKS_MINOR >= 6)) 71 # include <vxCpuLib.h> 73 # define QT_VXWORKS_HAS_CPUSET 78 #include <sys/pstat.h> 83 # define old_qDebug qDebug 86 #if !defined(Q_OS_IOS) 87 # include <CoreServices/CoreServices.h> 88 #endif // !defined(Q_OS_IOS) 92 # define qDebug QT_NO_QDEBUG_MACRO 97 #if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) 98 #include <sys/prctl.h> 101 #if defined(Q_OS_LINUX) && !defined(SCHED_IDLE) 103 # define SCHED_IDLE 5 106 #if defined(Q_OS_DARWIN) || !defined(Q_OS_OPENBSD) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) 107 #define QT_HAS_THREAD_PRIORITY_SCHEDULING 117 #if defined(Q_OS_LINUX) && defined(__GLIBC__) && (defined(Q_CC_GNU) || defined(Q_CC_INTEL)) 120 #if defined(Q_CC_XLC) || defined (Q_CC_SUN) 125 static __thread
QThreadData *currentThreadData = 0;
133 #if defined(Q_OS_VXWORKS) 158 #
if defined(Q_OS_VXWORKS)
178 pthread_once_t pthread_once_init = PTHREAD_ONCE_INIT;
188 return currentThreadData;
198 currentThreadData =
data;
207 currentThreadData = 0;
227 adopted->d_func()->running =
true;
228 adopted->d_func()->
finished =
false;
255 d->thread_id = pthread_self();
262 #if defined(Q_C_CALLBACKS) 266 typedef void*(*QtThreadCallback)(
void*);
268 #if defined(Q_C_CALLBACKS) 272 #endif // QT_NO_THREAD 276 #if defined(Q_OS_BLACKBERRY) 279 #if !defined(QT_NO_GLIB) 294 #if (defined(Q_OS_LINUX) || (defined(Q_OS_MAC) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) || defined(Q_OS_QNX)) 297 # if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) 299 prctl(PR_SET_NAME, (
unsigned long)name, 0, 0, 0);
300 # elif (defined(Q_OS_MAC) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) 303 pthread_setname_np(name);
304 # elif defined(Q_OS_QNX) 305 pthread_setname_np(threadId, name);
329 data->
quitNow = thr->d_func()->exited;
333 createEventDispatcher(data);
335 #if (defined(Q_OS_LINUX) || (defined(Q_OS_MAC) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) || defined(Q_OS_QNX)) 377 if (eventDispatcher) {
406 #if defined(QT_LINUXBASE) && !defined(_SC_NPROCESSORS_ONLN) 408 # define _SC_NPROCESSORS_ONLN 84 415 #if defined(Q_OS_MAC) && !defined(Q_WS_QPA) 417 cores = MPProcessorsScheduled();
418 #elif defined(Q_OS_HPUX) 420 struct pst_dynamic psd;
421 if (pstat_getdynamic(&psd,
sizeof(psd), 1, 0) == -1) {
422 perror(
"pstat_getdynamic");
425 cores = (int)psd.psd_proc_cnt;
427 #elif defined(Q_OS_BSD4) 429 size_t len =
sizeof(cores);
433 if (sysctl(mib, 2, &cores, &len, NULL, 0) != 0) {
437 #elif defined(Q_OS_IRIX) 439 cores = (int)sysconf(_SC_NPROC_ONLN);
440 #elif defined(Q_OS_INTEGRITY) 441 #if (__INTEGRITY_MAJOR_VERSION >= 10) 443 Value processorCount;
444 if (GetProcessorCount(CurrentTask(), &processorCount) == 0)
445 cores = processorCount;
449 #elif defined(Q_OS_VXWORKS) 451 # if defined(QT_VXWORKS_HAS_CPUSET) 452 cpuset_t cpus = vxCpuEnabledGet();
456 for (
int i = 0; i < 128 && !CPUSET_ISZERO(cpus); ++i) {
457 if (CPUSET_ISSET(cpus, i)) {
468 cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
488 pthread_mutex_init(&mtx, 0);
489 pthread_cond_init(&cnd, 0);
491 pthread_mutex_lock(&mtx);
492 (void) pthread_cond_timedwait(&cnd, &mtx, ti);
493 pthread_mutex_unlock(&mtx);
495 pthread_cond_destroy(&cnd);
496 pthread_mutex_destroy(&mtx);
502 gettimeofday(&tv, 0);
504 ti.tv_sec = tv.tv_sec + secs;
505 ti.tv_nsec = (tv.tv_usec * 1000);
512 gettimeofday(&tv, 0);
515 ti.tv_nsec = (tv.tv_usec + (msecs % 1000) * 1000) * 1000;
516 ti.tv_sec = tv.tv_sec + (msecs / 1000) + (ti.tv_nsec / 1000000000);
517 ti.tv_nsec %= 1000000000;
524 gettimeofday(&tv, 0);
527 ti.tv_nsec = (tv.tv_usec + (usecs % 1000000)) * 1000;
528 ti.tv_sec = tv.tv_sec + (usecs / 1000000) + (ti.tv_nsec / 1000000000);
529 ti.tv_nsec %= 1000000000;
533 #ifdef QT_HAS_THREAD_PRIORITY_SCHEDULING 542 *sched_policy = SCHED_RR;
558 #if defined(Q_OS_VXWORKS) && defined(VXWORKS_DKM) 560 prio_min = SCHED_FIFO_LOW_PRI;
561 prio_max = SCHED_FIFO_HIGH_PRI;
563 if ((*sched_policy == SCHED_RR) || (*sched_policy == SCHED_FIFO))
566 prio_min = sched_get_priority_min(*sched_policy);
567 prio_max = sched_get_priority_max(*sched_policy);
570 if (prio_min == -1 || prio_max == -1)
575 prio = ((priority - lowestPriority) * (prio_max - prio_min) / highestPriority) + prio_min;
576 prio =
qMax(prio_min,
qMin(prio_max, prio));
578 *sched_priority = prio;
589 d->thread_done.wait(locker.
mutex());
596 d->terminated =
false;
601 pthread_attr_init(&attr);
602 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
604 d->priority = priority;
606 #if defined(QT_HAS_THREAD_PRIORITY_SCHEDULING) 608 case InheritPriority:
617 if (pthread_attr_getschedpolicy(&attr, &sched_policy) != 0) {
620 qWarning(
"QThread::start: Cannot determine default scheduler policy");
628 qWarning(
"QThread::start: Cannot determine scheduler priority range");
633 sp.sched_priority = prio;
636 || pthread_attr_setschedpolicy(&attr, sched_policy) != 0
637 || pthread_attr_setschedparam(&attr, &sp) != 0) {
646 #endif // QT_HAS_THREAD_PRIORITY_SCHEDULING 649 if (
d->stackSize > 0) {
650 #if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0 > 0) 651 int code = pthread_attr_setstacksize(&attr,
d->stackSize);
654 #endif // _POSIX_THREAD_ATTR_STACKSIZE 657 qWarning(
"QThread::start: Thread stack size error: %s",
678 pthread_attr_destroy(&attr);
699 qWarning(
"QThread::start: Thread termination error: %s",
702 d->terminated =
true;
711 if (
d->thread_id == pthread_self()) {
712 qWarning(
"QThread::wait: Thread tried to wait on itself");
716 if (
d->finished || !
d->running)
720 if (!
d->thread_done.wait(locker.
mutex(), time))
728 QThread *thr = currentThread();
729 Q_ASSERT_X(thr != 0,
"QThread::setTerminationEnabled()",
730 "Current thread was not started with QThread.");
743 qWarning(
"QThread::setPriority: Cannot set priority, thread is not running");
747 d->priority = priority;
751 #ifdef QT_HAS_THREAD_PRIORITY_SCHEDULING 755 if (pthread_getschedparam(
d->thread_id, &sched_policy, ¶m) != 0) {
758 qWarning(
"QThread::setPriority: Cannot get scheduler parameters");
766 qWarning(
"QThread::setPriority: Cannot determine scheduler priority range");
770 param.sched_priority = prio;
771 int status = pthread_setschedparam(
d->thread_id, sched_policy, ¶m);
777 pthread_getschedparam(
d->thread_id, &sched_policy, ¶m);
778 param.sched_priority = sched_get_priority_min(sched_policy);
779 pthread_setschedparam(
d->thread_id, sched_policy, ¶m);
783 # endif // SCHED_IDLE 787 #endif // QT_NO_THREAD
QString qt_error_string(int errorCode)
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
virtual void run()
The starting point for the thread.
virtual void startingUp()
static bool versionSupported()
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
static void finish(void **)
#define QT_END_NAMESPACE
This macro expands to.
static void * start(void *arg)
void finished()
This signal is emitted when the thread has finished executing.
void pthread_cleanup_pop(int)
void unlock()
Unlocks this mutex locker.
static LibLoadStatus status
static QThread * theMainThread
long ASN1_INTEGER_get ASN1_INTEGER * a
static void clearCurrentThreadData()
The QString class provides a Unicode character string.
void started()
This signal is emitted when the thread starts executing.
static bool calculateUnixPriority(int priority, int *sched_policy, int *sched_priority)
#define Q_DESTRUCTOR_FUNCTION(AFUNC)
Priority
This enum type indicates how the operating system should schedule newly created threads.
static QObjectPrivate * get(QObject *o)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
static void setCurrentThreadName(pthread_t threadId, const char *name)
int pthread_cancel(pthread_t)
Priority priority() const
Returns the priority for a running thread.
static void yieldCurrentThread()
Yields execution of the current thread to another runnable thread, if any.
void relock()
Relocks an unlocked mutex locker.
static void msleep(unsigned long)
Forces the current thread to sleep for msecs milliseconds.
static QThreadData * get2(QThread *thread)
#define QT_BEGIN_NAMESPACE
This macro expands to.
static bool isEmpty(const char *str)
QThreadData(int initialRefCount=1)
static bool activateCallbacks(Callback, void **)
static void destroy_current_thread_data_key()
int pthread_setcancelstate(int, int *)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
void pthread_testcancel(void)
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
static void sendPostedEvents()
static void create_current_thread_data_key()
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
static pthread_once_t current_thread_data_once
virtual void closingDown()
#define Q_ASSERT_X(cond, where, what)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
void terminated()
This signal is emitted when the thread is terminated.
void pthread_cleanup_push(void(*)(void *), void *)
static pthread_key_t current_thread_data_key
QWaitCondition thread_done
static void setTerminationEnabled(bool enabled=true)
Enables or disables termination of the current thread based on the enabled parameter.
void start(Priority=InheritPriority)
Begins execution of the thread by calling run().
bool wait(unsigned long time=ULONG_MAX)
Blocks the thread until either of these conditions is met:
static void thread_sleep(struct timespec *ti)
static void set_thread_data(QThreadData *data)
QString objectName() const
static QThreadData * current()
#define Q_LIKELY(x)
Hints to the compiler that the enclosed condition, expr, is likely to evaluate to true...
QThread::Priority priority
static void destroy_current_thread_data(void *p)
void terminate()
Terminates the execution of the thread.
static const MacVersion MacintoshVersion
the version of the Macintosh operating system on which the application is run (Mac only)...
static void sleep(unsigned long)
Forces the current thread to sleep for secs seconds.
static void finish(void *)
static void createEventDispatcher(QThreadData *data)
#define qPrintable(string)
The QThread class provides a platform-independent way to manage threads.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
QAbstractEventDispatcher * eventDispatcher
static QThreadData * get_thread_data()
void setPriority(Priority priority)
This function sets the priority for a running thread.
static void clear_thread_data()
static void usleep(unsigned long)
Forces the current thread to sleep for usecs microseconds.
QMutex * mutex() const
Returns a pointer to the mutex that was locked in the constructor.
The QAbstractEventDispatcher class provides an interface to manage Qt's event queue.
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
static Qt::HANDLE currentThreadId()
Returns the thread handle of the currently executing thread.
int pthread_attr_setinheritsched(pthread_attr_t *, int)
static int idealThreadCount()
Returns the ideal number of threads that can be run on the system.