Browse Source

Remove use of mutex in single threaded code

gingerBill 1 year ago
parent
commit
e4ba786948
2 changed files with 15 additions and 7 deletions
  1. 1 2
      src/check_decl.cpp
  2. 14 5
      src/checker.cpp

+ 1 - 2
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) {

+ 14 - 5
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;
 }