Remove unneeded mutex

This commit is contained in:
gingerBill
2023-01-02 16:56:05 +00:00
parent fa562ec5d6
commit c293f5b7eb
6 changed files with 41 additions and 42 deletions
+17 -16
View File
@@ -193,8 +193,7 @@ gb_internal void check_did_you_mean_objc_entity(String const &name, Entity *e, b
GB_ASSERT(e->kind == Entity_TypeName); GB_ASSERT(e->kind == Entity_TypeName);
GB_ASSERT(e->TypeName.objc_metadata != nullptr); GB_ASSERT(e->TypeName.objc_metadata != nullptr);
auto *objc_metadata = e->TypeName.objc_metadata; auto *objc_metadata = e->TypeName.objc_metadata;
mutex_lock(objc_metadata->mutex); MUTEX_GUARD(objc_metadata->mutex);
defer (mutex_unlock(objc_metadata->mutex));
StringSet set = {}; StringSet set = {};
string_set_init(&set, heap_allocator()); string_set_init(&set, heap_allocator());
@@ -369,8 +368,7 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
GB_ASSERT(dst == nullptr); GB_ASSERT(dst == nullptr);
} }
mutex_lock(&info->gen_procs_mutex); // MUTEX_GUARD(&info->gen_procs_mutex);
defer (mutex_unlock(&info->gen_procs_mutex));
if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) { if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) {
return false; return false;
@@ -436,11 +434,14 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
return false; return false;
} }
auto *found_gen_procs = map_get(&info->gen_procs, base_entity->identifier.load()); GenProcsData *found_gen_procs = nullptr;
MUTEX_GUARD(&info->gen_procs_mutex);
found_gen_procs = map_get(&info->gen_procs, base_entity->identifier.load());
if (found_gen_procs) { if (found_gen_procs) {
auto procs = *found_gen_procs; MUTEX_GUARD(&found_gen_procs->mutex);
for_array(i, procs) { for (Entity *other : found_gen_procs->procs) {
Entity *other = procs[i];
Type *pt = base_type(other->type); Type *pt = base_type(other->type);
if (are_types_identical(pt, final_proc_type)) { if (are_types_identical(pt, final_proc_type)) {
if (poly_proc_data) { if (poly_proc_data) {
@@ -463,15 +464,13 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
// LEAK TODO(bill): Cloning this AST may be leaky // LEAK TODO(bill): Cloning this AST may be leaky
Ast *cloned_proc_type_node = clone_ast(pt->node); Ast *cloned_proc_type_node = clone_ast(pt->node);
success = check_procedure_type(&nctx, final_proc_type, cloned_proc_type_node, &operands); success = check_procedure_type(&nctx, final_proc_type, cloned_proc_type_node, &operands);
if (!success) { if (!success) {
return false; return false;
} }
if (found_gen_procs) { if (found_gen_procs) {
auto procs = *found_gen_procs; MUTEX_GUARD(&found_gen_procs->mutex);
for_array(i, procs) { for (Entity *other : found_gen_procs->procs) {
Entity *other = procs[i];
Type *pt = base_type(other->type); Type *pt = base_type(other->type);
if (are_types_identical(pt, final_proc_type)) { if (are_types_identical(pt, final_proc_type)) {
if (poly_proc_data) { if (poly_proc_data) {
@@ -567,11 +566,13 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
proc_info->poly_def_node = poly_def_node; proc_info->poly_def_node = poly_def_node;
if (found_gen_procs) { if (found_gen_procs) {
array_add(found_gen_procs, entity); MUTEX_GUARD(&found_gen_procs->mutex);
array_add(&found_gen_procs->procs, entity);
} else { } else {
auto array = array_make<Entity *>(heap_allocator()); GenProcsData gen_proc_data = {};
array_add(&array, entity); gen_proc_data.procs = array_make<Entity *>(heap_allocator());
map_set(&info->gen_procs, base_entity->identifier.load(), array); array_add(&gen_proc_data.procs, entity);
map_set(&info->gen_procs, base_entity->identifier.load(), gen_proc_data);
} }
if (poly_proc_data) { if (poly_proc_data) {
+6 -1
View File
@@ -311,6 +311,11 @@ struct LoadFileCache {
StringMap<u64> hashes; StringMap<u64> hashes;
}; };
struct GenProcsData {
Array<Entity *> procs;
BlockingMutex mutex;
};
// CheckerInfo stores all the symbol information for a type-checked program // CheckerInfo stores all the symbol information for a type-checked program
struct CheckerInfo { struct CheckerInfo {
Checker *checker; Checker *checker;
@@ -360,7 +365,7 @@ struct CheckerInfo {
RecursiveMutex gen_procs_mutex; RecursiveMutex gen_procs_mutex;
RecursiveMutex gen_types_mutex; RecursiveMutex gen_types_mutex;
PtrMap<Ast *, Array<Entity *> > gen_procs; // Key: Ast * | Identifier -> Entity PtrMap<Ast *, GenProcsData > gen_procs; // Key: Ast * | Identifier -> Entity
PtrMap<Type *, Array<Entity *> > gen_types; PtrMap<Type *, Array<Entity *> > gen_types;
BlockingMutex type_info_mutex; // NOT recursive BlockingMutex type_info_mutex; // NOT recursive
+6 -12
View File
@@ -37,7 +37,6 @@ gb_internal gb_inline void *align_formula_ptr(void *ptr, isize align) {
gb_global BlockingMutex global_memory_block_mutex; gb_global BlockingMutex global_memory_block_mutex;
gb_global BlockingMutex global_memory_allocator_mutex;
gb_internal void platform_virtual_memory_init(void); gb_internal void platform_virtual_memory_init(void);
@@ -55,9 +54,9 @@ struct MemoryBlock {
}; };
struct Arena { struct Arena {
MemoryBlock *curr_block; MemoryBlock * curr_block;
isize minimum_block_size; isize minimum_block_size;
bool ignore_mutex; BlockingMutex mutex;
}; };
enum { DEFAULT_MINIMUM_BLOCK_SIZE = 8ll*1024ll*1024ll }; enum { DEFAULT_MINIMUM_BLOCK_SIZE = 8ll*1024ll*1024ll };
@@ -83,10 +82,7 @@ gb_internal isize arena_align_forward_offset(Arena *arena, isize alignment) {
gb_internal void *arena_alloc(Arena *arena, isize min_size, isize alignment) { gb_internal void *arena_alloc(Arena *arena, isize min_size, isize alignment) {
GB_ASSERT(gb_is_power_of_two(alignment)); GB_ASSERT(gb_is_power_of_two(alignment));
BlockingMutex *mutex = &global_memory_allocator_mutex; mutex_lock(&arena->mutex);
if (!arena->ignore_mutex) {
mutex_lock(mutex);
}
isize size = 0; isize size = 0;
if (arena->curr_block != nullptr) { if (arena->curr_block != nullptr) {
@@ -113,9 +109,7 @@ gb_internal void *arena_alloc(Arena *arena, isize min_size, isize alignment) {
curr_block->used += size; curr_block->used += size;
GB_ASSERT(curr_block->used <= curr_block->size); GB_ASSERT(curr_block->used <= curr_block->size);
if (!arena->ignore_mutex) { mutex_unlock(&arena->mutex);
mutex_unlock(mutex);
}
// NOTE(bill): memory will be zeroed by default due to virtual memory // NOTE(bill): memory will be zeroed by default due to virtual memory
return ptr; return ptr;
@@ -304,7 +298,7 @@ gb_internal GB_ALLOCATOR_PROC(arena_allocator_proc) {
} }
gb_global gb_thread_local Arena permanent_arena = {nullptr, DEFAULT_MINIMUM_BLOCK_SIZE, true}; gb_global gb_thread_local Arena permanent_arena = {nullptr, DEFAULT_MINIMUM_BLOCK_SIZE};
gb_internal gbAllocator permanent_allocator() { gb_internal gbAllocator permanent_allocator() {
return arena_allocator(&permanent_arena); return arena_allocator(&permanent_arena);
} }
+2 -3
View File
@@ -57,9 +57,8 @@ gb_internal void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd)
if (pl->body != nullptr) { if (pl->body != nullptr) {
auto *found = map_get(&info->gen_procs, ident); auto *found = map_get(&info->gen_procs, ident);
if (found) { if (found) {
auto procs = *found; MUTEX_GUARD(&found->mutex);
for_array(i, procs) { for (Entity *e : found->procs) {
Entity *e = procs[i];
if (!ptr_set_exists(min_dep_set, e)) { if (!ptr_set_exists(min_dep_set, e)) {
continue; continue;
} }
+1 -2
View File
@@ -17,8 +17,7 @@
gb_global ThreadPool global_thread_pool; gb_global ThreadPool global_thread_pool;
gb_internal void init_global_thread_pool(void) { gb_internal void init_global_thread_pool(void) {
isize thread_count = gb_max(build_context.thread_count, 1); isize thread_count = gb_max(build_context.thread_count, 1);
isize worker_count = thread_count-1; // NOTE(bill): The main thread will also be used for work thread_pool_init(&global_thread_pool, permanent_allocator(), thread_count, "ThreadPoolWorker");
thread_pool_init(&global_thread_pool, permanent_allocator(), worker_count, "ThreadPoolWorker");
} }
gb_internal bool thread_pool_add_task(WorkerTaskProc *proc, void *data) { gb_internal bool thread_pool_add_task(WorkerTaskProc *proc, void *data) {
return thread_pool_add_task(&global_thread_pool, proc, data); return thread_pool_add_task(&global_thread_pool, proc, data);
+9 -8
View File
@@ -77,22 +77,23 @@ gb_internal void yield_process(void);
struct MutexGuard { struct MutexGuard {
MutexGuard() = delete; MutexGuard() = delete;
MutexGuard(MutexGuard const &) = delete; MutexGuard(MutexGuard const &) = delete;
MutexGuard(MutexGuard &&) = delete;
MutexGuard(BlockingMutex *bm) : bm{bm} { explicit MutexGuard(BlockingMutex *bm) noexcept : bm{bm} {
mutex_lock(this->bm); mutex_lock(this->bm);
} }
MutexGuard(RecursiveMutex *rm) : rm{rm} { explicit MutexGuard(RecursiveMutex *rm) noexcept : rm{rm} {
mutex_lock(this->rm); mutex_lock(this->rm);
} }
MutexGuard(BlockingMutex &bm) : bm{&bm} { explicit MutexGuard(BlockingMutex &bm) noexcept : bm{&bm} {
mutex_lock(this->bm); mutex_lock(this->bm);
} }
MutexGuard(RecursiveMutex &rm) : rm{&rm} { explicit MutexGuard(RecursiveMutex &rm) noexcept : rm{&rm} {
mutex_lock(this->rm); mutex_lock(this->rm);
} }
~MutexGuard() { ~MutexGuard() noexcept {
if (this->bm) { if (this->bm) {
mutex_unlock(this->bm); mutex_unlock(this->bm);
} else if (this->rm) { } else if (this->rm) {
@@ -100,14 +101,14 @@ struct MutexGuard {
} }
} }
operator bool() const { return true; } operator bool() const noexcept { return true; }
BlockingMutex *bm; BlockingMutex *bm;
RecursiveMutex *rm; RecursiveMutex *rm;
}; };
#define MUTEX_GUARD_BLOCK(m) if (MutexGuard GB_DEFER_3(_mutex_guard_){m}) #define MUTEX_GUARD_BLOCK(m) if (MutexGuard GB_DEFER_3(_mutex_guard_){m})
#define MUTEX_GUARD(m) MutexGuard GB_DEFER_3(_mutex_guard_){m} #define MUTEX_GUARD(m) mutex_lock(m); defer (mutex_unlock(m))
struct RecursiveMutex { struct RecursiveMutex {