mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-30 08:57:11 +00:00
0e2c3e5db4
* LazyInitializeAtLeastOnceDestroyNever v1 * Go back to using `union` as originally suggested by jbms@. The trick (also suggested by jbms@) is to add empty ctor + dtor. * Revert "Go back to using `union` as originally suggested by jbms@. The trick (also suggested by jbms@) is to add empty ctor + dtor." This reverts commite7b8c4f0fc
. * Remove `#include <stdalign.h>` * `include\pybind11/numpy.h(24,10): fatal error C1083: Cannot open include file: 'stdalign.h': No such file or directory` * @tkoeppe wrote: this is a C interop header (and we're not writing C) * Suppress gcc 4.8.5 (CentOS 7) warning. ``` include/pybind11/eigen/../numpy.h:63:53: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] return *reinterpret_cast<T *>(value_storage_); ^ ``` * Replace comments: Document PRECONDITION. Adopt comment suggested by @tkoeppe: https://github.com/pybind/pybind11/pull/4877#discussion_r1350356093 * Adopt suggestion by @tkoeppe: * https://github.com/pybind/pybind11/pull/4877#issuecomment-1752969127 * https://godbolt.org/z/Wa79nKz6e * Add `PYBIND11_CONSTINIT`, but it does not work for the current use cases: ``` g++ -o pybind11/tests/test_numpy_array.os -c -std=c++20 -fPIC -fvisibility=hidden -O0 -g -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wundef -Wnon-virtual-dtor -Wunused-result -Werror -isystem /usr/include/python3.11 -isystem /usr/include/eigen3 -DPYBIND11_STRICT_ASSERTS_CLASS_HOLDER_VS_TYPE_CASTER_MIX -DPYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD_IF_AVAILABLE -DPYBIND11_TEST_BOOST -Ipybind11/include -I/usr/local/google/home/rwgk/forked/pybind11/include -I/usr/local/google/home/rwgk/clone/pybind11/include /usr/local/google/home/rwgk/forked/pybind11/tests/test_numpy_array.cpp ``` ``` In file included from /usr/local/google/home/rwgk/forked/pybind11/tests/test_numpy_array.cpp:10: /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/numpy.h: In static member function ‘static pybind11::detail::npy_api& pybind11::detail::npy_api::get()’: /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/numpy.h:258:82: error: ‘constinit’ variable ‘api_init’ does not have a constant initializer 258 | PYBIND11_CONSTINIT static LazyInitializeAtLeastOnceDestroyNever<npy_api> api_init; | ^~~~~~~~ ``` ``` In file included from /usr/local/google/home/rwgk/forked/pybind11/tests/test_numpy_array.cpp:10: /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/numpy.h: In static member function ‘static pybind11::object& pybind11::dtype::_dtype_from_pep3118()’: /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/numpy.h:697:13: error: ‘constinit’ variable ‘imported_obj’ does not have a constant initializer 697 | imported_obj; | ^~~~~~~~~~~~ ``` * Revert "Add `PYBIND11_CONSTINIT`, but it does not work for the current use cases:" This reverts commitf07b28bda9
. * Reapply "Add `PYBIND11_CONSTINIT`, but it does not work for the current use cases:" This reverts commit36be645758
. * Add Default Member Initializer on `value_storage_` as suggested by @tkoeppe: https://github.com/pybind/pybind11/pull/4877#issuecomment-1753201342 This fixes the errors reported under commitf07b28bda9
. * Fix copy-paste-missed-a-change mishap in commit88cec1152a
. * Semi-paranoid placement new (based on https://github.com/pybind/pybind11/pull/4877#discussion_r1350573114). * Move PYBIND11_CONSTINIT to detail/common.h * Move code to the right places, rename new class and some variables. * Fix oversight: update tests/extra_python_package/test_files.py * Get the name right first. * Use `std::call_once`, `std::atomic`, following a pattern developed by @tkoeppe * Make the API more self-documenting (and possibly more easily reusable). * google-clang-tidy IWYU fixes * Rewrite comment as suggested by @tkoeppe * Update test_exceptions.cpp and exceptions.rst * Fix oversight in previous commit: add `PYBIND11_CONSTINIT` * Make `get_stored()` non-const for simplicity. As suggested by @tkoeppe: not seeing any reasonable use in which `get_stored` has to be const. * Add comment regarding `KeyboardInterrupt` behavior, based heavily on information provided by @jbms. * Add `assert(PyGILState_Check())` in `gil_scoped_release` ctor (simple & non-simple implementation) as suggested by @EthanSteinberg. * Fix oversight in previous commit (missing include cassert). * Remove use of std::atomic, leaving comments with rationale, why it is not needed. * Rewrite comment re `std:optional` based on deeper reflection (aka 2nd thoughts). * Additional comment with the conclusion of a discussion under PR #4877. * https://github.com/pybind/pybind11/pull/4877#issuecomment-1757363179 * Small comment changes suggested by @tkoeppe.
248 lines
8.3 KiB
C++
248 lines
8.3 KiB
C++
/*
|
|
pybind11/gil.h: RAII helpers for managing the GIL
|
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
BSD-style license that can be found in the LICENSE file.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "detail/common.h"
|
|
|
|
#include <cassert>
|
|
|
|
#if defined(WITH_THREAD) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
|
|
# include "detail/internals.h"
|
|
#endif
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
|
|
// forward declarations
|
|
PyThreadState *get_thread_state_unchecked();
|
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
#if defined(WITH_THREAD)
|
|
|
|
# if !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
|
|
|
|
/* The functions below essentially reproduce the PyGILState_* API using a RAII
|
|
* pattern, but there are a few important differences:
|
|
*
|
|
* 1. When acquiring the GIL from an non-main thread during the finalization
|
|
* phase, the GILState API blindly terminates the calling thread, which
|
|
* is often not what is wanted. This API does not do this.
|
|
*
|
|
* 2. The gil_scoped_release function can optionally cut the relationship
|
|
* of a PyThreadState and its associated thread, which allows moving it to
|
|
* another thread (this is a fairly rare/advanced use case).
|
|
*
|
|
* 3. The reference count of an acquired thread state can be controlled. This
|
|
* can be handy to prevent cases where callbacks issued from an external
|
|
* thread would otherwise constantly construct and destroy thread state data
|
|
* structures.
|
|
*
|
|
* See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an
|
|
* example which uses features 2 and 3 to migrate the Python thread of
|
|
* execution to another thread (to run the event loop on the original thread,
|
|
* in this case).
|
|
*/
|
|
|
|
class gil_scoped_acquire {
|
|
public:
|
|
PYBIND11_NOINLINE gil_scoped_acquire() {
|
|
auto &internals = detail::get_internals();
|
|
tstate = (PyThreadState *) PYBIND11_TLS_GET_VALUE(internals.tstate);
|
|
|
|
if (!tstate) {
|
|
/* Check if the GIL was acquired using the PyGILState_* API instead (e.g. if
|
|
calling from a Python thread). Since we use a different key, this ensures
|
|
we don't create a new thread state and deadlock in PyEval_AcquireThread
|
|
below. Note we don't save this state with internals.tstate, since we don't
|
|
create it we would fail to clear it (its reference count should be > 0). */
|
|
tstate = PyGILState_GetThisThreadState();
|
|
}
|
|
|
|
if (!tstate) {
|
|
tstate = PyThreadState_New(internals.istate);
|
|
# if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
|
|
if (!tstate) {
|
|
pybind11_fail("scoped_acquire: could not create thread state!");
|
|
}
|
|
# endif
|
|
tstate->gilstate_counter = 0;
|
|
PYBIND11_TLS_REPLACE_VALUE(internals.tstate, tstate);
|
|
} else {
|
|
release = detail::get_thread_state_unchecked() != tstate;
|
|
}
|
|
|
|
if (release) {
|
|
PyEval_AcquireThread(tstate);
|
|
}
|
|
|
|
inc_ref();
|
|
}
|
|
|
|
gil_scoped_acquire(const gil_scoped_acquire &) = delete;
|
|
gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
|
|
|
|
void inc_ref() { ++tstate->gilstate_counter; }
|
|
|
|
PYBIND11_NOINLINE void dec_ref() {
|
|
--tstate->gilstate_counter;
|
|
# if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
|
|
if (detail::get_thread_state_unchecked() != tstate) {
|
|
pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
|
|
}
|
|
if (tstate->gilstate_counter < 0) {
|
|
pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
|
|
}
|
|
# endif
|
|
if (tstate->gilstate_counter == 0) {
|
|
# if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
|
|
if (!release) {
|
|
pybind11_fail("scoped_acquire::dec_ref(): internal error!");
|
|
}
|
|
# endif
|
|
PyThreadState_Clear(tstate);
|
|
if (active) {
|
|
PyThreadState_DeleteCurrent();
|
|
}
|
|
PYBIND11_TLS_DELETE_VALUE(detail::get_internals().tstate);
|
|
release = false;
|
|
}
|
|
}
|
|
|
|
/// This method will disable the PyThreadState_DeleteCurrent call and the
|
|
/// GIL won't be acquired. This method should be used if the interpreter
|
|
/// could be shutting down when this is called, as thread deletion is not
|
|
/// allowed during shutdown. Check _Py_IsFinalizing() on Python 3.7+, and
|
|
/// protect subsequent code.
|
|
PYBIND11_NOINLINE void disarm() { active = false; }
|
|
|
|
PYBIND11_NOINLINE ~gil_scoped_acquire() {
|
|
dec_ref();
|
|
if (release) {
|
|
PyEval_SaveThread();
|
|
}
|
|
}
|
|
|
|
private:
|
|
PyThreadState *tstate = nullptr;
|
|
bool release = true;
|
|
bool active = true;
|
|
};
|
|
|
|
class gil_scoped_release {
|
|
public:
|
|
// PRECONDITION: The GIL must be held when this constructor is called.
|
|
explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
|
|
assert(PyGILState_Check());
|
|
// `get_internals()` must be called here unconditionally in order to initialize
|
|
// `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an
|
|
// initialization race could occur as multiple threads try `gil_scoped_acquire`.
|
|
auto &internals = detail::get_internals();
|
|
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
|
|
tstate = PyEval_SaveThread();
|
|
if (disassoc) {
|
|
// Python >= 3.7 can remove this, it's an int before 3.7
|
|
// NOLINTNEXTLINE(readability-qualified-auto)
|
|
auto key = internals.tstate;
|
|
PYBIND11_TLS_DELETE_VALUE(key);
|
|
}
|
|
}
|
|
|
|
gil_scoped_release(const gil_scoped_release &) = delete;
|
|
gil_scoped_release &operator=(const gil_scoped_release &) = delete;
|
|
|
|
/// This method will disable the PyThreadState_DeleteCurrent call and the
|
|
/// GIL won't be acquired. This method should be used if the interpreter
|
|
/// could be shutting down when this is called, as thread deletion is not
|
|
/// allowed during shutdown. Check _Py_IsFinalizing() on Python 3.7+, and
|
|
/// protect subsequent code.
|
|
PYBIND11_NOINLINE void disarm() { active = false; }
|
|
|
|
~gil_scoped_release() {
|
|
if (!tstate) {
|
|
return;
|
|
}
|
|
// `PyEval_RestoreThread()` should not be called if runtime is finalizing
|
|
if (active) {
|
|
PyEval_RestoreThread(tstate);
|
|
}
|
|
if (disassoc) {
|
|
// Python >= 3.7 can remove this, it's an int before 3.7
|
|
// NOLINTNEXTLINE(readability-qualified-auto)
|
|
auto key = detail::get_internals().tstate;
|
|
PYBIND11_TLS_REPLACE_VALUE(key, tstate);
|
|
}
|
|
}
|
|
|
|
private:
|
|
PyThreadState *tstate;
|
|
bool disassoc;
|
|
bool active = true;
|
|
};
|
|
|
|
# else // PYBIND11_SIMPLE_GIL_MANAGEMENT
|
|
|
|
class gil_scoped_acquire {
|
|
PyGILState_STATE state;
|
|
|
|
public:
|
|
gil_scoped_acquire() : state{PyGILState_Ensure()} {}
|
|
gil_scoped_acquire(const gil_scoped_acquire &) = delete;
|
|
gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
|
|
~gil_scoped_acquire() { PyGILState_Release(state); }
|
|
void disarm() {}
|
|
};
|
|
|
|
class gil_scoped_release {
|
|
PyThreadState *state;
|
|
|
|
public:
|
|
// PRECONDITION: The GIL must be held when this constructor is called.
|
|
gil_scoped_release() {
|
|
assert(PyGILState_Check());
|
|
state = PyEval_SaveThread();
|
|
}
|
|
gil_scoped_release(const gil_scoped_release &) = delete;
|
|
gil_scoped_release &operator=(const gil_scoped_release &) = delete;
|
|
~gil_scoped_release() { PyEval_RestoreThread(state); }
|
|
void disarm() {}
|
|
};
|
|
|
|
# endif // PYBIND11_SIMPLE_GIL_MANAGEMENT
|
|
|
|
#else // WITH_THREAD
|
|
|
|
class gil_scoped_acquire {
|
|
public:
|
|
gil_scoped_acquire() {
|
|
// Trick to suppress `unused variable` error messages (at call sites).
|
|
(void) (this != (this + 1));
|
|
}
|
|
gil_scoped_acquire(const gil_scoped_acquire &) = delete;
|
|
gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
|
|
void disarm() {}
|
|
};
|
|
|
|
class gil_scoped_release {
|
|
public:
|
|
gil_scoped_release() {
|
|
// Trick to suppress `unused variable` error messages (at call sites).
|
|
(void) (this != (this + 1));
|
|
}
|
|
gil_scoped_release(const gil_scoped_release &) = delete;
|
|
gil_scoped_release &operator=(const gil_scoped_release &) = delete;
|
|
void disarm() {}
|
|
};
|
|
|
|
#endif // WITH_THREAD
|
|
|
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|