Ymir native: move world pool lock into ymir_box3d_soa.c and delete planar ymir_box3d.c

This commit is contained in:
Meta Vens 2026-12-03 21:35:12 +01:00
commit ee8ac211d2
4 changed files with 36 additions and 1822 deletions

View file

@ -11,7 +11,7 @@ set(BOX3D_VALIDATE ON CACHE BOOL "" FORCE)
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../extern/box3d" "${CMAKE_CURRENT_BINARY_DIR}/box3d")
add_library(ymir_box3d SHARED ymir_box3d.c ymir_box3d_soa.c ymir_particles.c)
add_library(ymir_box3d SHARED ymir_box3d_soa.c ymir_particles.c)
target_link_libraries(ymir_box3d PRIVATE box3d::box3d)
target_include_directories(ymir_box3d PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../extern/box3d/include")
target_compile_features(ymir_box3d PRIVATE c_std_17)

File diff suppressed because it is too large Load diff

View file

@ -5,8 +5,7 @@
// place through `ymir_box3d_soa_get_view`: the pointers are stable for the world's whole life
// (capacities are fixed at creation) and the state lanes change only inside `ymir_box3d_soa_step`.
//
// This world is not the retained planar session in ymir_box3d.c: that one locks Y, zeroes gravity
// and hands out an array of structs by copy. See docs/soa-world.md.
// See docs/soa-world.md.
#include <box3d/base.h>
#include <box3d/box3d.h>
@ -15,7 +14,6 @@
#include <box3d/types.h>
#include "ymir_particles.h"
#include "ymir_world_lock.h"
#include <assert.h>
#include <float.h>
@ -24,6 +22,40 @@
#include <stdlib.h>
#include <string.h>
// Process-wide lock around Box3D world creation and destruction. b3CreateWorld and b3DestroyWorld scan
// and rewrite the global world pool (b3_worlds) with no synchronization, so two threads creating worlds
// at once can claim the same slot and corrupt it. Stepping and every other call touch only the caller's
// own world and stay unlocked. A single world is still not thread-safe.
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
static SRWLOCK ymir_world_pool_lock = SRWLOCK_INIT;
#define YMIR_WORLD_POOL_LOCK() AcquireSRWLockExclusive(&ymir_world_pool_lock)
#define YMIR_WORLD_POOL_UNLOCK() ReleaseSRWLockExclusive(&ymir_world_pool_lock)
#else
#include <pthread.h>
static pthread_mutex_t ymir_world_pool_lock = PTHREAD_MUTEX_INITIALIZER;
#define YMIR_WORLD_POOL_LOCK() pthread_mutex_lock(&ymir_world_pool_lock)
#define YMIR_WORLD_POOL_UNLOCK() pthread_mutex_unlock(&ymir_world_pool_lock)
#endif
static b3WorldId ymir_create_world_locked(const b3WorldDef* def)
{
YMIR_WORLD_POOL_LOCK();
b3WorldId world_id = b3CreateWorld(def);
YMIR_WORLD_POOL_UNLOCK();
return world_id;
}
static void ymir_destroy_world_locked(b3WorldId world_id)
{
YMIR_WORLD_POOL_LOCK();
b3DestroyWorld(world_id);
YMIR_WORLD_POOL_UNLOCK();
}
#if defined(_WIN32)
#define YMIR_EXPORT __declspec(dllexport)
#define YMIR_CALL __cdecl

View file

@ -1,12 +0,0 @@
// Process-wide lock around Box3D world creation and destruction. b3CreateWorld and b3DestroyWorld scan
// and rewrite the global world pool (b3_worlds) with no synchronization, so two threads creating worlds
// at once can claim the same slot and corrupt it. Stepping and every other call touch only the caller's
// own world and stay unlocked. A single world is still not thread-safe. One lock, defined in
// ymir_box3d.c, serves every translation unit.
#pragma once
#include <box3d/box3d.h>
#include <box3d/types.h>
b3WorldId ymir_create_world_locked(const b3WorldDef* def);
void ymir_destroy_world_locked(b3WorldId world_id);