From e4ba786948e8f3abe89ff2ec6b7618f4af2b21cb Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 15 Jul 2024 01:29:57 +0100 Subject: [PATCH] Remove use of mutex in single threaded code --- src/check_decl.cpp | 3 +-- src/checker.cpp | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 6828774e4..a1436fe03 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -182,8 +182,7 @@ gb_internal void override_entity_in_scope(Entity *original_entity, Entity *new_e original_entity->type = new_entity->type; original_entity->aliased_of = new_entity; - Ast *empty_ident = nullptr; - original_entity->identifier.compare_exchange_strong(empty_ident, new_entity->identifier); + original_entity->identifier.store(new_entity->identifier); if (original_entity->identifier.load() != nullptr && original_entity->identifier.load()->kind == Ast_Ident) { diff --git a/src/checker.cpp b/src/checker.cpp index 0e65af211..cc39e9a44 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -384,6 +384,8 @@ gb_internal Entity *scope_lookup_current(Scope *s, String const &name) { return nullptr; } +gb_global bool in_single_threaded_mode_scopes = false; + gb_internal void scope_lookup_parent(Scope *scope, String const &name, Scope **scope_, Entity **entity_) { if (scope != nullptr) { bool gone_thru_proc = false; @@ -391,9 +393,9 @@ gb_internal void scope_lookup_parent(Scope *scope, String const &name, Scope **s StringHashKey key = string_hash_string(name); for (Scope *s = scope; s != nullptr; s = s->parent) { Entity **found = nullptr; - rw_mutex_shared_lock(&s->mutex); + if (!in_single_threaded_mode_scopes) rw_mutex_shared_lock(&s->mutex); found = string_map_get(&s->elements, key); - rw_mutex_shared_unlock(&s->mutex); + if (!in_single_threaded_mode_scopes) rw_mutex_shared_unlock(&s->mutex); if (found) { Entity *e = *found; if (gone_thru_proc) { @@ -513,7 +515,11 @@ end:; gb_internal Entity *scope_insert(Scope *s, Entity *entity) { String name = entity->token.string; - return scope_insert_with_name(s, name, entity); + if (in_single_threaded_mode_scopes) { + return scope_insert_with_name_no_mutex(s, name, entity); + } else { + return scope_insert_with_name(s, name, entity); + } } gb_internal Entity *scope_insert_no_mutex(Scope *s, Entity *entity) { @@ -1795,8 +1801,7 @@ gb_internal void add_entity_use(CheckerContext *c, Ast *identifier, Entity *enti if (identifier == nullptr || identifier->kind != Ast_Ident) { return; } - Ast *empty_ident = nullptr; - entity->identifier.compare_exchange_strong(empty_ident, identifier); + entity->identifier.store(identifier); identifier->Ident.entity = entity; @@ -4591,6 +4596,8 @@ gb_internal void check_single_global_entity(Checker *c, Entity *e, DeclInfo *d) } gb_internal void check_all_global_entities(Checker *c) { + in_single_threaded_mode_scopes = true; + // NOTE(bill): This must be single threaded // Don't bother trying for_array(i, c->info.entities) { @@ -4610,6 +4617,8 @@ gb_internal void check_all_global_entities(Checker *c) { (void)type_align_of(e->type); } } + + in_single_threaded_mode_scopes = false; }