Files
Mujoco_WASM/python/mujoco/constants.cc
T
Saran Tunyasuvunakool a07ae6f849 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
2026-07-01 08:08:42 -07:00

102 lines
2.5 KiB
C++

// Copyright 2022 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <utility>
#include <vector>
#include <mujoco/mjmodel.h>
#include <mujoco/mjtype.h>
#include <mujoco/mjvisualize.h>
#include <mujoco/mujoco.h>
#include <pybind11/cast.h>
#include <pybind11/pybind11.h>
namespace mujoco::python {
namespace {
namespace py = ::pybind11;
template <auto N>
py::tuple MakeTuple(
const char* (&strings)[N]) {
py::list result;
for (int i = 0; i < N; i++) {
result.append(py::str(strings[i]));
}
return std::move(result);
}
template <auto N>
py::tuple MakeTuple(const char* (&strings)[N][3]) {
py::list result;
for (int i = 0; i < N; i++) {
result.append(py::make_tuple(
py::str(strings[i][0]),
py::str(strings[i][1]),
py::str(strings[i][2])));
}
return std::move(result);
}
PYBIND11_MODULE(_constants, pymodule, pybind11::mod_gil_not_used()) {
#define X(var) pymodule.attr(#var) = var
// from mjmodel.h
X(mjPI);
X(mjMAXVAL);
X(mjMINMU);
X(mjMINIMP);
X(mjMAXIMP);
X(mjMAXCONPAIR);
X(mjMAXTREEDEPTH);
X(mjMAXFLEXNODES);
X(mjMINAWAKE);
X(mjNEQDATA);
X(mjNDYN);
X(mjNGAIN);
X(mjNBIAS);
X(mjNFLUID);
X(mjNREF);
X(mjNIMP);
X(mjNSENS);
X(mjNSOLVER);
X(mjNISLAND);
X(mjNPOLY);
// from mjvisualize.h
X(mjNGROUP);
X(mjMAXLIGHT);
X(mjMAXOVERLAY);
X(mjMAXLINE);
X(mjMAXLINEPNT);
X(mjMAXPLANEGRID);
// from mujoco.h
X(mjVERSION_HEADER);
// from mjtype.h
X(mjMINVAL);
#undef X
pymodule.attr("mjDISABLESTRING") = MakeTuple(mjDISABLESTRING);
pymodule.attr("mjENABLESTRING") = MakeTuple(mjENABLESTRING);
pymodule.attr("mjTIMERSTRING") = MakeTuple(mjTIMERSTRING);
pymodule.attr("mjLABELSTRING") = MakeTuple(mjLABELSTRING);
pymodule.attr("mjFRAMESTRING") = MakeTuple(mjFRAMESTRING);
pymodule.attr("mjVISSTRING") = MakeTuple(mjVISSTRING);
pymodule.attr("mjRNDSTRING") = MakeTuple(mjRNDSTRING);
pymodule.attr("MJTNUM_BYTES") = sizeof(mjtNum);
}
} // namespace
} // namespace mujoco::python