Skip to content

Commit d1a986c

Browse files
committed
Address code review
1 parent 9b90b96 commit d1a986c

2 files changed

Lines changed: 23 additions & 34 deletions

File tree

Include/internal/pycore_lock.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
7175
extern 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

Python/lock.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88
#include "pycore_time.h" // _PyTime_Add()
99
#include "pycore_stats.h" // FT_STAT_MUTEX_SLEEP_INC()
1010

11-
#ifdef MS_WINDOWS
12-
# ifndef WIN32_LEAN_AND_MEAN
13-
# define WIN32_LEAN_AND_MEAN
14-
# endif
15-
# include <windows.h> // SwitchToThread()
16-
#elif defined(HAVE_SCHED_H)
17-
# include <sched.h> // sched_yield()
18-
#endif
1911

2012
// If a thread waits on a lock for longer than TIME_TO_BE_FAIR_NS (1 ms), then
2113
// the unlocking thread directly hands off ownership of the lock. This avoids
@@ -40,28 +32,6 @@ struct mutex_entry {
4032
int handed_off;
4133
};
4234

43-
void
44-
_Py_yield(void)
45-
{
46-
#if defined(__GNUC__) || defined(__clang__)
47-
# if defined(__x86_64__) || defined(__i386__)
48-
__asm__ volatile ("pause" ::: "memory");
49-
# elif defined(__aarch64__)
50-
__asm__ volatile ("wfe");
51-
# elif defined(__arm__) && __ARM_ARCH >= 7
52-
__asm__ volatile ("yield" ::: "memory");
53-
# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
54-
__asm__ volatile ("or 27,27,27" ::: "memory");
55-
# else
56-
sched_yield();
57-
# endif
58-
#elif defined(MS_WINDOWS)
59-
SwitchToThread();
60-
#elif defined(HAVE_SCHED_H)
61-
sched_yield();
62-
#endif
63-
}
64-
6535
PyLockStatus
6636
_PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
6737
{

0 commit comments

Comments
 (0)