Skip to content

gh-117511: Make PyMutex public in the non-limited API #117731

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

Merged
merged 14 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Include/cpython/lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,34 @@ typedef struct PyMutex {
uint8_t _bits; // (private)
} PyMutex;

// (private) slow path for locking the mutex
PyAPI_FUNC(void) _PyMutex_LockSlow(PyMutex *m);
// exported function for locking the mutex
PyAPI_FUNC(void) PyMutex_Lock(PyMutex *m);

// (private) slow path for unlocking the mutex
PyAPI_FUNC(void) _PyMutex_UnlockSlow(PyMutex *m);
// exported function for unlocking the mutex
PyAPI_FUNC(void) PyMutex_Unlock(PyMutex *m);

// Locks the mutex.
//
// If the mutex is currently locked, the calling thread will be parked until
// the mutex is unlocked. If the current thread holds the GIL, then the GIL
// will be released while the thread is parked.
static inline void
PyMutex_Lock(PyMutex *m)
_PyMutex_Lock(PyMutex *m)
{
uint8_t expected = _Py_UNLOCKED;
if (!_Py_atomic_compare_exchange_uint8(&m->_bits, &expected, _Py_LOCKED)) {
_PyMutex_LockSlow(m);
PyMutex_Lock(m);
}
}
#define PyMutex_Lock _PyMutex_Lock

// Unlocks the mutex.
static inline void
PyMutex_Unlock(PyMutex *m)
_PyMutex_Unlock(PyMutex *m)
{
uint8_t expected = _Py_LOCKED;
if (!_Py_atomic_compare_exchange_uint8(&m->_bits, &expected, _Py_UNLOCKED)) {
_PyMutex_UnlockSlow(m);
PyMutex_Unlock(m);
}
}
#define PyMutex_Unlock _PyMutex_Unlock
2 changes: 1 addition & 1 deletion Python/critical_section.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ _PyCriticalSection_BeginSlow(_PyCriticalSection *c, PyMutex *m)
c->prev = (uintptr_t)tstate->critical_section;
tstate->critical_section = (uintptr_t)c;

_PyMutex_LockSlow(m);
PyMutex_Lock(m);
c->mutex = m;
}

Expand Down
6 changes: 4 additions & 2 deletions Python/lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ _Py_yield(void)
#endif
}

#undef PyMutex_Lock
void
_PyMutex_LockSlow(PyMutex *m)
PyMutex_Lock(PyMutex *m)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might move it to the end to still use the static inline macro in the following lines.

{
_PyMutex_LockTimed(m, -1, _PY_LOCK_DETACH);
}
Expand Down Expand Up @@ -182,8 +183,9 @@ _PyMutex_TryUnlock(PyMutex *m)
}
}

#undef PyMutex_Unlock
void
_PyMutex_UnlockSlow(PyMutex *m)
PyMutex_Unlock(PyMutex *m)
{
if (_PyMutex_TryUnlock(m) < 0) {
Py_FatalError("unlocking mutex that is not locked");
Expand Down