Skip to content

Commit e3e3973

Browse files
committed
Address code review
1 parent 6d731a3 commit e3e3973

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

Include/object.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
239239
#define Py_Is(x, y) ((x) == (y))
240240

241241
#if defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
242+
PyAPI_FUNC(uintptr_t) _Py_TSS_Tstate_Addr(void);
243+
242244
static inline uintptr_t
243245
_Py_ThreadId(void)
244246
{
@@ -291,18 +293,12 @@ _Py_ThreadId(void)
291293
__asm__ ("mv %0, tp" : "=r" (tid));
292294
#endif
293295
#elif defined(thread_local) || defined(__GNUC__)
294-
#if defined(thread_local)
295-
static thread_local int __tp = 0;
296-
#else
297-
// Assume that we only support C11 compilers.
298-
static __thread int __tp = 0;
299-
#endif
300296
// gh-112535: Using characteristics of TLS address mapping.
301297
// The address of the thread-local variable is not equal with the actual thread pointer,
302298
// However, it has the property of being determined by loader at runtime, with no duplication of values
303299
// between different threads. So it can be used as the similar role of __builtin_thread_pointer().
304300
// But since it requires offset calculation, this hack is more expensive than __builtin_thread_pointer().
305-
tid = (uintptr_t)&__tp;
301+
tid = _Py_TSS_Tstate_Addr();
306302
#else
307303
# error "define _Py_ThreadId for this platform"
308304
#endif

Include/pystate.h

-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
119119
*/
120120
PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
121121

122-
123122
#ifndef Py_LIMITED_API
124123
# define Py_CPYTHON_PYSTATE_H
125124
# include "cpython/pystate.h"

Python/pystate.c

+10
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,16 @@ _PyThreadState_Bind(PyThreadState *tstate)
19511951
}
19521952
}
19531953

1954+
#if defined(Py_GIL_DISABLED)
1955+
uintptr_t
1956+
_Py_TSS_Tstate_Addr(void) {
1957+
#ifdef HAVE_THREAD_LOCAL
1958+
return (uintptr_t)&_Py_tss_tstate;
1959+
#else
1960+
# error "no supported thread-local variable storage classifier"
1961+
#endif
1962+
}
1963+
#endif
19541964

19551965
/***********************************/
19561966
/* routines for advanced debuggers */

0 commit comments

Comments
 (0)