-fixed events thread safety
This commit is contained in:
parent
f27e4c30ad
commit
49a60d33c5
2 changed files with 17 additions and 0 deletions
|
|
@ -8,6 +8,7 @@ import std.experimental.allocator;
|
||||||
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;
|
import std.experimental.allocator.mallocator : AlignedMallocator, Mallocator;
|
||||||
import std.algorithm.comparison : max;
|
import std.algorithm.comparison : max;
|
||||||
|
|
||||||
|
import core.sync.mutex;
|
||||||
|
|
||||||
/*struct Event
|
/*struct Event
|
||||||
{
|
{
|
||||||
|
|
@ -73,9 +74,15 @@ struct EventManager
|
||||||
void initialize(EntityManager m) nothrow @nogc
|
void initialize(EntityManager m) nothrow @nogc
|
||||||
{
|
{
|
||||||
allocator = BlockAllocator(events_block_size, events_blocks_in_allocation);
|
allocator = BlockAllocator(events_block_size, events_blocks_in_allocation);
|
||||||
|
event_block_alloc_mutex = Mallocator.instance.make!Mutex;
|
||||||
manager = m;
|
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
|
export void sendEvent(Ev)(EntityID id, Ev event, uint thread_id = 0) nothrow @nogc
|
||||||
{
|
{
|
||||||
uint block_id = current_index+thread_id;
|
uint block_id = current_index+thread_id;
|
||||||
|
|
@ -87,6 +94,10 @@ struct EventManager
|
||||||
|
|
||||||
if(block is null)
|
if(block is null)
|
||||||
{
|
{
|
||||||
|
event_block_alloc_mutex.lock_nothrow();
|
||||||
|
scope (exit)
|
||||||
|
event_block_alloc_mutex.unlock_nothrow();
|
||||||
|
|
||||||
block = cast(EventBlock*) allocator.getBlock();
|
block = cast(EventBlock*) allocator.getBlock();
|
||||||
*block = EventBlock();
|
*block = EventBlock();
|
||||||
data.first_blocks[block_id] = block;
|
data.first_blocks[block_id] = block;
|
||||||
|
|
@ -95,6 +106,10 @@ struct EventManager
|
||||||
|
|
||||||
if(block.count >= data.max_events)
|
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();
|
EventBlock* new_block = cast(EventBlock*) allocator.getBlock();
|
||||||
*new_block = EventBlock();
|
*new_block = EventBlock();
|
||||||
block.next = new_block;
|
block.next = new_block;
|
||||||
|
|
@ -265,6 +280,7 @@ struct EventManager
|
||||||
EventList process_events;*/
|
EventList process_events;*/
|
||||||
uint current_index = 0;
|
uint current_index = 0;
|
||||||
EventData[] events;
|
EventData[] events;
|
||||||
|
Mutex event_block_alloc_mutex;
|
||||||
|
|
||||||
BlockAllocator/*!(events_block_size, events_blocks_in_allocation)*/ allocator;
|
BlockAllocator/*!(events_block_size, events_blocks_in_allocation)*/ allocator;
|
||||||
EntityManager manager;
|
EntityManager manager;
|
||||||
|
|
|
||||||
|
|
@ -253,6 +253,7 @@ export class EntityManager
|
||||||
~this()
|
~this()
|
||||||
{
|
{
|
||||||
id_manager.deinitialize();
|
id_manager.deinitialize();
|
||||||
|
event_manager.destroy();
|
||||||
|
|
||||||
if(threads)Mallocator.instance.dispose(threads);
|
if(threads)Mallocator.instance.dispose(threads);
|
||||||
if(entity_block_alloc_mutex)Mallocator.instance.dispose(entity_block_alloc_mutex);
|
if(entity_block_alloc_mutex)Mallocator.instance.dispose(entity_block_alloc_mutex);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue