@@ -13,6 +13,10 @@ extern "C" {
1313# error "this header requires Py_BUILD_CORE define"
1414#endif
1515
16+ #if defined(MS_WINDOWS)
17+ # include < emmintrin.h> // _mm_pause()
18+ #endif
19+
1620// _Py_UNLOCKED is defined as 0 and _Py_LOCKED as 1 in Include/cpython/pylock.h
1721#define _Py_HAS_PARKED 2
1822#define _Py_ONCE_INITIALIZED 4
@@ -70,10 +74,25 @@ PyMutex_LockFlags(PyMutex *m, _PyLockFlags flags)
7074// error messages) otherwise returns 0.
7175extern int _PyMutex_TryUnlock (PyMutex *m);
7276
73- // Yield the processor using a lightweight CPU pause hint (e.g., x86 PAUSE,
74- // AArch64 WFE). Falls back to sched_yield()/SwitchToThread() on platforms
75- // without a known pause instruction.
76- extern void _Py_yield (void );
77+ // Lightweight CPU pause hint for spin-wait loops (e.g., x86 PAUSE, AArch64 WFE).
78+ // Falls back to sched_yield() on platforms without a known pause instruction.
79+ static inline void
80+ _Py_yield (void )
81+ {
82+ #if defined(__x86_64__) || defined(__i386__)
83+ __asm__ volatile (" pause" ::: " memory" );
84+ #elif defined(__aarch64__)
85+ __asm__ volatile (" wfe" );
86+ #elif defined(__arm__) && __ARM_ARCH >= 7
87+ __asm__ volatile (" yield" ::: " memory" );
88+ #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
89+ __asm__ volatile (" or 27,27,27" ::: " memory" );
90+ #elif defined(MS_WINDOWS)
91+ _mm_pause ();
92+ #elif defined(HAVE_SCHED_H)
93+ sched_yield ();
94+ #endif
95+ }
7796
7897
7998// PyEvent is a one-time event notification
0 commit comments