Skip to content
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

fix: use PyMutex instead of std::mutex in free-threaded build #5219

Merged
merged 3 commits into from
Jul 2, 2024

Conversation

colesbury
Copy link
Contributor

Description

PyMutex is now part of the public C API as of 3.13.0b3 and generally has slightly less overhead than std::mutex.

See #5112

Suggested changelog entry:

Use ``PyMutex`` instead of ``std::mutex`` in the free-threaded build for internal locking.

colesbury and others added 2 commits July 1, 2024 21:58
PyMutex is now part of the public C API as of 3.13.0b3 and generally has
slightly less overhead than std::mutex.
// alignas(64) would be better, but causes compile errors in macOS before 10.14 (see #5200)
char padding[64 - (sizeof(std::mutex) + sizeof(instance_map)) % 64];
char padding[64 - (sizeof(instance_map) + sizeof(std::mutex)) % 64];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oversight? — Was this meant to be sizeof(pymutex) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks.

@@ -148,20 +148,35 @@ struct override_hash {

using instance_map = std::unordered_multimap<const void *, instance *>;

#ifdef Py_GIL_DISABLED
// Wrapper around PyMutex to provide BasicLockable semantics
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry I missed this when I looked before:

Could you please remove the

#include <mutex>

near the top? (I double-checked that std::mutex is not referenced anymore in this file.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We still need it for std::unique_lock

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, ok, thanks.

@rwgk rwgk merged commit bb05e08 into pybind:master Jul 2, 2024
86 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Jul 2, 2024
wenqing pushed a commit to wenqing/pybind11 that referenced this pull request Jul 6, 2024
* Use PyMutex instead of std::mutex in free-threaded build.

PyMutex is now part of the public C API as of 3.13.0b3 and generally has
slightly less overhead than std::mutex.

* style: pre-commit fixes

* Fix instance_map_shard padding

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@henryiii henryiii changed the title Use PyMutex instead of std::mutex in free-threaded build. fix: use PyMutex instead of std::mutex in free-threaded build Aug 13, 2024
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Aug 13, 2024
henryiii pushed a commit that referenced this pull request Aug 13, 2024
* Use PyMutex instead of std::mutex in free-threaded build.

PyMutex is now part of the public C API as of 3.13.0b3 and generally has
slightly less overhead than std::mutex.

* style: pre-commit fixes

* Fix instance_map_shard padding

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@colesbury colesbury deleted the pymutex branch September 10, 2024 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants