gh-146636: abi3t: Define Py_GIL_DISABLED but do not use it (GH-148142) · python/cpython@fbc1a5b · GitHub
Skip to content

Commit fbc1a5b

Browse files
authored
gh-146636: abi3t: Define Py_GIL_DISABLED but do not use it (GH-148142)
When compiling for abi3t, define Py_GIL_DISABLED, so that users who check it to enable additional locking aren't broken. But also avoid using Py_GIL_DISABLED in Python headers themselves -- abi3 and abi3t ought to be the same except the _Py_OPAQUE_PYOBJECT differences. A check for this is coming in a later PR. It will require rewriting some preprocessor conditions, some of these changes are included in this PR. For _Py_IsOwnedByCurrentThread & supporting functions I opted to move them to a cpython/ header, as they're rather self-contained.
1 parent 5e9d90b commit fbc1a5b

5 files changed

Lines changed: 115 additions & 96 deletions

File tree

Include/cpython/object.h

Lines changed: 79 additions & 0 deletions

Include/moduleobject.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ struct PyModuleDef_Slot {
113113
# define Py_MOD_GIL_NOT_USED ((void *)1)
114114
#endif
115115

116-
#if !defined(Py_LIMITED_API) && defined(Py_GIL_DISABLED)
116+
#if !defined(Py_LIMITED_API)
117+
# if defined(Py_GIL_DISABLED)
117118
PyAPI_FUNC(int) PyUnstable_Module_SetGIL(PyObject *module, void *gil);
119+
# endif
118120
#endif
119121

120122
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15)

Include/object.h

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -186,85 +186,6 @@ typedef struct PyVarObject PyVarObject;
186186
PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
187187
#define Py_Is(x, y) ((x) == (y))
188188

189-
#if defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
190-
PyAPI_FUNC(uintptr_t) _Py_GetThreadLocal_Addr(void);
191-
192-
static inline uintptr_t
193-
_Py_ThreadId(void)
194-
{
195-
uintptr_t tid;
196-
#if defined(_MSC_VER) && defined(_M_X64)
197-
tid = __readgsqword(48);
198-
#elif defined(_MSC_VER) && defined(_M_IX86)
199-
tid = __readfsdword(24);
200-
#elif defined(_MSC_VER) && defined(_M_ARM64)
201-
tid = __getReg(18);
202-
#elif defined(__MINGW32__) && defined(_M_X64)
203-
tid = __readgsqword(48);
204-
#elif defined(__MINGW32__) && defined(_M_IX86)
205-
tid = __readfsdword(24);
206-
#elif defined(__MINGW32__) && defined(_M_ARM64)
207-
tid = __getReg(18);
208-
#elif defined(__i386__)
209-
__asm__("{movl %%gs:0, %0|mov %0, dword ptr gs:[0]}" : "=r" (tid)); // 32-bit always uses GS
210-
#elif defined(__MACH__) && defined(__x86_64__)
211-
__asm__("{movq %%gs:0, %0|mov %0, qword ptr gs:[0]}" : "=r" (tid)); // x86_64 macOSX uses GS
212-
#elif defined(__x86_64__)
213-
__asm__("{movq %%fs:0, %0|mov %0, qword ptr fs:[0]}" : "=r" (tid)); // x86_64 Linux, BSD uses FS
214-
#elif defined(__arm__) && __ARM_ARCH >= 7
215-
__asm__ ("mrc p15, 0, %0, c13, c0, 3\nbic %0, %0, #3" : "=r" (tid));
216-
#elif defined(__aarch64__) && defined(__APPLE__)
217-
__asm__ ("mrs %0, tpidrro_el0" : "=r" (tid));
218-
#elif defined(__aarch64__)
219-
__asm__ ("mrs %0, tpidr_el0" : "=r" (tid));
220-
#elif defined(__powerpc64__)
221-
#if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)
222-
tid = (uintptr_t)__builtin_thread_pointer();
223-
#else
224-
// r13 is reserved for use as system thread ID by the Power 64-bit ABI.
225-
register uintptr_t tp __asm__ ("r13");
226-
__asm__("" : "=r" (tp));
227-
tid = tp;
228-
#endif
229-
#elif defined(__powerpc__)
230-
#if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)
231-
tid = (uintptr_t)__builtin_thread_pointer();
232-
#else
233-
// r2 is reserved for use as system thread ID by the Power 32-bit ABI.
234-
register uintptr_t tp __asm__ ("r2");
235-
__asm__ ("" : "=r" (tp));
236-
tid = tp;
237-
#endif
238-
#elif defined(__s390__) && defined(__GNUC__)
239-
// Both GCC and Clang have supported __builtin_thread_pointer
240-
// for s390 from long time ago.
241-
tid = (uintptr_t)__builtin_thread_pointer();
242-
#elif defined(__riscv)
243-
#if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)
244-
tid = (uintptr_t)__builtin_thread_pointer();
245-
#else
246-
// tp is Thread Pointer provided by the RISC-V ABI.
247-
__asm__ ("mv %0, tp" : "=r" (tid));
248-
#endif
249-
#else
250-
// Fallback to a portable implementation if we do not have a faster
251-
// platform-specific implementation.
252-
tid = _Py_GetThreadLocal_Addr();
253-
#endif
254-
return tid;
255-
}
256-
257-
static inline Py_ALWAYS_INLINE int
258-
_Py_IsOwnedByCurrentThread(PyObject *ob)
259-
{
260-
#ifdef _Py_THREAD_SANITIZER
261-
return _Py_atomic_load_uintptr_relaxed(&ob->ob_tid) == _Py_ThreadId();
262-
#else
263-
return ob->ob_tid == _Py_ThreadId();
264-
#endif
265-
}
266-
#endif
267-
268189
PyAPI_DATA(PyTypeObject) PyLong_Type;
269190
PyAPI_DATA(PyTypeObject) PyBool_Type;
270191

@@ -652,8 +573,10 @@ given type object has a specified feature.
652573
#define _Py_IMMORTAL_FLAGS (1 << 0)
653574
#define _Py_LEGACY_ABI_CHECK_FLAG (1 << 1) /* see PyModuleDef_Init() */
654575
#define _Py_STATICALLY_ALLOCATED_FLAG (1 << 2)
655-
#if defined(Py_GIL_DISABLED) && defined(Py_DEBUG)
656-
#define _Py_TYPE_REVEALED_FLAG (1 << 3)
576+
#if !defined(Py_LIMITED_API)
577+
# if defined(Py_GIL_DISABLED) && defined(Py_DEBUG)
578+
# define _Py_TYPE_REVEALED_FLAG (1 << 3)
579+
# endif
657580
#endif
658581

659582
#define Py_CONSTANT_NONE 0

Include/pyport.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@
7777
# define Py_BUILD_CORE
7878
#endif
7979

80+
#if defined(Py_TARGET_ABI3T)
81+
# if !defined(Py_GIL_DISABLED)
82+
// Define Py_GIL_DISABLED for users' needs. This macro is used to enable
83+
// locking needed in for free-threaded interpreters builds.
84+
# define Py_GIL_DISABLED
85+
# endif
86+
#endif
87+
8088

8189
/**************************************************************************
8290
Symbols and macros to supply platform-independent interfaces to basic

Include/refcount.h

Lines changed: 21 additions & 14 deletions

0 commit comments

Comments
 (0)