1// Copyright (C) 2023 The Qt Company Ltd.
2// Copyright (C) 2023 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#ifndef QYIELDCPU_H
7#define QYIELDCPU_H
8
9#include <QtCore/qcompilerdetection.h>
10#include <QtCore/qprocessordetection.h>
11#include <QtCore/qtconfigmacros.h>
12
13#ifdef Q_CC_MSVC_ONLY
14// MSVC defines _YIELD_PROCESSOR() in <xatomic.h>, but as that is a private
15// header, we include the public ones
16# ifdef __cplusplus
17# include <atomic>
18extern "C"
19# endif
20void _mm_pause(void); // the compiler recognizes as intrinsic
21#endif
22
23QT_BEGIN_NAMESPACE
24
25Q_ALWAYS_INLINE
26#ifdef Q_CC_GNU
27__attribute__((artificial))
28#endif
29void qYieldCpu(void) Q_DECL_NOEXCEPT;
30
31void qYieldCpu(void)
32#ifdef __cplusplus
33 noexcept
34#endif
35{
36#if __has_builtin(__yield)
37 __yield(); // Generic
38#elif defined(_YIELD_PROCESSOR) && defined(Q_CC_MSVC)
39 _YIELD_PROCESSOR(); // Generic; MSVC's <atomic>
40
41#elif __has_builtin(__builtin_ia32_pause)
42 __builtin_ia32_pause();
43#elif defined(Q_PROCESSOR_X86) && defined(Q_CC_GNU)
44 // GCC < 10 didn't have __has_builtin()
45 __builtin_ia32_pause();
46#elif defined(Q_PROCESSOR_X86) && defined(Q_CC_MSVC)
47 _mm_pause();
48#elif defined(Q_PROCESSOR_X86)
49 __asm__("pause"); // hopefully asm() works in this compiler
50
51#elif __has_builtin(__builtin_arm_yield)
52 __builtin_arm_yield();
53#elif defined(Q_PROCESSOR_ARM) && Q_PROCESSOR_ARM >= 7 && defined(Q_CC_GNU)
54 __asm__("yield"); // this works everywhere
55
56#elif defined(Q_PROCESSOR_RISCV)
57 __asm__(".word 0x0100000f"); // a.k.a. "pause"
58
59#elif defined(_YIELD_PROCESSOR) && defined(Q_CC_GHS)
60 _YIELD_PROCESSOR; // Green Hills (INTEGRITY), but only on ARM
61#endif
62}
63
64QT_END_NAMESPACE
65
66#endif // QYIELDCPU_H
67

source code of qtbase/src/corelib/thread/qyieldcpu.h