Merge branch 'pybind:master' into master

This commit is contained in:
Steve R. Sun 2024-07-03 08:58:26 +08:00 committed by GitHub
commit a46371c2bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 12 deletions

View File

@ -25,14 +25,14 @@ repos:
# Clang format the codebase automatically # Clang format the codebase automatically
- repo: https://github.com/pre-commit/mirrors-clang-format - repo: https://github.com/pre-commit/mirrors-clang-format
rev: "v18.1.5" rev: "v18.1.8"
hooks: hooks:
- id: clang-format - id: clang-format
types_or: [c++, c, cuda] types_or: [c++, c, cuda]
# Ruff, the Python auto-correcting linter/formatter written in Rust # Ruff, the Python auto-correcting linter/formatter written in Rust
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.7 rev: v0.5.0
hooks: hooks:
- id: ruff - id: ruff
args: ["--fix", "--show-fixes"] args: ["--fix", "--show-fixes"]
@ -40,7 +40,7 @@ repos:
# Check static types with mypy # Check static types with mypy
- repo: https://github.com/pre-commit/mirrors-mypy - repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.10.0" rev: "v1.10.1"
hooks: hooks:
- id: mypy - id: mypy
args: [] args: []
@ -79,7 +79,7 @@ repos:
# Also code format the docs # Also code format the docs
- repo: https://github.com/adamchainz/blacken-docs - repo: https://github.com/adamchainz/blacken-docs
rev: "1.16.0" rev: "1.18.0"
hooks: hooks:
- id: blacken-docs - id: blacken-docs
additional_dependencies: additional_dependencies:
@ -142,14 +142,14 @@ repos:
# PyLint has native support - not always usable, but works for us # PyLint has native support - not always usable, but works for us
- repo: https://github.com/PyCQA/pylint - repo: https://github.com/PyCQA/pylint
rev: "v3.2.2" rev: "v3.2.4"
hooks: hooks:
- id: pylint - id: pylint
files: ^pybind11 files: ^pybind11
# Check schemas on some of our YAML files # Check schemas on some of our YAML files
- repo: https://github.com/python-jsonschema/check-jsonschema - repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.4 rev: 0.28.6
hooks: hooks:
- id: check-readthedocs - id: check-readthedocs
- id: check-github-workflows - id: check-github-workflows

View File

@ -148,20 +148,35 @@ struct override_hash {
using instance_map = std::unordered_multimap<const void *, instance *>; 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. // Instance map shards are used to reduce mutex contention in free-threaded Python.
struct instance_map_shard { struct instance_map_shard {
std::mutex mutex;
instance_map registered_instances; instance_map registered_instances;
pymutex mutex;
// alignas(64) would be better, but causes compile errors in macOS before 10.14 (see #5200) // 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(pymutex)) % 64];
}; };
static_assert(sizeof(instance_map_shard) % 64 == 0,
"instance_map_shard size is not a multiple of 64 bytes");
#endif
/// Internal data structure used to track registered instances and types. /// Internal data structure used to track registered instances and types.
/// Whenever binary incompatible changes are made to this structure, /// Whenever binary incompatible changes are made to this structure,
/// `PYBIND11_INTERNALS_VERSION` must be incremented. /// `PYBIND11_INTERNALS_VERSION` must be incremented.
struct internals { struct internals {
#ifdef Py_GIL_DISABLED #ifdef Py_GIL_DISABLED
std::mutex mutex; pymutex mutex;
#endif #endif
// std::type_index -> pybind11's type information // std::type_index -> pybind11's type information
type_map<type_info *> registered_types_cpp; type_map<type_info *> registered_types_cpp;
@ -614,7 +629,7 @@ inline local_internals &get_local_internals() {
} }
#ifdef Py_GIL_DISABLED #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 #else
# define PYBIND11_LOCK_INTERNALS(internals) # define PYBIND11_LOCK_INTERNALS(internals)
#endif #endif
@ -651,7 +666,7 @@ inline auto with_instance_map(const void *ptr,
auto idx = static_cast<size_t>(hash & internals.instance_shards_mask); auto idx = static_cast<size_t>(hash & internals.instance_shards_mask);
auto &shard = internals.instance_shards[idx]; 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); return cb(shard.registered_instances);
#else #else
(void) ptr; (void) ptr;
@ -667,7 +682,7 @@ inline size_t num_registered_instances() {
size_t count = 0; size_t count = 0;
for (size_t i = 0; i <= internals.instance_shards_mask; ++i) { for (size_t i = 0; i <= internals.instance_shards_mask; ++i) {
auto &shard = internals.instance_shards[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(); count += shard.registered_instances.size();
} }
return count; return count;