-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,20 +148,32 @@ struct override_hash { | |
|
||
using instance_map = std::unordered_multimap<const void *, instance *>; | ||
|
||
#ifdef Py_GIL_DISABLED | ||
// Wrapper around PyMutex to provide BasicLockable semantics | ||
class pymutex { | ||
PyMutex mutex; | ||
|
||
public: | ||
pymutex() : mutex({}) {} | ||
void lock() { PyMutex_Lock(&mutex); } | ||
void unlock() { PyMutex_Unlock(&mutex); } | ||
}; | ||
|
||
// Instance map shards are used to reduce mutex contention in free-threaded Python. | ||
struct instance_map_shard { | ||
std::mutex mutex; | ||
instance_map registered_instances; | ||
pymutex 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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oversight? — Was this meant to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thanks. |
||
}; | ||
#endif | ||
|
||
/// Internal data structure used to track registered instances and types. | ||
/// Whenever binary incompatible changes are made to this structure, | ||
/// `PYBIND11_INTERNALS_VERSION` must be incremented. | ||
struct internals { | ||
#ifdef Py_GIL_DISABLED | ||
std::mutex mutex; | ||
pymutex mutex; | ||
#endif | ||
// std::type_index -> pybind11's type information | ||
type_map<type_info *> registered_types_cpp; | ||
|
@@ -614,7 +626,7 @@ inline local_internals &get_local_internals() { | |
} | ||
|
||
#ifdef Py_GIL_DISABLED | ||
# define PYBIND11_LOCK_INTERNALS(internals) std::unique_lock<std::mutex> lock((internals).mutex) | ||
# define PYBIND11_LOCK_INTERNALS(internals) std::unique_lock<pymutex> lock((internals).mutex) | ||
#else | ||
# define PYBIND11_LOCK_INTERNALS(internals) | ||
#endif | ||
|
@@ -651,7 +663,7 @@ inline auto with_instance_map(const void *ptr, | |
auto idx = static_cast<size_t>(hash & internals.instance_shards_mask); | ||
|
||
auto &shard = internals.instance_shards[idx]; | ||
std::unique_lock<std::mutex> lock(shard.mutex); | ||
std::unique_lock<pymutex> lock(shard.mutex); | ||
return cb(shard.registered_instances); | ||
#else | ||
(void) ptr; | ||
|
@@ -667,7 +679,7 @@ inline size_t num_registered_instances() { | |
size_t count = 0; | ||
for (size_t i = 0; i <= internals.instance_shards_mask; ++i) { | ||
auto &shard = internals.instance_shards[i]; | ||
std::unique_lock<std::mutex> lock(shard.mutex); | ||
std::unique_lock<pymutex> lock(shard.mutex); | ||
count += shard.registered_instances.size(); | ||
} | ||
return count; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
near the top? (I double-checked that
std::mutex
is not referenced anymore in this file.)There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, ok, thanks.