Make MuJoCo Python bindings compatible with free-threading.
Introduce a new header `gil.h` defining `MutexLockIfGilDisabled` to support thread-safety in both standard and free-threaded CPython builds. Protect critical shared states and registries: - Guard global Python callback pointers in `callbacks.cc` using a mutex. Move `gil_scoped_acquire` into local blocks around refcount modifications to prevent `longjmp` from bypassing destructors. - Protect raw pointer maps in `structs_wrappers.cc` with static mutexes. - Replace TOCTOU race in `mjcb_time` initialization with thread-safe `std::call_once`. - Add synchronization to lazy indexer array cache initialization in `indexers.cc` and `indexer_xmacro.h`. - Protect vector mutations in `StructListBase::PopulateUpTo` in `structs.h` with a mutex. - Revert unnecessary atomic changes to threadpool counters. - Declare free-threading compatibility by passing `pybind11::mod_gil_not_used()` to all extension modules. Fixes #3259 Fixes #3256 Fixes #2978 PiperOrigin-RevId: 941101502 Change-Id: Iec4ce58afcbc75d4b0be6a9a21fc8a47854242e3
This commit is contained in:
committed by
Copybara-Service
parent
cab191755a
commit
a07ae6f849
@@ -201,7 +201,7 @@ class Viewer {
|
||||
std::vector<std::byte> pixels_;
|
||||
};
|
||||
|
||||
PYBIND11_MODULE(native_viewer_cc, m) {
|
||||
PYBIND11_MODULE(native_viewer_cc, m, pybind11::mod_gil_not_used()) {
|
||||
pybind11::module_::import("mujoco._structs");
|
||||
pybind11::class_<Viewer>(m, "Viewer")
|
||||
.def(pybind11::init<const std::string&, int, int, const std::string&>())
|
||||
|
||||
@@ -45,7 +45,7 @@ py::object Parse(std::string_view filepath) {
|
||||
|
||||
} // namespace mujoco::python
|
||||
|
||||
PYBIND11_MODULE(parser, m) {
|
||||
PYBIND11_MODULE(parser, m, pybind11::mod_gil_not_used()) {
|
||||
pybind11::module_::import("mujoco._structs");
|
||||
m.def("parse", &mujoco::python::Parse,
|
||||
pybind11::return_value_policy::take_ownership);
|
||||
|
||||
@@ -75,7 +75,7 @@ class Renderer {
|
||||
|
||||
} // namespace mujoco::python
|
||||
|
||||
PYBIND11_MODULE(renderer, m) {
|
||||
PYBIND11_MODULE(renderer, m, pybind11::mod_gil_not_used()) {
|
||||
pybind11::module_::import("mujoco._structs");
|
||||
pybind11::class_<mujoco::python::Renderer>(m, "Renderer")
|
||||
.def(pybind11::init<const std::string&>())
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace py = pybind11;
|
||||
|
||||
using StepControl = mujoco::platform::StepControl;
|
||||
|
||||
PYBIND11_MODULE(sim, m) {
|
||||
PYBIND11_MODULE(sim, m, pybind11::mod_gil_not_used()) {
|
||||
py::module_::import("mujoco._structs");
|
||||
m.doc() = "MuJoCo platform simulation bindings for Link.";
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ struct RenderFlags {
|
||||
std::array<uint8_t, mjNRNDFLAG> flags = {0};
|
||||
};
|
||||
|
||||
PYBIND11_MODULE(ux, m) {
|
||||
PYBIND11_MODULE(ux, m, pybind11::mod_gil_not_used()) {
|
||||
py::module_::import("mujoco._structs");
|
||||
py::class_<RenderFlags>(m, "RenderFlags")
|
||||
.def(py::init<>())
|
||||
|
||||
Reference in New Issue
Block a user