Skip to content

Expose critical section APIs that accept a PyMutex rather than an object #67

@ngoldbaum

Description

@ngoldbaum

c.f. python/cpython#133296

Right now extension authors can use the workaround of creating a dummy object to use exclusively with critical sections, but IMO it should be possible to use the critical section API with a PyMutex directly. This makes it easier for objects that don't subtype PyObject to use critical sections or to use critical sections in situations where a PyMutex lock might lead to deadlocks.

Proposed API

I think the internal APIs make sense to make public.

Specifically:

  • Py_BEGIN_CRITICAL_SECTION_MUTEX(mutex),
  • Py_BEGIN_CRITICAL_SECTION2_MUTEX(mutex1, mutex2)

These are identical to the corresponding Py_BEGIN_CRITICAL_SECTION and Py_BEGIN_CRITICAL_SECTION2 macros (e.g. they include braces), but they accept a PyMutex instead of an object. We also need to make these API functions public:

  • void PyCriticalSection_BeginMutex(PyCriticalSection *c, PyMutex *mutex)
  • void PyCriticalSection2_BeginMutex(PyCriticalSection2 *c, PyMutex *mutex1, PyMutex *mutex2)

These can be real C API functions that call the static inline functions _PyCriticalSection_BeginMutex, _PyCriticalSection2_BeginMutex.

These can be used in cases where using the macros is awkward, but should be avoided for the same reason the regular object versions should be avoided, since the macros force you to deal with managing the critical sections.

The new macros should be paired with the existing END macros (Py_END_CRITICAL_SECTION, Py_END_CRITICAL_SECTION2).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions