Skip to content

Commit b044b4f

Browse files
committed
Merge branch 'master' into sh_merge_master
2 parents 64c886d + 50acb81 commit b044b4f

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

.pre-commit-config.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ repos:
2525

2626
# Clang format the codebase automatically
2727
- repo: https://github.com/pre-commit/mirrors-clang-format
28-
rev: "v18.1.5"
28+
rev: "v18.1.8"
2929
hooks:
3030
- id: clang-format
3131
types_or: [c++, c, cuda]
3232

3333
# Ruff, the Python auto-correcting linter/formatter written in Rust
3434
- repo: https://github.com/astral-sh/ruff-pre-commit
35-
rev: v0.4.7
35+
rev: v0.5.0
3636
hooks:
3737
- id: ruff
3838
args: ["--fix", "--show-fixes"]
3939
- id: ruff-format
4040

4141
# Check static types with mypy
4242
- repo: https://github.com/pre-commit/mirrors-mypy
43-
rev: "v1.10.0"
43+
rev: "v1.10.1"
4444
hooks:
4545
- id: mypy
4646
args: []
@@ -80,7 +80,7 @@ repos:
8080

8181
# Also code format the docs
8282
- repo: https://github.com/adamchainz/blacken-docs
83-
rev: "1.16.0"
83+
rev: "1.18.0"
8484
hooks:
8585
- id: blacken-docs
8686
additional_dependencies:
@@ -144,14 +144,14 @@ repos:
144144

145145
# PyLint has native support - not always usable, but works for us
146146
- repo: https://github.com/PyCQA/pylint
147-
rev: "v3.2.2"
147+
rev: "v3.2.4"
148148
hooks:
149149
- id: pylint
150150
files: ^pybind11
151151

152152
# Check schemas on some of our YAML files
153153
- repo: https://github.com/python-jsonschema/check-jsonschema
154-
rev: 0.28.4
154+
rev: 0.28.6
155155
hooks:
156156
- id: check-readthedocs
157157
- id: check-github-workflows

docs/requirements.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ breathe==4.35.0 \
1616
--hash=sha256:5165541c3c67b6c7adde8b3ecfe895c6f7844783c4076b6d8d287e4f33d62386 \
1717
--hash=sha256:52c581f42ca4310737f9e435e3851c3d1f15446205a85fbc272f1f97ed74f5be
1818
# via -r requirements.in
19-
certifi==2024.2.2 \
20-
--hash=sha256:0569859f95fc761b18b45ef421b1290a0f65f147e92a1e5eb3e635f9a5e4e66f \
21-
--hash=sha256:dc383c07b76109f368f6106eee2b593b04a011ea4d55f652c6ca24a754d1cdd1
19+
certifi==2024.7.4 \
20+
--hash=sha256:5a1e7645bc0ec61a09e26c36f6106dd4cf40c6db3a1fb6352b0244e7fb057c7b \
21+
--hash=sha256:c198e21b1289c2ab85ee4e67bb4b4ef3ead0892059901a8d5b622f24a1101e90
2222
# via requests
2323
charset-normalizer==3.3.2 \
2424
--hash=sha256:06435b539f889b1f6f4ac1758871aae42dc3a8c0e24ac9e60c2384973ad73027 \

include/pybind11/detail/internals.h

+21-6
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,35 @@ struct override_hash {
149149

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

152+
#ifdef Py_GIL_DISABLED
153+
// Wrapper around PyMutex to provide BasicLockable semantics
154+
class pymutex {
155+
PyMutex mutex;
156+
157+
public:
158+
pymutex() : mutex({}) {}
159+
void lock() { PyMutex_Lock(&mutex); }
160+
void unlock() { PyMutex_Unlock(&mutex); }
161+
};
162+
152163
// Instance map shards are used to reduce mutex contention in free-threaded Python.
153164
struct instance_map_shard {
154-
std::mutex mutex;
155165
instance_map registered_instances;
166+
pymutex mutex;
156167
// alignas(64) would be better, but causes compile errors in macOS before 10.14 (see #5200)
157-
char padding[64 - (sizeof(std::mutex) + sizeof(instance_map)) % 64];
168+
char padding[64 - (sizeof(instance_map) + sizeof(pymutex)) % 64];
158169
};
159170

171+
static_assert(sizeof(instance_map_shard) % 64 == 0,
172+
"instance_map_shard size is not a multiple of 64 bytes");
173+
#endif
174+
160175
/// Internal data structure used to track registered instances and types.
161176
/// Whenever binary incompatible changes are made to this structure,
162177
/// `PYBIND11_INTERNALS_VERSION` must be incremented.
163178
struct internals {
164179
#ifdef Py_GIL_DISABLED
165-
std::mutex mutex;
180+
pymutex mutex;
166181
#endif
167182
// std::type_index -> pybind11's type information
168183
type_map<type_info *> registered_types_cpp;
@@ -625,7 +640,7 @@ inline local_internals &get_local_internals() {
625640
}
626641

627642
#ifdef Py_GIL_DISABLED
628-
# define PYBIND11_LOCK_INTERNALS(internals) std::unique_lock<std::mutex> lock((internals).mutex)
643+
# define PYBIND11_LOCK_INTERNALS(internals) std::unique_lock<pymutex> lock((internals).mutex)
629644
#else
630645
# define PYBIND11_LOCK_INTERNALS(internals)
631646
#endif
@@ -662,7 +677,7 @@ inline auto with_instance_map(const void *ptr,
662677
auto idx = static_cast<size_t>(hash & internals.instance_shards_mask);
663678

664679
auto &shard = internals.instance_shards[idx];
665-
std::unique_lock<std::mutex> lock(shard.mutex);
680+
std::unique_lock<pymutex> lock(shard.mutex);
666681
return cb(shard.registered_instances);
667682
#else
668683
(void) ptr;
@@ -678,7 +693,7 @@ inline size_t num_registered_instances() {
678693
size_t count = 0;
679694
for (size_t i = 0; i <= internals.instance_shards_mask; ++i) {
680695
auto &shard = internals.instance_shards[i];
681-
std::unique_lock<std::mutex> lock(shard.mutex);
696+
std::unique_lock<pymutex> lock(shard.mutex);
682697
count += shard.registered_instances.size();
683698
}
684699
return count;

0 commit comments

Comments
 (0)