Add python freethreading support - #307
Merged
Merged
Conversation
Only request the free-threading build flags when the target interpreter is an actual free-threaded (no-GIL) build (Py_GIL_DISABLED), and only when the tools are new enough: - swig -nogil (swig >= 4.4.0) - Cython freethreading_compatible=True directive (Cython >= 3.1.0) Warn and fall back gracefully (GIL re-enabled at import) when the tools are too old. Remove the legacy swig -py3 flag entirely, as it has been deprecated and a no-op since swig 4.1.0. Applied consistently across both build systems (autotools and meson). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
test-freethreading.py imports _xraylib (swig) and xraylib_np (cython) in fresh subprocesses and inspects sys._is_gil_enabled(): - on a free-threaded (no-GIL) build the GIL starts disabled and is only re-enabled when a module that has NOT declared free-threading support is imported, so the test asserts the GIL stays disabled -> confirming the modules were built with -nogil / freethreading_compatible=True - on a regular build it asserts the opposite (the GIL is active) A subprocess per module is required because re-enabling the GIL is a permanent, process-wide side effect that would otherwise mask later imports. Registered in both build systems. Also deduplicate the free-threaded detection in configure.ac into a single guarded XRL_CHECK_PYTHON_FREETHREADED macro. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The Python bindings now declare themselves free-threading (no-GIL)
compatible via Py_MOD_GIL_NOT_USED, but the SWIG-wrapped crystal
functions operate on the mutable process-global Crystal_arr, whose
accesses were previously serialized only by the GIL. Under a
free-threaded interpreter that serialization is gone, leaving a genuine
data race (e.g. concurrent Crystal_AddCrystal / Crystal_ReadFile /
Crystal_GetCrystal calls).
Serialize the shared-global paths with a minimal internal mutex:
- New non-installed header src/xraylib-aux-private.h provides an xrl_lock
abstraction: SRWLOCK on _WIN32 (native MinGW/MSYS2, kernel32-only, no
libwinpthread-1.dll dependency) and pthread_mutex_t elsewhere (Linux,
macOS, Cygwin). Both are statically initializable.
- xrl_lock_acquire/xrl_lock_release wrappers in xraylib-aux.c (hidden
visibility, not exported).
- The four crystal functions touching Crystal_arr are split into static
*_impl bodies wrapped by thin public functions that take the lock only
when c_array == NULL (the shared global); caller-owned arrays remain
lock-free, preserving the documented contract and the public API.
Build wiring adds no new redistributable dependency:
- autotools: vendored m4/ax_pthread.m4; configure.ac host-gates
AC_CHECK_DECL(AcquireSRWLockExclusive) on mingw and AX_PTHREAD
elsewhere; PTHREAD_CFLAGS/LIBS flow into libxrl and the prdata tool.
- meson: dependency('threads') off-Windows, cc.has_function check for
AcquireSRWLockExclusive on Windows.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add a C threaded test (tests/) and a Python threading test (python/tests/) that hammer the process-global crystal array from multiple threads, exercising the internal lock that makes the free-threading support truthful. Both are wired into the meson and autotools build systems. Add a support-gated ThreadSanitizer CI job that builds the whole library with -Db_sanitize=thread and runs the crystal tests under TSan. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.