Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qthread_unix.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#include "qthread.h"
7#include "qthread_p.h"
8
9#include <private/qcoreapplication_p.h>
10#include <private/qcore_unix_p.h>
11#include "qdebug.h"
13#include <private/qtools_p.h>
14
15#if defined(Q_OS_WASM)
16# include <private/qeventdispatcher_wasm_p.h>
17#else
18# include <private/qeventdispatcher_unix_p.h>
19# if defined(Q_OS_DARWIN)
20# include <private/qeventdispatcher_cf_p.h>
21# elif !defined(QT_NO_GLIB)
22# include <private/qeventdispatcher_glib_p.h>
23# endif
24#endif
25
26#include <sched.h>
27#include <errno.h>
28#if __has_include(<pthread_np.h>)
29# include <pthread_np.h>
30#endif
31
32#if defined(Q_OS_FREEBSD)
33# include <sys/cpuset.h>
34#elif defined(Q_OS_BSD4)
35# include <sys/sysctl.h>
36#endif
37#ifdef Q_OS_VXWORKS
38# include <vxCpuLib.h>
39# include <cpuset.h>
40#endif
41
42#ifdef Q_OS_HPUX
43#include <sys/pstat.h>
44#endif
45
46#if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
47#include <sys/prctl.h>
48#endif
49
50#if defined(Q_OS_LINUX) && !defined(SCHED_IDLE)
51// from linux/sched.h
52# define SCHED_IDLE 5
53#endif
54
55#if defined(Q_OS_DARWIN) || !defined(Q_OS_ANDROID) && !defined(Q_OS_OPENBSD) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0)
56#define QT_HAS_THREAD_PRIORITY_SCHEDULING
57#endif
58
59#if defined(Q_OS_QNX)
60#include <sys/neutrino.h>
61#endif
62
63#if defined(Q_OS_OHOS)
64// OHOS SDK defines PTHREAD_CANCEL_DISABLE but lacks matching pthread_*() functions
65// Details in QTBUG-146708
66# undef PTHREAD_CANCEL_DISABLE
67#endif
68
70
71[[maybe_unused]]
72Q_STATIC_LOGGING_CATEGORY(lcQThread, "qt.core.thread", QtWarningMsg)
73
74using namespace QtMiscUtils;
75
76#if QT_CONFIG(thread)
77
78static_assert(sizeof(pthread_t) <= sizeof(Qt::HANDLE));
79
80enum { ThreadPriorityResetFlag = 0x80000000 };
81
82// If we have a way to perform a timed pthread_join(), we will do it if its
83// clock is not worse than the one QWaitCondition is using. This ensures that
84// QThread::wait() only returns after pthread_join() or equivalent has
85// returned, ensuring that the thread has definitely exited.
86//
87// Because only one thread can call this family of functions at a time, we
88// count how many threads are waiting and all but one of them wait on a
89// QWaitCondition, with the joining thread having the responsibility for waking
90// up all others when the joining concludes. If the joining times out, the
91// thread in charge wakes up one of the other waiters (if there's any) to
92// assume responsibility for joining.
93//
94// If we don't have a way to perform timed pthread_join(), then we don't try
95// joining a all. All waiting threads will wait for the launched thread to
96// call QWaitCondition::wakeAll(). Note in this case it is possible for the
97// waiting threads to conclude the launched thread has exited before it has.
98//
99// To support this scenario, we start the thread in detached state.
100static constexpr bool UsingPThreadTimedJoin = QT_CONFIG(pthread_clockjoin)
101 || (QT_CONFIG(pthread_timedjoin) && QWaitConditionClockId == CLOCK_REALTIME);
102#if !QT_CONFIG(pthread_clockjoin)
103int pthread_clockjoin_np(...) { return ENOSYS; } // pretend
104#endif
105#if !QT_CONFIG(pthread_timedjoin)
106int pthread_timedjoin_np(...) { return ENOSYS; } // pretend
107#endif
108
109#if QT_CONFIG(broken_threadlocal_dtors)
110// On most modern platforms, the C runtime has a helper function that helps the
111// C++ runtime run the thread_local non-trivial destructors when threads exit
112// and that code ensures that they are run in the correct order on program exit
113// too ([basic.start.term]/2: "The destruction of all constructed objects with
114// thread storage duration within that thread strongly happens before
115// destroying any object with static storage duration."). In the absence of
116// this function, the ordering can be wrong depending on when the first
117// non-trivial thread_local object was created relative to other statics.
118// Moreover, this can be racy and having our own thread_local early in
119// QThreadPrivate::start() made it even more so. See QTBUG-129846 for analysis.
120//
121// There's a good correlation between this C++11 feature and our ability to
122// call QThreadPrivate::cleanup() from destroy_thread_data().
123//
124// https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/libsupc%2B%2B/atexit_thread.cc;hb=releases/gcc-14.2.0#l133
125// https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/libcxxabi/src/cxa_thread_atexit.cpp#L118-L120
126#endif
127//
128// Thus, the destruction of QThreadData is split into 3 steps:
129// - finish()
130// - cleanup()
131// - deref & delete
132//
133// The reason for the first split is that user content may run as a result of
134// the finished() signal, in thread_local destructors or similar, so we don't
135// want to destroy the event dispatcher too soon. If we did, the event
136// dispatcher could get recreated.
137//
138// For auxiliary threads started with QThread, finish() is run as soon as run()
139// returns, while cleanup() and the deref happen at pthread_set_specific
140// destruction time (except for broken_threadlocal_dtors, see above).
141//
142// For auxiliary threads started with something else and adopted as a
143// QAdoptedThread, there's only one choice: all three steps happen at at
144// pthread_set_specific destruction time.
145//
146// Finally, for the thread that called ::exit() (which in most cases happens by
147// returning from the main() function), finish() and cleanup() happen at
148// function-local static destructor time, and the deref & delete happens later,
149// at global static destruction time. That way, we delete the event dispatcher
150// before QLibraryStore's clean up runs and unloads remaining plugins. This
151// strategy was chosen because of crashes observed while running the event
152// dispatcher's destructor, and though the cause of the crash was something
153// else (QFactoryLoader always loads with PreventUnloadHint set), other plugins
154// may still attempt to access QThreadData in their global destructors.
155
156Q_CONSTINIT static thread_local QThreadData *currentThreadData = nullptr;
157
158static void destroy_current_thread_data(QThreadData *data, bool calledFromExit)
159{
160 QThread *thread = data->thread.loadAcquire();
161
162#ifdef Q_OS_APPLE
163 // apparent runtime bug: the trivial has been cleared and we end up
164 // recreating the QThreadData
165 currentThreadData = data;
166#endif
167
168 if (data->isAdopted) {
169 // If this is an adopted thread, then QThreadData owns the QThread and
170 // this is very likely the last reference. These pointers cannot be
171 // null and there is no race.
172 QThreadPrivate *thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread));
173 thread_p->finish(calledFromExit);
174 if constexpr (!QT_CONFIG(broken_threadlocal_dtors))
175 thread_p->cleanup();
176 } else if constexpr (!QT_CONFIG(broken_threadlocal_dtors)) {
177 // We may be racing the QThread destructor in another thread. With
178 // two-phase clean-up enabled, there's also no race because it will
179 // stop in a call to QThread::wait() until we call cleanup().
180 QThreadPrivate *thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread));
181 thread_p->cleanup();
182 } else {
183 // We may be racing the QThread destructor in another thread and it may
184 // have begun destruction; we must not dereference the QThread pointer.
185 }
186}
187
188static void deref_current_thread_data(QThreadData *data)
189{
190 // the QThread object may still have a reference, so this may not delete
191 data->deref();
192
193 // ... but we must reset it to zero before returning so we aren't
194 // leaving a dangling pointer.
195 currentThreadData = nullptr;
196}
197
198static void destroy_auxiliary_thread_data(void *p)
199{
200 auto data = static_cast<QThreadData *>(p);
201 destroy_current_thread_data(data, false);
202 deref_current_thread_data(data);
203}
204
205// Utility functions for getting, setting and clearing thread specific data.
206static QThreadData *get_thread_data()
207{
208 return currentThreadData;
209}
210
211namespace {
212struct QThreadDataDestroyer
213{
214 pthread_key_t key;
215 QThreadDataDestroyer() noexcept
216 {
217 pthread_key_create(&key, &destroy_auxiliary_thread_data);
218 }
219 ~QThreadDataDestroyer()
220 {
221 // running global static destructors upon ::exit()
222 if (QThreadData *data = get_thread_data())
223 deref_current_thread_data(data);
224 pthread_key_delete(key);
225 }
226
227 struct EarlyMainThread {
228 EarlyMainThread() { QThreadStoragePrivate::init(); }
229 ~EarlyMainThread()
230 {
231 // running function-local destructors upon ::exit()
232 if (QThreadData *data = get_thread_data())
233 destroy_current_thread_data(data, true);
234 }
235 };
236};
237}
238#if QT_SUPPORTS_INIT_PRIORITY
239Q_DECL_INIT_PRIORITY(10)
240#endif
241static QThreadDataDestroyer threadDataDestroyer; // intentional non-trivial init & destruction
242
243static void set_thread_data(QThreadData *data) noexcept
244{
245 if (data) {
246 // As noted above: one global static for the thread that called
247 // ::exit() (which may not be a Qt thread) and the pthread_key_t for
248 // all others.
249 static QThreadDataDestroyer::EarlyMainThread currentThreadCleanup;
250 pthread_setspecific(threadDataDestroyer.key, data);
251 }
252 currentThreadData = data;
253}
254
255template <typename T>
256static typename std::enable_if<std::is_integral_v<T>, Qt::HANDLE>::type to_HANDLE(T id)
257{
258 return reinterpret_cast<Qt::HANDLE>(static_cast<intptr_t>(id));
259}
260
261template <typename T>
262static typename std::enable_if<std::is_integral_v<T>, T>::type from_HANDLE(Qt::HANDLE id)
263{
264 return static_cast<T>(reinterpret_cast<intptr_t>(id));
265}
266
267template <typename T>
268static typename std::enable_if<std::is_pointer_v<T>, Qt::HANDLE>::type to_HANDLE(T id)
269{
270 return id;
271}
272
273template <typename T>
274static typename std::enable_if<std::is_pointer_v<T>, T>::type from_HANDLE(Qt::HANDLE id)
275{
276 return static_cast<T>(id);
277}
278
279void QThreadData::clearCurrentThreadData()
280{
281 set_thread_data(nullptr);
282}
283
284QThreadData *QThreadData::currentThreadData() noexcept
285{
286 return get_thread_data();
287}
288
289QThreadData *QThreadData::createCurrentThreadData()
290{
291 Q_ASSERT(!currentThreadData());
292
293 QThreadData *data = new QThreadData();
294
295 // This needs to be called prior to new QAdoptedThread() to avoid
296 // recursion (see qobject.cpp).
297 set_thread_data(data);
298
299 QT_TRY {
300 data->thread.storeRelease(new QAdoptedThread(data));
301 } QT_CATCH(...) {
302 deref_current_thread_data(data);
303 QT_RETHROW;
304 }
305 return data;
306}
307
308void QAdoptedThread::init()
309{
310}
311
312/*
313 QThreadPrivate
314*/
315
316extern "C" {
317typedef void *(*QtThreadCallback)(void *);
318}
319
320#endif // QT_CONFIG(thread)
321
322QAbstractEventDispatcher *QThreadPrivate::createEventDispatcher(QThreadData *data)
323{
324 Q_UNUSED(data);
325#if defined(Q_OS_DARWIN)
326 bool ok = false;
327 int value = qEnvironmentVariableIntValue("QT_EVENT_DISPATCHER_CORE_FOUNDATION", &ok);
328 if (ok && value > 0)
329 return new QEventDispatcherCoreFoundation;
330 else
331 return new QEventDispatcherUNIX;
332#elif defined(Q_OS_WASM)
333 return new QEventDispatcherWasm();
334#elif !defined(QT_NO_GLIB)
335 const bool isQtMainThread = data->thread.loadAcquire() == QCoreApplicationPrivate::mainThread();
336 if (qEnvironmentVariableIsEmpty("QT_NO_GLIB")
337 && (isQtMainThread || qEnvironmentVariableIsEmpty("QT_NO_THREADED_GLIB"))
338 && QEventDispatcherGlib::versionSupported())
339 return new QEventDispatcherGlib;
340 else
341 return new QEventDispatcherUNIX;
342#else
343 return new QEventDispatcherUNIX;
344#endif
345}
346
347#if QT_CONFIG(thread)
348
349template <typename String>
350static void setCurrentThreadName(QThread *thr, String &objectName)
351{
352 auto setit = [](const char *name) {
353# if defined(Q_OS_LINUX)
354 prctl(PR_SET_NAME, name);
355# elif defined(Q_OS_DARWIN)
356 pthread_setname_np(name);
357# elif defined(Q_OS_OPENBSD)
358 pthread_set_name_np(pthread_self(), name);
359# elif defined(Q_OS_QNX) || defined(Q_OS_BSD4)
360 pthread_setname_np(pthread_self(), name);
361# else
362 Q_UNUSED(name)
363# endif
364 };
365 if (Q_LIKELY(objectName.isEmpty()))
366 setit(thr->metaObject()->className());
367 else
368 setit(std::exchange(objectName, {}).toLocal8Bit());
369}
370
371// Handling of exceptions and cancellations for start(), finish() and cleanup()
372//
373// These routines expect that the user code throw no exceptions. Exiting
374// start() with an exception should cause std::terminate to be called. Thread
375// cancellations are allowed: if one is detected, the implementation is
376// expected to cleanly call QThreadPrivate::finish(), emit the necessary
377// signals and notifications, and clean up after itself. [Note there's a small
378// race between QThread::start() returning and QThreadPrivate::start() turning
379// cancellations off, during which time no finish() is called.]
380//
381// These routines implement application termination by unexpected exceptions by
382// simply not having any try/catch block at all. As start() is called directly
383// from the C library's PThread runtime, there should be no active C++
384// try/catch block (### if we ever change this to std::thread, the assumption
385// needs to be rechecked, though both libc++ and libstdc++ at the time of
386// writing are try/catch-free). [except.handle]/8 says:
387//
388// > If no matching handler is found, the function std::terminate is invoked;
389// > whether or not the stack is unwound before this invocation of std::terminate
390// > is implementation-defined.
391//
392// Both major implementations of Unix C++ Standard Libraries terminate without
393// unwinding, which is useful to detect the unhandled exception in post-mortem
394// debugging. This code adds no try/catch to retain that ability.
395//
396// Because of that, we could have marked these functions noexcept and ignored
397// exception safety. We don't because of PThread cancellations. The GNU libc
398// implements PThread cancellations using stack unwinding, so a cancellation
399// *will* unwind the stack and *will* execute our C++ destructors, unlike
400// exceptions. Therefore, our code in start() must be exception-safe after we
401// turn cancellations back on, and until we turn them off again in finish().
402//
403// Everywhere else, PThread cancellations are handled without unwinding the
404// stack.
405
406static void setCancellationEnabled(bool enable)
407{
408#ifdef PTHREAD_CANCEL_DISABLE
409 if (enable) {
410 // may unwind the stack, see above
411 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, nullptr);
412 pthread_testcancel();
413 } else {
414 // this doesn't unwind the stack
415 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, nullptr);
416 }
417#else
418 Q_UNUSED(enable)
419#endif
420}
421
422void *QThreadPrivate::start(void *arg)
423{
424 setCancellationEnabled(false);
425
426 QThread *thr = reinterpret_cast<QThread *>(arg);
427 QThreadData *data = QThreadData::get2(thr);
428
429 // this ensures the thread-local is created as early as possible
430 set_thread_data(data);
431
432 // If a QThread is restarted, reuse the QBindingStatus, too
433 data->reuseBindingStatusForNewNativeThread();
434
435 pthread_cleanup_push([](void *arg) { static_cast<QThread *>(arg)->d_func()->finish(); }, arg);
436 { // pthread cancellation protection
437
438 // The functions called in this block do not usually throw (but have
439 // qWarning/qCDebug, which may throw std::bad_alloc).
440 {
441 QMutexLocker locker(&thr->d_func()->mutex);
442
443 // do we need to reset the thread priority?
444 if (thr->d_func()->priority & ThreadPriorityResetFlag) {
445 thr->d_func()->setPriority(QThread::Priority(thr->d_func()->priority & ~ThreadPriorityResetFlag));
446 }
447
448 // threadId is set in QThread::start()
449 Q_ASSERT(data->threadId.loadRelaxed() == QThread::currentThreadId());
450
451 data->ref();
452 data->quitNow = thr->d_func()->exited;
453 }
454
455 // Sets the name of the current thread. We can only do this
456 // when the thread is starting, as we don't have a cross
457 // platform way of setting the name of an arbitrary thread.
458 setCurrentThreadName(thr, thr->d_func()->objectName);
459
460 // Re-enable cancellations before calling out to user code in run(),
461 // allowing the event dispatcher to abort this thread starting (exceptions
462 // aren't allowed to do that). This will also deliver a pending
463 // cancellation queued either by a slot connected to started() or by
464 // another thread using QThread::terminate().
465 setCancellationEnabled(true);
466
467 data->ensureEventDispatcher();
468 data->eventDispatcher.loadRelaxed()->startingUp();
469
470 emit thr->started(QThread::QPrivateSignal());
471
472 thr->run();
473 }
474
475 // This calls finish(); later, the currentThreadCleanup thread-local
476 // destructor will call cleanup().
477 pthread_cleanup_pop(1);
478 return nullptr;
479}
480
481void QThreadPrivate::finish(bool calledFromExit)
482{
483 QThreadPrivate *d = this;
484 QThread *thr = q_func();
485
486 // Disable cancellation; we're already in the finishing touches of this
487 // thread, and we don't want cleanup to be disturbed by
488 // abi::__forced_unwind being thrown from all kinds of functions.
489 setCancellationEnabled(false);
490
491 QMutexLocker locker(&d->mutex);
492
493 d->threadState = QThreadPrivate::Finishing;
494 locker.unlock();
495 emit thr->finished(QThread::QPrivateSignal());
496 QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
497
498 QThreadStoragePrivate::finish(&d->data->tls, calledFromExit);
499
500 if constexpr (QT_CONFIG(broken_threadlocal_dtors))
501 cleanup();
502}
503
504void QThreadPrivate::cleanup()
505{
506 QThreadPrivate *d = this;
507
508 // Disable cancellation again: we did it above, but some user code
509 // running between finish() and cleanup() may have turned them back on.
510 setCancellationEnabled(false);
511
512 QMutexLocker locker(&d->mutex);
513 d->priority = QThread::InheritPriority;
514
515 QAbstractEventDispatcher *eventDispatcher = d->data->eventDispatcher.loadRelaxed();
516 if (eventDispatcher) {
517 d->data->eventDispatcher = nullptr;
518 locker.unlock();
519 eventDispatcher->closingDown();
520 delete eventDispatcher;
521 locker.relock();
522 }
523
524 d->interruptionRequested.store(false, std::memory_order_relaxed);
525
526 d->wakeAll();
527}
528
529
530/**************************************************************************
531 ** QThread
532 *************************************************************************/
533
534/*
535 CI tests fails on ARM architectures if we try to use the assembler, so
536 stick to the pthread version there. The assembler would be
537
538 // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0344k/Babeihid.html
539 asm volatile ("mrc p15, 0, %0, c13, c0, 3" : "=r" (tid));
540
541 and
542
543 // see glibc/sysdeps/aarch64/nptl/tls.h
544 asm volatile ("mrs %0, tpidr_el0" : "=r" (tid));
545
546 for 32 and 64bit versions, respectively.
547*/
548Qt::HANDLE QThread::currentThreadIdImpl() noexcept
549{
550 return to_HANDLE(pthread_self());
551}
552
553#if defined(QT_LINUXBASE) && !defined(_SC_NPROCESSORS_ONLN)
554// LSB doesn't define _SC_NPROCESSORS_ONLN.
555# define _SC_NPROCESSORS_ONLN 84
556#endif
557
558#ifdef Q_OS_WASM
559int QThreadPrivate::idealThreadCount = 1;
560#endif
561
562#if QT_CONFIG(trivial_auto_var_init_pattern) && defined(Q_CC_GNU_ONLY)
563// Don't pre-fill the automatic-storage arrays used in this function
564// (important for the FreeBSD & Linux code using a VLA).
565__attribute__((optimize("trivial-auto-var-init=uninitialized")))
566#endif
567int QThread::idealThreadCount() noexcept
568{
569 int cores = 1;
570
571#if defined(Q_OS_HPUX)
572 // HP-UX
573 struct pst_dynamic psd;
574 if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) == -1) {
575 perror("pstat_getdynamic");
576 } else {
577 cores = (int)psd.psd_proc_cnt;
578 }
579#elif (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) || defined(Q_OS_FREEBSD)
580 QT_WARNING_PUSH
581# if defined(Q_CC_CLANG) && Q_CC_CLANG >= 1800
582 QT_WARNING_DISABLE_CLANG("-Wvla-cxx-extension")
583# endif
584
585 // get the number of threads we're assigned, not the total in the system
586 constexpr qsizetype MaxCpuCount = 1024 * 1024;
587 constexpr qsizetype MaxCpuSetArraySize = MaxCpuCount / sizeof(cpu_set_t) / 8;
588 qsizetype size = 1;
589 do {
590 cpu_set_t cpuset[size];
591 if (sched_getaffinity(0, sizeof(cpu_set_t) * size, cpuset) == 0) {
592 cores = CPU_COUNT_S(sizeof(cpu_set_t) * size, cpuset);
593 break;
594 }
595 size *= 4;
596 } while (size < MaxCpuSetArraySize);
597 QT_WARNING_POP
598#elif defined(Q_OS_BSD4)
599 // OpenBSD, NetBSD, BSD/OS, Darwin (macOS, iOS, etc.)
600 size_t len = sizeof(cores);
601 int mib[2];
602 mib[0] = CTL_HW;
603 mib[1] = HW_NCPU;
604 if (sysctl(mib, 2, &cores, &len, NULL, 0) != 0) {
605 perror("sysctl");
606 }
607#elif defined(Q_OS_INTEGRITY)
608#if (__INTEGRITY_MAJOR_VERSION >= 10)
609 // Integrity V10+ does support multicore CPUs
610 Value processorCount;
611 if (GetProcessorCount(CurrentTask(), &processorCount) == 0)
612 cores = processorCount;
613 else
614#endif
615 // as of aug 2008 Integrity only supports one single core CPU
616 cores = 1;
617#elif defined(Q_OS_VXWORKS)
618 cpuset_t cpus = vxCpuEnabledGet();
619 cores = 0;
620
621 // 128 cores should be enough for everyone ;)
622 for (int i = 0; i < 128 && !CPUSET_ISZERO(cpus); ++i) {
623 if (CPUSET_ISSET(cpus, i)) {
624 CPUSET_CLR(cpus, i);
625 cores++;
626 }
627 }
628#elif defined(Q_OS_WASM)
629 cores = QThreadPrivate::idealThreadCount;
630#else
631 // the rest: Solaris, AIX, Tru64
632 cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
633 if (cores == -1)
634 return 1;
635#endif
636 return cores;
637}
638
639void QThread::yieldCurrentThread()
640{
641 sched_yield();
642}
643
644#endif // QT_CONFIG(thread)
645
646static void qt_nanosleep(timespec amount)
647{
648 // We'd like to use clock_nanosleep.
649 //
650 // But clock_nanosleep is from POSIX.1-2001 and both are *not*
651 // affected by clock changes when using relative sleeps, even for
652 // CLOCK_REALTIME.
653 //
654 // nanosleep is POSIX.1-1993
655
656 int r;
657 QT_EINTR_LOOP(r, nanosleep(&amount, &amount));
658}
659
660void QThread::sleep(unsigned long secs)
661{
662 sleep(std::chrono::seconds{secs});
663}
664
665void QThread::msleep(unsigned long msecs)
666{
667 sleep(std::chrono::milliseconds{msecs});
668}
669
670void QThread::usleep(unsigned long usecs)
671{
672 sleep(std::chrono::microseconds{usecs});
673}
674
675void QThread::sleep(std::chrono::nanoseconds nsec)
676{
677 qt_nanosleep(durationToTimespec(nsec));
678}
679
680#if QT_CONFIG(thread)
681
682#ifdef QT_HAS_THREAD_PRIORITY_SCHEDULING
683#if defined(Q_OS_QNX)
684static bool calculateUnixPriority(int priority, int *sched_policy, int *sched_priority)
685{
686 // On QNX, NormalPriority is mapped to 10. A QNX system could use a value different
687 // than 10 for the "normal" priority but it's difficult to achieve this so we'll
688 // assume that no one has ever created such a system. This makes the mapping from
689 // Qt priorities to QNX priorities lopsided. There's usually more space available
690 // to map into above the "normal" priority than below it. QNX also has a privileged
691 // priority range (for threads that assist the kernel). We'll assume that no Qt
692 // thread needs to use priorities in that range.
693 int priority_norm = 10;
694 // _sched_info::priority_priv isn't documented. You'd think that it's the start of the
695 // privileged priority range but it's actually the end of the unpriviledged range.
696 struct _sched_info info;
697 if (SchedInfo_r(0, *sched_policy, &info) != EOK)
698 return false;
699
700 if (priority == QThread::IdlePriority) {
701 *sched_priority = info.priority_min;
702 return true;
703 }
704
705 if (priority_norm < info.priority_min)
706 priority_norm = info.priority_min;
707 if (priority_norm > info.priority_priv)
708 priority_norm = info.priority_priv;
709
710 int to_min, to_max;
711 int from_min, from_max;
712 int prio;
713 if (priority < QThread::NormalPriority) {
714 to_min = info.priority_min;
715 to_max = priority_norm;
716 from_min = QThread::LowestPriority;
717 from_max = QThread::NormalPriority;
718 } else {
719 to_min = priority_norm;
720 to_max = info.priority_priv;
721 from_min = QThread::NormalPriority;
722 from_max = QThread::TimeCriticalPriority;
723 }
724
725 prio = ((priority - from_min) * (to_max - to_min)) / (from_max - from_min) + to_min;
726 prio = qBound(to_min, prio, to_max);
727
728 *sched_priority = prio;
729 return true;
730}
731#else
732// Does some magic and calculate the Unix scheduler priorities
733// sched_policy is IN/OUT: it must be set to a valid policy before calling this function
734// sched_priority is OUT only
735static bool calculateUnixPriority(int priority, int *sched_policy, int *sched_priority)
736{
737#ifdef SCHED_IDLE
738 if (priority == QThread::IdlePriority) {
739 *sched_policy = SCHED_IDLE;
740 *sched_priority = 0;
741 return true;
742 }
743 const int lowestPriority = QThread::LowestPriority;
744#else
745 const int lowestPriority = QThread::IdlePriority;
746#endif
747 const int highestPriority = QThread::TimeCriticalPriority;
748
749 int prio_min;
750 int prio_max;
751#if defined(Q_OS_VXWORKS)
752 // for other scheduling policies than SCHED_RR or SCHED_FIFO
753 prio_min = SCHED_FIFO_LOW_PRI;
754 prio_max = SCHED_FIFO_HIGH_PRI;
755
756 if ((*sched_policy == SCHED_RR) || (*sched_policy == SCHED_FIFO))
757#endif
758 {
759 prio_min = sched_get_priority_min(*sched_policy);
760 prio_max = sched_get_priority_max(*sched_policy);
761 }
762
763 if (prio_min == -1 || prio_max == -1)
764 return false;
765
766 int prio;
767 // crudely scale our priority enum values to the prio_min/prio_max
768 prio = ((priority - lowestPriority) * (prio_max - prio_min) / highestPriority) + prio_min;
769 prio = qMax(prio_min, qMin(prio_max, prio));
770
771 *sched_priority = prio;
772 return true;
773}
774#endif
775#endif
776
777void QThread::start(Priority priority)
778{
779 Q_D(QThread);
780 QMutexLocker locker(&d->mutex);
781
782 if (d->threadState == QThreadPrivate::Finishing)
783 d->wait(locker, QDeadlineTimer::Forever);
784
785 if (d->threadState == QThreadPrivate::Running)
786 return;
787
788 d->threadState = QThreadPrivate::Running;
789 d->returnCode = 0;
790 d->exited = false;
791 d->interruptionRequested.store(false, std::memory_order_relaxed);
792 d->terminated = false;
793
794 pthread_attr_t attr;
795 pthread_attr_init(&attr);
796 if constexpr (!UsingPThreadTimedJoin)
797 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
798 if (d->serviceLevel != QThread::QualityOfService::Auto) {
799#ifdef Q_OS_DARWIN
800 pthread_attr_set_qos_class_np(&attr, d->nativeQualityOfServiceClass(), 0);
801#else
802 // No such functionality on other OSes. We promise "no effect", so don't
803 // print a warning either.
804#endif
805 }
806
807 d->priority = priority;
808
809#if defined(QT_HAS_THREAD_PRIORITY_SCHEDULING)
810 switch (priority) {
811 case InheritPriority:
812 {
813 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
814 break;
815 }
816
817 default:
818 {
819 int sched_policy;
820 if (pthread_attr_getschedpolicy(&attr, &sched_policy) != 0) {
821 // failed to get the scheduling policy, don't bother
822 // setting the priority
823 qWarning("QThread::start: Cannot determine default scheduler policy");
824 break;
825 }
826
827 int prio;
828 if (!calculateUnixPriority(priority, &sched_policy, &prio)) {
829 // failed to get the scheduling parameters, don't
830 // bother setting the priority
831 qWarning("QThread::start: Cannot determine scheduler priority range");
832 break;
833 }
834
835 sched_param sp;
836 sp.sched_priority = prio;
837
838 if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0
839 || pthread_attr_setschedpolicy(&attr, sched_policy) != 0
840 || pthread_attr_setschedparam(&attr, &sp) != 0) {
841 // could not set scheduling hints, fallback to inheriting them
842 // we'll try again from inside the thread
843 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
844 d->priority = qToUnderlying(priority) | ThreadPriorityResetFlag;
845 }
846 break;
847 }
848 }
849#endif // QT_HAS_THREAD_PRIORITY_SCHEDULING
850
851
852 if (d->stackSize > 0) {
853#if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0 > 0)
854 int code = pthread_attr_setstacksize(&attr, d->stackSize);
855#else
856 int code = ENOSYS; // stack size not supported, automatically fail
857#endif // _POSIX_THREAD_ATTR_STACKSIZE
858
859 if (code) {
860 qErrnoWarning(code, "QThread::start: Thread stack size error");
861
862 // we failed to set the stacksize, and as the documentation states,
863 // the thread will fail to run...
864 d->threadState = QThreadPrivate::NotStarted;
865 return;
866 }
867 }
868
869#ifdef Q_OS_INTEGRITY
870 if (Q_LIKELY(objectName().isEmpty()))
871 pthread_attr_setthreadname(&attr, metaObject()->className());
872 else
873 pthread_attr_setthreadname(&attr, objectName().toLocal8Bit());
874#else
875 // avoid interacting with the binding system
876 d->objectName = d->extraData ? d->extraData->objectName.valueBypassingBindings()
877 : QString();
878#endif
879
880 pthread_t threadId;
881 int code = pthread_create(&threadId, &attr, QThreadPrivate::start, this);
882 if (code == EPERM) {
883 // caller does not have permission to set the scheduling
884 // parameters/policy
885#if defined(QT_HAS_THREAD_PRIORITY_SCHEDULING)
886 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
887#endif
888 code = pthread_create(&threadId, &attr, QThreadPrivate::start, this);
889 }
890 d->data->threadId.storeRelaxed(to_HANDLE(threadId));
891
892 pthread_attr_destroy(&attr);
893
894 if (code) {
895 qErrnoWarning(code, "QThread::start: Thread creation error");
896
897 d->threadState = QThreadPrivate::NotStarted;
898 d->data->threadId.storeRelaxed(nullptr);
899 }
900}
901
902void QThread::terminate()
903{
904#if !defined(Q_OS_ANDROID) && !defined(Q_OS_OHOS)
905 Q_D(QThread);
906 QMutexLocker locker(&d->mutex);
907
908 const auto id = d->data->threadId.loadRelaxed();
909 if (!id)
910 return;
911
912 if (d->terminated) // don't try again, avoids killing the wrong thread on threadId reuse (ABA)
913 return;
914
915 d->terminated = true;
916
917 const bool selfCancelling = d->data == get_thread_data();
918 if (selfCancelling) {
919 // Posix doesn't seem to specify whether the stack of cancelled threads
920 // is unwound, and there's nothing preventing a QThread from
921 // terminate()ing itself, so drop the mutex before calling
922 // pthread_cancel():
923 locker.unlock();
924 }
925
926 if (int code = pthread_cancel(from_HANDLE<pthread_t>(id))) {
927 if (selfCancelling)
928 locker.relock();
929 d->terminated = false; // allow to try again
930 qErrnoWarning(code, "QThread::start: Thread termination error");
931 }
932#endif
933}
934
935static void wakeAllInternal(QThreadPrivate *d)
936{
937 d->threadState = QThreadPrivate::Finished;
938 if (d->waiters)
939 d->thread_done.wakeAll();
940}
941
942inline void QThreadPrivate::wakeAll()
943{
944 if (data->isAdopted || !UsingPThreadTimedJoin)
945 wakeAllInternal(this);
946}
947
948bool QThreadPrivate::wait(QMutexLocker<QMutex> &locker, QDeadlineTimer deadline)
949{
950 constexpr int HasJoinerBit = int(0x8000'0000); // a.k.a. sign bit
951 struct timespec ts, *pts = nullptr;
952 if (!deadline.isForever()) {
953 ts = deadlineToAbstime(deadline);
954 pts = &ts;
955 }
956
957 auto doJoin = [&] {
958 // pthread_join() & family are cancellation points
959 struct CancelState {
960 QThreadPrivate *d;
961 QMutexLocker<QMutex> *locker;
962 int joinResult = ETIMEDOUT;
963 static void run(void *arg) { static_cast<CancelState *>(arg)->run(); }
964 void run()
965 {
966 locker->relock();
967 if (joinResult == ETIMEDOUT && d->waiters)
968 d->thread_done.wakeOne();
969 else if (joinResult == 0)
970 wakeAllInternal(d);
971 d->waiters &= ~HasJoinerBit;
972 }
973 } nocancel = { this, &locker };
974 int &r = nocancel.joinResult;
975
976 // we're going to perform the join, so don't let other threads do it
977 waiters |= HasJoinerBit;
978 locker.unlock();
979
980 pthread_cleanup_push(&CancelState::run, &nocancel);
981 pthread_t thrId = from_HANDLE<pthread_t>(data->threadId.loadRelaxed());
982 if constexpr (QT_CONFIG(pthread_clockjoin))
983 r = pthread_clockjoin_np(thrId, nullptr, QSteadyClockClockId, pts);
984 else
985 r = pthread_timedjoin_np(thrId, nullptr, pts);
986 Q_ASSERT(r == 0 || r == ETIMEDOUT);
987 pthread_cleanup_pop(1);
988
989 Q_ASSERT(waiters >= 0);
990 return r != ETIMEDOUT;
991 };
992 Q_ASSERT(threadState != QThreadPrivate::Finished);
993 Q_ASSERT(locker.isLocked());
994
995 bool result = false;
996
997 // both branches call cancellation points
998 ++waiters;
999 bool mustJoin = (waiters & HasJoinerBit) == 0;
1000 pthread_cleanup_push([](void *ptr) {
1001 --(*static_cast<decltype(waiters) *>(ptr));
1002 }, &waiters);
1003 for (;;) {
1004 if (UsingPThreadTimedJoin && mustJoin && !data->isAdopted) {
1005 result = doJoin();
1006 break;
1007 }
1008 if (!thread_done.wait(locker.mutex(), deadline))
1009 break; // timed out
1010 result = threadState == QThreadPrivate::Finished;
1011 if (result)
1012 break; // success
1013 mustJoin = (waiters & HasJoinerBit) == 0;
1014 }
1015 pthread_cleanup_pop(1);
1016
1017 return result;
1018}
1019
1020void QThread::setTerminationEnabled(bool enabled)
1021{
1022 QThread *thr = currentThread();
1023 Q_ASSERT_X(thr != nullptr, "QThread::setTerminationEnabled()",
1024 "Current thread was not started with QThread.");
1025
1026 Q_UNUSED(thr);
1027 setCancellationEnabled(enabled);
1028}
1029
1030// Caller must lock the mutex
1031void QThreadPrivate::setPriority(QThread::Priority threadPriority)
1032{
1033 priority = threadPriority;
1034
1035 // copied from start() with a few modifications:
1036
1037#ifdef QT_HAS_THREAD_PRIORITY_SCHEDULING
1038 int sched_policy;
1039 sched_param param;
1040
1041 if (pthread_getschedparam(from_HANDLE<pthread_t>(data->threadId.loadRelaxed()), &sched_policy, &param) != 0) {
1042 // failed to get the scheduling policy, don't bother setting
1043 // the priority
1044 qWarning("QThread::setPriority: Cannot get scheduler parameters");
1045 return;
1046 }
1047
1048 int prio;
1049 if (!calculateUnixPriority(priority, &sched_policy, &prio)) {
1050 // failed to get the scheduling parameters, don't
1051 // bother setting the priority
1052 qWarning("QThread::setPriority: Cannot determine scheduler priority range");
1053 return;
1054 }
1055
1056 param.sched_priority = prio;
1057 int status = pthread_setschedparam(from_HANDLE<pthread_t>(data->threadId.loadRelaxed()), sched_policy, &param);
1058
1059# ifdef SCHED_IDLE
1060 // were we trying to set to idle priority and failed?
1061 if (status == -1 && sched_policy == SCHED_IDLE && errno == EINVAL) {
1062 // reset to lowest priority possible
1063 pthread_getschedparam(from_HANDLE<pthread_t>(data->threadId.loadRelaxed()), &sched_policy, &param);
1064 param.sched_priority = sched_get_priority_min(sched_policy);
1065 pthread_setschedparam(from_HANDLE<pthread_t>(data->threadId.loadRelaxed()), sched_policy, &param);
1066 }
1067# else
1068 Q_UNUSED(status);
1069# endif // SCHED_IDLE
1070#endif
1071}
1072
1073void QThreadPrivate::setQualityOfServiceLevel(QThread::QualityOfService qosLevel)
1074{
1075 [[maybe_unused]]
1076 Q_Q(QThread);
1077 serviceLevel = qosLevel;
1078#ifdef Q_OS_DARWIN
1079 qCDebug(lcQThread) << "Setting thread QoS class to" << serviceLevel << "for thread" << q;
1080 pthread_set_qos_class_self_np(nativeQualityOfServiceClass(), 0);
1081#endif
1082}
1083
1084#ifdef Q_OS_DARWIN
1085qos_class_t QThreadPrivate::nativeQualityOfServiceClass() const
1086{
1087 // @note Consult table[0] to see what the levels mean
1088 // [0] https://developer.apple.com/library/archive/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/PrioritizeWorkAtTheTaskLevel.html#//apple_ref/doc/uid/TP40013929-CH35-SW5
1089 // There are more levels but they have two other documented ones,
1090 // QOS_CLASS_BACKGROUND, which is below UTILITY, but has no guarantees
1091 // for scheduling (ie. the OS could choose to never give it CPU time),
1092 // and QOS_CLASS_USER_INITIATED, documented as being intended for
1093 // user-initiated actions, such as loading a text document.
1094 switch (serviceLevel) {
1095 case QThread::QualityOfService::Auto:
1096 return QOS_CLASS_DEFAULT;
1097 case QThread::QualityOfService::High:
1098 return QOS_CLASS_USER_INTERACTIVE;
1099 case QThread::QualityOfService::Eco:
1100 return QOS_CLASS_UTILITY;
1101 }
1102 Q_UNREACHABLE_RETURN(QOS_CLASS_DEFAULT);
1103}
1104#endif
1105
1106#endif // QT_CONFIG(thread)
1107
1108QT_END_NAMESPACE
static QAbstractEventDispatcher * createEventDispatcher(QThreadData *data)
Combined button and popup list for selecting options.
#define __has_include(x)
#define Q_STATIC_LOGGING_CATEGORY(name,...)
static void qt_nanosleep(timespec amount)