From da79124e5d43c54a358fd18b64414ec2c1536476 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 26 Aug 2021 23:10:15 +0100 Subject: Use local mutex for each `AstFile.arena` --- src/common_memory.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/common_memory.cpp') diff --git a/src/common_memory.cpp b/src/common_memory.cpp index ffdc9118a..dfc86b5f3 100644 --- a/src/common_memory.cpp +++ b/src/common_memory.cpp @@ -59,6 +59,8 @@ struct MemoryBlock { struct Arena { MemoryBlock * curr_block; isize minimum_block_size; + bool use_local_mutex; + BlockingMutex local_mutex; }; enum { DEFAULT_MINIMUM_BLOCK_SIZE = 8ll*1024ll*1024ll }; @@ -79,15 +81,25 @@ isize arena_align_forward_offset(Arena *arena, isize alignment) { return alignment_offset; } +void arena_init_local_mutex(Arena *arena) { + mutex_init(&arena->local_mutex); + arena->use_local_mutex = true; +} + + void *arena_alloc(Arena *arena, isize min_size, isize alignment) { GB_ASSERT(gb_is_power_of_two(alignment)); - isize size = 0; + + BlockingMutex *mutex = &global_memory_allocator_mutex; + if (arena->use_local_mutex) { + mutex = &arena->local_mutex; + } - // TODO(bill): make it so that this can be done lock free (if possible) - mutex_lock(&global_memory_allocator_mutex); + mutex_lock(mutex); + isize size = 0; if (arena->curr_block != nullptr) { size = min_size + arena_align_forward_offset(arena, alignment); } @@ -112,7 +124,7 @@ void *arena_alloc(Arena *arena, isize min_size, isize alignment) { curr_block->used += size; GB_ASSERT(curr_block->used <= curr_block->size); - mutex_unlock(&global_memory_allocator_mutex); + mutex_unlock(mutex); // NOTE(bill): memory will be zeroed by default due to virtual memory return ptr; -- cgit v1.2.3