Skip to content

gh-137514: Add a free-threading wrapper for mutexes #137515

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
Aug 7, 2025

Conversation

ZeroIntensity
Copy link
Member

@ZeroIntensity ZeroIntensity commented Aug 7, 2025

Add FT_MUTEX_LOCK/FT_MUTEX_UNLOCK, which call PyMutex_Lock and PyMutex_Unlock on the free-threaded build, and no-op otherwise.

@@ -15993,13 +15985,13 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,
PyObject *interned = get_interned_dict(interp);
assert(interned != NULL);

LOCK_INTERNED(interp);
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it is worth to keep the old interface here. And implement LOCK_INTERNED via FT_MUTEX_LOCK.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor

Choose a reason for hiding this comment

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

It is shorter, easier to read and less error-prone on my sight.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, instead of adding another locking macro, how about an INTERN_MUTEX macro that acts as a shorthand for &_Py_INTERP_CACHED_OBJECT(interp, interned_mutex).

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like a good idea!

@@ -509,9 +502,9 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)

// needs to be decref'd outside of the lock
PyObject *old_profileobj;
LOCK_SETUP();
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. I think it is better to keep the old one.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this one is fine. We already used sys_trace_profile_mutex inline in pystate.c.

@ZeroIntensity ZeroIntensity changed the title gh-137514: Add an FT wrapper for mutexes gh-137514: Add a free-threading wrapper for mutexes Aug 7, 2025
@ZeroIntensity ZeroIntensity merged commit 082f370 into python:main Aug 7, 2025
40 checks passed
@ZeroIntensity ZeroIntensity deleted the common-ft-lock-interface branch August 7, 2025 15:24
@ZeroIntensity
Copy link
Member Author

Do we want this in 3.14?

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.

4 participants