Skip to content

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

Merged
vstinner merged 7 commits into
python:mainfrom
vstinner:pymutex
May 30, 2025
Merged

vstinner merged 7 commits into
python:mainfrom
vstinner:pymutex

Conversation

@vstinner

@vstinner vstinner commented May 26, 2025 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 May 27, 2025
Comment thread Python/thread.c Resolved
Comment thread Python/thread.c Resolved
Comment thread Python/thread.c Resolved
vstinner added 2 commits May 28, 2025 12:28
Don't call signal handlers if interrupted, only return PY_LOCK_INTR.

@colesbury colesbury left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread Python/thread.c Resolved
vstinner and others added 2 commits May 30, 2025 11:29
Co-authored-by: Sam Gross <colesbury@gmail.com>
@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
@vstinner
vstinner deleted the pymutex branch May 30, 2025 10:15
@vstinner

Copy link
Copy Markdown
Member Author

Merged, thanks for the review @colesbury.

Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
…Mutex (python#134747)

Co-authored-by: Sam Gross <colesbury@gmail.com>
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…Mutex (python#134747)

Co-authored-by: Sam Gross <colesbury@gmail.com>
@vstinner

vstinner commented Nov 6, 2025

Copy link
Copy Markdown
Member Author

Oh, I forgot to update sys.thread_info: I created #141140 for that.

gostak-dd pushed a commit to gostak-dd/cpython that referenced this pull request Jun 2, 2026
…Mutex (python#134747)

Co-authored-by: Sam Gross <colesbury@gmail.com>
vstinner pushed a commit that referenced this pull request Jul 24, 2026
…GH-154338) (#154655)

gh-134745: Remove dead code in `thread_nt.h` left by gh-134747 (GH-154338)
(cherry picked from commit 9560bd8)

Co-authored-by: Ivy Xu <fakeshadow1337@gmail.com>
mbeijen pushed a commit to mbeijen/cpython that referenced this pull request Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants