Skip to content

gh-140544: Always assume that thread locals are available - #140690

Merged
ZeroIntensity merged 7 commits into
python:mainfrom
ZeroIntensity:remove-have-thread-local
Oct 28, 2025
Merged

gh-140544: Always assume that thread locals are available#140690
ZeroIntensity merged 7 commits into
python:mainfrom
ZeroIntensity:remove-have-thread-local

Conversation

@ZeroIntensity

@ZeroIntensityZeroIntensity commented Oct 27, 2025

Copy link
Copy Markdown
Member

Python has required thread local support since 3.12. By assuming that thread locals are always supported, we can improve the performance of third-party extensions by allowing them to access the attached thread and interpreter states directly.

cc @cdce8p

@ZeroIntensityZeroIntensity changed the title gh-140544: Always assume the HAVE_THREAD_LOCAL macro is defined.gh-140544: Always assume that thread locals are availableOct 27, 2025
@cdce8p

cdce8p commented Oct 27, 2025

Copy link
Copy Markdown
Contributor

Hmm, I'm still getting the same error I mentioned earlier.

 In file included from /.../cpython/Include/internal/pycore_object.h:12: (diff)
In file included from /.../cpython/Include/internal/pycore_gc.h:12: (diff)
/.../cpython/Include/internal/pycore_pystate.h:93:8: error: unknown type name '_Py_thread_local' (diff)
extern _Py_thread_local PyThreadState *_Py_tss_tstate; (diff)
^ (diff)
/.../cpython/Include/internal/pycore_pystate.h:93:38: error: expected ';' after top level declarator (diff)
extern _Py_thread_local PyThreadState *_Py_tss_tstate; (diff)
^ (diff)
; (diff)
/.../cpython/Include/internal/pycore_pystate.h:94:8: error: unknown type name '_Py_thread_local' (diff)
extern _Py_thread_local PyInterpreterState *_Py_tss_interp; (diff)
^ (diff)
/.../cpython/Include/internal/pycore_pystate.h:94:43: error: expected ';' after top level declarator (diff)
extern _Py_thread_local PyInterpreterState *_Py_tss_interp; (diff)
^ (diff)
; (diff)
/.../cpython/Include/internal/pycore_pystate.h:119:12: error: use of undeclared identifier '_Py_tss_tstate' (diff)
return _Py_tss_tstate; (diff)
^ (diff)
/.../cpython/Include/internal/pycore_pystate.h:213:12: error: use of undeclared identifier '_Py_tss_interp' (diff)
return _Py_tss_interp; (diff)
^ (diff)
6 errors generated. (diff)

Somehow the # ifdef Py_BUILD_CORE check fails, even though mypyc defines it.
https://github.com/python/mypy/blob/v1.18.2/mypyc/lib-rt/pythonsupport.h#L16-L18

@cdce8p

Copy link
Copy Markdown
Contributor

Do I need to pass Py_BUILD_CORE through to my local cpython build? This is my current build setup

make clean
./configure --with-openssl=$(brew --prefix openssl@3)
make -j4 -s

@ZeroIntensity

ZeroIntensity commented Oct 27, 2025

Copy link
Copy Markdown
MemberAuthor

Hm, you're defining Py_BUILD_COREafterPython.h has been included, so pyport.h isn't seeing it. I'm not sure if that's something we intended to support.

@cdce8p

Copy link
Copy Markdown
Contributor

Hm, you're defining Py_BUILD_COREafterPython.h has been included, so pyport.h isn't seeing it. I'm not sure if that's something we intended to support.

That hasn't been an issue so far. The check here works fine / if I remove Py_BUILD_CORE from mypyc I get the error as expected.

#ifndefPy_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif

This fixes extensions that define Py_BUILD_CORE after including
Python.h, but before including pycore_pystate.h, such as mypyc.
@ZeroIntensity

Copy link
Copy Markdown
MemberAuthor

I just pushed a new commit, where _Py_thread_local is moved to pycore_pystate.h. Can you test that?

@cdce8p

Copy link
Copy Markdown
Contributor

I just pushed a new commit, where _Py_thread_local is moved to pycore_pystate.h. Can you test that?

This works now.

--

Hm, you're defining Py_BUILD_COREafterPython.h has been included, so pyport.h isn't seeing it. I'm not sure if that's something we intended to support.

I missed that mypyc is defining Py_BUILD_CORE before including any internal/** headers. Though that's much simpler than doing the same for Python.h which is included nearly everywhere.

Comment threadInclude/pyport.h Outdated
Comment threadInclude/internal/pycore_pystate.h Outdated
Comment threadInclude/internal/pycore_pystate.h
@kumaraditya303

Copy link
Copy Markdown
Contributor

@cdce8p Would you please check that mypyc still works after the new changes?

@cdce8p

Copy link
Copy Markdown
Contributor

@cdce8p Would you please check that mypyc still works after the new changes?

Just ran the whole test suite again. Looks good.

@ZeroIntensity
ZeroIntensity merged commit 2cefa70 into python:mainOct 28, 2025
48 checks passed
@ZeroIntensity
ZeroIntensity deleted the remove-have-thread-local branch October 28, 2025 13:07
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
…onGH-140690)
Python has required thread local support since 3.12 (see pythonGH-103324). By assuming that thread locals are always supported, we can improve the performance of third-party extensions by allowing them to access the attached thread and interpreter states directly.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@ZeroIntensity@cdce8p@kumaraditya303