diff --git a/source/ecs/events.d b/source/ecs/events.d index 47d9415..b2bd8cc 100644 --- a/source/ecs/events.d +++ b/source/ecs/events.d @@ -8,6 +8,7 @@ import std.experimental.allocator; import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator; import std.algorithm.comparison : max; +import core.sync.mutex; /*struct Event { @@ -73,9 +74,15 @@ struct EventManager void initialize(EntityManager m) nothrow @nogc { allocator = BlockAllocator(events_block_size, events_blocks_in_allocation); + event_block_alloc_mutex = Mallocator.instance.make!Mutex; manager = m; } + void destroy() + { + Mallocator.instance.dispose(event_block_alloc_mutex); + } + export void sendEvent(Ev)(EntityID id, Ev event, uint thread_id = 0) nothrow @nogc { uint block_id = current_index+thread_id; @@ -87,6 +94,10 @@ struct EventManager if(block is null) { + event_block_alloc_mutex.lock_nothrow(); + scope (exit) + event_block_alloc_mutex.unlock_nothrow(); + block = cast(EventBlock*) allocator.getBlock(); *block = EventBlock(); data.first_blocks[block_id] = block; @@ -95,6 +106,10 @@ struct EventManager if(block.count >= data.max_events) { + event_block_alloc_mutex.lock_nothrow(); + scope (exit) + event_block_alloc_mutex.unlock_nothrow(); + EventBlock* new_block = cast(EventBlock*) allocator.getBlock(); *new_block = EventBlock(); block.next = new_block; @@ -265,6 +280,7 @@ struct EventManager EventList process_events;*/ uint current_index = 0; EventData[] events; + Mutex event_block_alloc_mutex; BlockAllocator/*!(events_block_size, events_blocks_in_allocation)*/ allocator; EntityManager manager; diff --git a/source/ecs/manager.d b/source/ecs/manager.d index 19d7cd1..a96a699 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -253,6 +253,7 @@ export class EntityManager ~this() { id_manager.deinitialize(); + event_manager.destroy(); if(threads)Mallocator.instance.dispose(threads); if(entity_block_alloc_mutex)Mallocator.instance.dispose(entity_block_alloc_mutex);