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