Repository navigation
Expose _PyCriticalSection_BeginMutex as a public API #133296
Copy link
Copy link
Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
It would be helpful to expose _PyCriticalSection_BeginMutex as a public API.
There are times when one does not have really have a PyObject, but still might want to use a critical section. For example, we wanted to use a critical section to protect some initialization code in a C extension module, where we wanted to write something like this:
static PyMutex mu;
static PyObject* mymodule = nullptr;
// It is important that we use a critical section here rather than a regular
// PyMutex, say 'M', because we need to avoid the following deadlock:
// * thread T1 holds M and calls ImportModule, which attempts to acquire a
// critical section C.
// * thread T2 holds critical section C and attempts to call this code and
// acquire M.
// In general, if you're going to hold a lock while calling into Python code, it had better be a critical section.
PyCriticalSection cs;
_PyCriticalSection_BeginMutex(&cs, &mu);
if (!mymodule) {
PyObject *m = PyImport_ImportModule("mymodule");
// Importing may have released the critical section, so it is possible
// another thread might have populated mymodule. Make sure we clean up after
// ourselves in that case.
std::swap(mymodule, m);
Py_XDECREF(m);
}
PyCriticalSection_End(&cs);Currently one would have to write something like the following instead:
static PyObject object_for_mu; // Not really an object, we just want its mutex.
static PyObject* mymodule = nullptr;
PyCriticalSection cs;
PyCriticalSection_Begin(&cs, &object_for_mu);
if (!mymodule) {
PyObject *m = PyImport_ImportModule("mymodule");
std::swap(mymodule, m);
Py_XDECREF(m);
}
PyCriticalSection_End(&cs);Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement
Activity
sharktide commentedon May 26, 2025
I'm thinking of tackling this one
vstinner commentedon Jun 2, 2025
Please open an issue in https://github.com/capi-workgroup/decisions/issues/ project in this case.
pythongh-133296: Publicly expose critical section API that accepts Py…
gh-133296: Publicly expose critical section API that accepts PyMutex (g…
kumaraditya303 commentedon Jul 22, 2025
#135899 added this API.
pythongh-133296: Publicly expose critical section API that accepts Py…
[3.14] gh-133296: Publicly expose critical section API that accepts P…
gh-133296: Fix versionadded for C API functions that were backported (#…
pythongh-133296: Publicly expose critical section API that accepts Py…
pythongh-133296: Fix versionadded for C API functions that were backp…
pythongh-133296: Publicly expose critical section API that accepts Py…
pythongh-133296: Fix versionadded for C API functions that were backp…
[3.14] pythongh-133296: Publicly expose critical section API that acc…