Browse Source

Remove unneeded mutex

gingerBill 2 years ago
parent
commit
c293f5b7eb
6 changed files with 41 additions and 42 deletions
  1. 17 16
      src/check_expr.cpp
  2. 6 1
      src/checker.hpp
  3. 6 12
      src/common_memory.cpp
  4. 2 3
      src/llvm_backend_stmt.cpp
  5. 1 2
      src/main.cpp
  6. 9 8
      src/threading.cpp

+ 17 - 16
src/check_expr.cpp

@@ -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->TypeName.objc_metadata != nullptr);
 	auto *objc_metadata = e->TypeName.objc_metadata;
-	mutex_lock(objc_metadata->mutex);
-	defer (mutex_unlock(objc_metadata->mutex));
+	MUTEX_GUARD(objc_metadata->mutex);
 
 	StringSet set = {};
 	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);
 	}
 
-	mutex_lock(&info->gen_procs_mutex);
-	defer (mutex_unlock(&info->gen_procs_mutex));
+	// MUTEX_GUARD(&info->gen_procs_mutex);
 
 	if (!src->Proc.is_polymorphic || src->Proc.is_poly_specialized) {
 		return false;
@@ -436,11 +434,14 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
 		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) {
-		auto procs = *found_gen_procs;
-		for_array(i, procs) {
-			Entity *other = procs[i];
+		MUTEX_GUARD(&found_gen_procs->mutex);
+		for (Entity *other : found_gen_procs->procs) {
 			Type *pt = base_type(other->type);
 			if (are_types_identical(pt, final_proc_type)) {
 				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
 		Ast *cloned_proc_type_node = clone_ast(pt->node);
 		success = check_procedure_type(&nctx, final_proc_type, cloned_proc_type_node, &operands);
-
 		if (!success) {
 			return false;
 		}
 
 		if (found_gen_procs) {
-			auto procs = *found_gen_procs;
-			for_array(i, procs) {
-				Entity *other = procs[i];
+			MUTEX_GUARD(&found_gen_procs->mutex);
+			for (Entity *other : found_gen_procs->procs) {
 				Type *pt = base_type(other->type);
 				if (are_types_identical(pt, final_proc_type)) {
 					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;
 
 	if (found_gen_procs) {
-		array_add(found_gen_procs, entity);
+		MUTEX_GUARD(&found_gen_procs->mutex);
+		array_add(&found_gen_procs->procs, entity);
 	} else {
-		auto array = array_make<Entity *>(heap_allocator());
-		array_add(&array, entity);
-		map_set(&info->gen_procs, base_entity->identifier.load(), array);
+		GenProcsData gen_proc_data = {};
+		gen_proc_data.procs = array_make<Entity *>(heap_allocator());
+		array_add(&gen_proc_data.procs, entity);
+		map_set(&info->gen_procs, base_entity->identifier.load(), gen_proc_data);
 	}
 
 	if (poly_proc_data) {

+ 6 - 1
src/checker.hpp

@@ -311,6 +311,11 @@ struct LoadFileCache {
 	StringMap<u64> hashes;
 };
 
+struct GenProcsData {
+	Array<Entity *> procs;
+	BlockingMutex   mutex;
+};
+
 // CheckerInfo stores all the symbol information for a type-checked program
 struct CheckerInfo {
 	Checker *checker;
@@ -360,7 +365,7 @@ struct CheckerInfo {
 
 	RecursiveMutex gen_procs_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; 
 
 	BlockingMutex type_info_mutex; // NOT recursive

+ 6 - 12
src/common_memory.cpp

@@ -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_allocator_mutex;
 
 gb_internal void platform_virtual_memory_init(void);
 
@@ -55,9 +54,9 @@ struct MemoryBlock {
 };
 
 struct Arena {
-	MemoryBlock *curr_block;
-	isize        minimum_block_size;
-	bool         ignore_mutex;
+	MemoryBlock * curr_block;
+	isize         minimum_block_size;
+	BlockingMutex mutex;
 };
 
 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_ASSERT(gb_is_power_of_two(alignment));
 	
-	BlockingMutex *mutex = &global_memory_allocator_mutex;
-	if (!arena->ignore_mutex) {
-		mutex_lock(mutex);
-	}
+	mutex_lock(&arena->mutex);
 	
 	isize size = 0;
 	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;
 	GB_ASSERT(curr_block->used <= curr_block->size);
 	
-	if (!arena->ignore_mutex) {
-		mutex_unlock(mutex);
-	}
+	mutex_unlock(&arena->mutex);
 	
 	// NOTE(bill): memory will be zeroed by default due to virtual memory 
 	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() {
 	return arena_allocator(&permanent_arena);
 }

+ 2 - 3
src/llvm_backend_stmt.cpp

@@ -57,9 +57,8 @@ gb_internal void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd)
 		if (pl->body != nullptr) {
 			auto *found = map_get(&info->gen_procs, ident);
 			if (found) {
-				auto procs = *found;
-				for_array(i, procs) {
-					Entity *e = procs[i];
+				MUTEX_GUARD(&found->mutex);
+				for (Entity *e : found->procs) {
 					if (!ptr_set_exists(min_dep_set, e)) {
 						continue;
 					}

+ 1 - 2
src/main.cpp

@@ -17,8 +17,7 @@
 gb_global ThreadPool global_thread_pool;
 gb_internal void init_global_thread_pool(void) {
 	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(), worker_count, "ThreadPoolWorker");
+	thread_pool_init(&global_thread_pool, permanent_allocator(), thread_count, "ThreadPoolWorker");
 }
 gb_internal bool thread_pool_add_task(WorkerTaskProc *proc, void *data) {
 	return thread_pool_add_task(&global_thread_pool, proc, data);

+ 9 - 8
src/threading.cpp

@@ -77,22 +77,23 @@ gb_internal void yield_process(void);
 
 
 struct MutexGuard {
-	MutexGuard() = delete;
+	MutexGuard()                   = delete;
 	MutexGuard(MutexGuard const &) = delete;
+	MutexGuard(MutexGuard &&)      = delete;
 
-	MutexGuard(BlockingMutex *bm) : bm{bm} {
+	explicit MutexGuard(BlockingMutex *bm) noexcept : bm{bm} {
 		mutex_lock(this->bm);
 	}
-	MutexGuard(RecursiveMutex *rm) : rm{rm} {
+	explicit MutexGuard(RecursiveMutex *rm) noexcept : rm{rm} {
 		mutex_lock(this->rm);
 	}
-	MutexGuard(BlockingMutex &bm) : bm{&bm} {
+	explicit MutexGuard(BlockingMutex &bm) noexcept : bm{&bm} {
 		mutex_lock(this->bm);
 	}
-	MutexGuard(RecursiveMutex &rm) : rm{&rm} {
+	explicit MutexGuard(RecursiveMutex &rm) noexcept : rm{&rm} {
 		mutex_lock(this->rm);
 	}
-	~MutexGuard() {
+	~MutexGuard() noexcept {
 		if (this->bm) {
 			mutex_unlock(this->bm);
 		} else if (this->rm) {
@@ -100,14 +101,14 @@ struct MutexGuard {
 		}
 	}
 
-	operator bool() const { return true; }
+	operator bool() const noexcept { return true; }
 
 	BlockingMutex *bm;
 	RecursiveMutex *rm;
 };
 
 #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 {