Skip to content

Commit 4a93640

Browse files
yuvaltassacopybara-github
authored andcommitted
Fix data race lazy-init spin lock deadlock on Windows.
PiperOrigin-RevId: 942556300 Change-Id: Iffe7a3e71c0f6273603e2ada14bf8d924b0ee6cf
1 parent 11f1da0 commit 4a93640

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/engine/engine_util_errmem.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
#include <string.h>
2222
#include <time.h>
2323

24+
#if defined(_WIN32)
25+
#include <windows.h>
26+
#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__EMSCRIPTEN__)
27+
#include <sched.h>
28+
#endif
29+
2430
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
2531
#include <unistd.h>
2632
#endif
@@ -152,7 +158,11 @@ static const mjLogConfig* mju_getLogConfigPtr(void) {
152158
} else {
153159
// another thread is initializing; spin until it completes
154160
while (!mj_atomic_load_bool(&env_init_done)) {
155-
// empty
161+
#if defined(_WIN32)
162+
Sleep(0);
163+
#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__EMSCRIPTEN__)
164+
sched_yield();
165+
#endif
156166
}
157167
}
158168
}

0 commit comments

Comments
 (0)