Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FFI definitions from pythread.h #4872

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LegionMammal978
Copy link

This PR adds all definitions from pythread.h and cpython/pythread.h that haven't been deprecated or removed as of CPython 3.13. Of the functions, some are fully documented, some are undocumented but still contained in the limited API list, and some are neither documented nor in the limited API. Some of the limited-API functions, such as PyThread_get_thread_ident(), are widely used by 3rd-party code despite being undocumented, so it would be useful to include them in the bindings. The full breakdown:

  • Documented, limited API: functions PyThread_tss_alloc, PyThread_tss_create, PyThread_tss_delete, PyThread_tss_free, PyThread_tss_get, PyThread_tss_is_created, PyThread_tss_set; struct Py_tss_t.
  • Undocumented, limited API: functions PyThread_GetInfo, PyThread_acquire_lock, PyThread_acquire_lock_timed, PyThread_allocate_lock, PyThread_exit_thread, PyThread_free_lock, PyThread_get_stacksize, PyThread_get_thread_ident, PyThread_get_thread_native_id, PyThread_init_thread, PyThread_release_lock, PyThread_set_stacksize, PyThread_start_new_thread; enum PyLockStatus; typedefs PY_TIMEOUT_T, PyThread_type_lock; constants NOWAIT_LOCK, PY_LOCK_ACQUIRED, PY_LOCK_FAILURE, PY_LOCK_INTR, WAIT_LOCK.
  • Documented, non-limited API: constant Py_tss_NEEDS_INIT.
  • Undocumented, non-limited API: static PY_TIMEOUT_MAX; constant PYTHREAD_INVALID_THREAD_ID.

PyThread_get_thread_native_id() is a particularly fiddly definition due to all the OS-based #ifdefs guarding it: I've approximated them with cfg(target_os) values to the best of my ability, but it would be much easier if we could directly check that the PY_HAVE_THREAD_NATIVE_ID macro is defined. I've also gone through the PyPy and GraalPy versions of the headers to see which functions they support.

@LegionMammal978 LegionMammal978 force-pushed the add-pythread branch 2 times, most recently from 93defdc to 400c8c5 Compare January 24, 2025 20:27
@LegionMammal978
Copy link
Author

Hmm, I'm not entirely sure how to replicate the NATIVE_TSS_KEY_T without a billion cfg() guards. In CPython, it's the pthread_key_t provided by cpython/pthread_stubs.h:

#ifdef HAVE_PTHREAD_H
#   include <pthread.h>
#   define NATIVE_TSS_KEY_T     pthread_key_t
#elif defined(NT_THREADS)
#   define NATIVE_TSS_KEY_T     unsigned long
#elif defined(HAVE_PTHREAD_STUBS)
#   include "cpython/pthread_stubs.h"
#   define NATIVE_TSS_KEY_T     pthread_key_t
#else
#   error "Require native threads. See https://bugs.python.org/issue31370"
#endif

It might make sense just to add a carve-out for wasi here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant