Skip to content

Commit 1aed3ab

Browse files
committed
Update comments
1 parent a6754ba commit 1aed3ab

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

include/pybind11/detail/internals.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,19 +627,23 @@ class internals_pp_manager {
627627
/// Get the pointer-to-pointer for the main interpreter, allocating it if it does not already
628628
/// exist. May acquire the GIL. Will never return nullptr.
629629
std::unique_ptr<InternalsType> *get_pp_for_main_interpreter() {
630-
// This function **assumes** that the current thread is running in the main interpreter.
631630
if (!seen_main_interpreter_) {
631+
// The first call to this function **MUST** be from the main interpreter.
632+
// Here we **ASSUME** that the current thread is running in the main interpreter.
633+
// The caller is responsible for ensuring this.
632634
std::call_once(seen_main_interpreter_flag_, [&] {
633635
gil_scoped_acquire_simple gil;
634636
internals_singleton_pp_ = get_or_create_pp_in_state_dict();
635637
seen_main_interpreter_ = true;
636638
});
637639
}
640+
// This is shared between all threads and all interpreters.
638641
return internals_singleton_pp_;
639642
}
640643

641644
/// Drop all the references we're currently holding.
642645
void unref() {
646+
// See comment in get_pp() above.
643647
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
644648
if (get_num_interpreters_seen() > 1) {
645649
last_istate_tls() = nullptr;
@@ -651,6 +655,7 @@ class internals_pp_manager {
651655
}
652656

653657
void destroy() {
658+
// See comment in get_pp() above.
654659
#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
655660
if (get_num_interpreters_seen() > 1) {
656661
auto *tstate = get_thread_state_unchecked();
@@ -711,8 +716,8 @@ class internals_pp_manager {
711716

712717
char const *holder_id_ = nullptr;
713718
on_fetch_function *on_fetch_ = nullptr;
719+
// Pointer to the singleton internals for the main interpreter
714720
std::unique_ptr<InternalsType> *internals_singleton_pp_;
715-
716721
std::once_flag seen_main_interpreter_flag_;
717722
std::atomic_bool seen_main_interpreter_{false};
718723
};

include/pybind11/gil_safe_call_once.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class gil_safe_call_once_and_store {
130130
// CPython API calls in the `fn()` call below may release and reacquire the GIL.
131131
gil_scoped_release gil_rel; // Needed to establish lock ordering.
132132
detail::with_internals([&](detail::internals &internals) {
133-
const void *key = reinterpret_cast<const void *>(this);
133+
const void *const key = reinterpret_cast<const void *>(this);
134134
auto &storage_map = internals.call_once_storage_map;
135135
// There can be multiple threads going through here.
136136
detail::call_once_storage<T> *value = nullptr;
@@ -168,7 +168,7 @@ class gil_safe_call_once_and_store {
168168
T *result = last_storage_ptr_;
169169
if (!is_last_storage_valid()) {
170170
detail::with_internals([&](detail::internals &internals) {
171-
const void *key = reinterpret_cast<const void *>(this);
171+
const void *const key = reinterpret_cast<const void *>(this);
172172
auto &storage_map = internals.call_once_storage_map;
173173
auto *value = static_cast<detail::call_once_storage<T> *>(storage_map.at(key));
174174
result = last_storage_ptr_ = reinterpret_cast<T *>(value->storage);

0 commit comments

Comments
 (0)