Move cache creation into lambda initializer to avoid data race.

PiperOrigin-RevId: 853295269
Change-Id: Ic1c8f3d2fec876070da432f8b709b341f3c6cc0a
This commit is contained in:
Sam Haves
2026-01-07 09:29:06 -08:00
committed by Copybara-Service
parent e1261ba5f1
commit 3c93d6b979
+10 -7
View File
@@ -1922,12 +1922,15 @@ void mj_clearCache(mjCache* cache) {
// get the internal asset cache used by the compiler
mjCache* mj_getCache() {
static mjCache cache_cwrapper = {0};
// mjCCache is not trivially destructible and so the global cache needs to
// allocated on the heap
if constexpr (kGlobalCacheSize != 0) {
static mjCCache* cache = new(std::nothrow) mjCCache(kGlobalCacheSize);
cache_cwrapper.impl_ = cache->Capacity() > 0 ? cache : nullptr;
}
static mjCache cache_cwrapper = []() {
mjCache c = {0};
// mjCCache is not trivially destructible and so the global cache needs to
// allocated on the heap
if constexpr (kGlobalCacheSize != 0) {
static mjCCache* cache = new (std::nothrow) mjCCache(kGlobalCacheSize);
c.impl_ = cache->Capacity() > 0 ? cache : nullptr;
}
return c;
}();
return &cache_cwrapper;
}