-fixed events thread safety

This commit is contained in:
Mergul 2019-08-14 11:00:21 +02:00
parent f27e4c30ad
commit 49a60d33c5
2 changed files with 17 additions and 0 deletions

View file

@ -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;