Skip to content

gh-134745: Change PyThread_allocate_lock() implementation to PyMutex #134747

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 7 commits into from
May 30, 2025

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented May 26, 2025

PyMutex is a fast and portable lock currently only used internally by Python. This change modifies PyThread_allocate_lock to reuse PyMutex.

@vstinner
Copy link
Member Author

This draft PR currently only modify Python/thread_pthread.h. I left Python/thread_nt.h unchanged.

_PyThread_at_fork_reinit() is still implemented by allocating a new lock and leaking the old lock.

@vstinner
Copy link
Member Author

@colesbury: What do you think of this idea? Does it make sense? Do you see any possible issue?

If the approach makes sense, I will update my PR to modify also the Windows implementation.

@colesbury
Copy link
Contributor

I think the idea is fine, but the PR title and description are confusing. threading.Lock / _thread.lock / _thread._thread.allocate_lock() already use PyMutex (all three are the same thing):

typedef struct {
PyObject_HEAD
PyMutex lock;
} lockobject;

This PR changes the implementation of the C API PyThread_allocate_lock(). We are generally using PyMutex directly instead of PyThread_allocate_lock() in new code, and we've also changed a lot of existing uses too. Using PyMutex directly is simpler because it avoids an allocation that may fail.

I think it's okay to change the implementation of PyThread_allocate_lock(). It looks like it's going to simplify the code, but there's some risk of unknown behavioral changes.

@vstinner vstinner changed the title gh-134745: Change threading.Lock implementation to PyMutex gh-134745: Change PyThread_allocate_lock() implementation to PyMutex May 27, 2025
vstinner added 2 commits May 28, 2025 12:28
Don't call signal handlers if interrupted, only return PY_LOCK_INTR.
Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

This LGTM, but I think you should include a NEWS entry. One behavioral change is that previously intr_flag was ignored on Windows, but now it actually makes the acquisitions interruptible.

@vstinner vstinner enabled auto-merge (squash) May 30, 2025 09:42
@vstinner vstinner disabled auto-merge May 30, 2025 09:44
@vstinner vstinner enabled auto-merge (squash) May 30, 2025 09:45
@vstinner vstinner merged commit ebf6d13 into python:main May 30, 2025
39 checks passed
@vstinner vstinner deleted the pymutex branch May 30, 2025 10:15
@vstinner
Copy link
Member Author

Merged, thanks for the review @colesbury.

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

Successfully merging this pull request may close these issues.

2 participants