SanderMertens / flecs

A fast entity component system (ECS) for C & C++

Home Page:https://www.flecs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make thread-safe api for generation management

Hexlord opened this issue · comments

Describe the problem you are trying to solve.
When using pre-allocated manually managed id entities (e.g. allocating 100000 entities in advance, and never destruct-ing them, only clear-ing them) we want a way to lock-lessly recycle them by incrementing their generation, but currently the api for that is not thread safe:

ecs_set_entity_generation(world, id_with_new_generation); // not thread-safe (chunk alloc)
ecs_ensure(world, id_with_new_generation); // not thread-safe (chunk alloc + dense grow)

Describe the solution you'd like
Have a thread-safe api that never grows dense, never creates chunks, and only asserts that those already exist, and then we could "touch" our 100000 pre-allocated entities by giving them a dummy component like a name (to enforce chunk alloc & dense index), and then lock-lessly update their generation at arbitrary times from multi-threaded systems without sync-points, by never destruct-ing them - only clear-ing them (to keep chunk and dense intact).

This is possible with the current API:

if (ecs_exists(world, id_without_generation)) {
  ecs_set_entity_generation(world, id_with_new_generation);
}

Note that this is not thread safe, as multiple threads may try to overwrite the generation of the same entity.

Note that this is not thread safe, as multiple threads may try to overwrite the generation of the same entity.

I have a guarantee that only one thread tries to write to generation of each entity, so I seem to be safe with current api, cheers!