Skip to content

Commit

Permalink
Replace comments:
Browse files Browse the repository at this point in the history
Document PRECONDITION.

Adopt comment suggested by @tkoeppe: #4877 (comment)
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Oct 9, 2023
1 parent 88cec11 commit 1ce2715
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ PYBIND11_NAMESPACE_BEGIN(detail)
template <typename T>
class LazyInitializeAtLeastOnceDestroyNever {
public:
// PRECONDITION: The GIL must be held when `Get()` is called.
// It is possible that multiple threads execute `Get()` with `initialized_`
// still being false, and thus proceed to execute `initialize()`. This can
// happen if `initialize()` releases and reacquires the GIL internally.
// We accept this, and expect the operation to be both idempotent and cheap.
template <typename Initialize>
T &Get(Initialize &&initialize) {
if (!initialized_) {
assert(PyGILState_Check());
// Multiple threads may run this concurrently, but that is fine.
auto value = initialize(); // May release and re-acquire the GIL.
if (!initialized_) { // This runs with the GIL held,
new // therefore this is reached only once.
(reinterpret_cast<T *>(value_storage_)) T(std::move(value));
auto value = initialize();
if (!initialized_) {
new (reinterpret_cast<T *>(value_storage_)) T(std::move(value));
initialized_ = true;
}
}
Expand Down

0 comments on commit 1ce2715

Please sign in to comment.