aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2023-04-18 17:20:50 +0100
committergingerBill <bill@gingerbill.org>2023-04-18 17:20:50 +0100
commit843eaf8893e3a7df9645c0bcba86c71a01a14765 (patch)
tree25ff685d654e7063d30022b2a979d69a39c3de6e /src
parent30fa8f8ac2cd19442e027042a3b0031f294b9cd2 (diff)
Fix race condition with -use-separate-modules due to type determination
Diffstat (limited to 'src')
-rw-r--r--src/llvm_backend.hpp8
-rw-r--r--src/llvm_backend_general.cpp6
-rw-r--r--src/llvm_backend_utility.cpp7
3 files changed, 18 insertions, 3 deletions
diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp
index 73e125f41..964195223 100644
--- a/src/llvm_backend.hpp
+++ b/src/llvm_backend.hpp
@@ -139,9 +139,11 @@ struct lbModule {
AstPackage *pkg; // possibly associated
AstFile *file; // possibly associated
- PtrMap<Type *, LLVMTypeRef> types;
- PtrMap<Type *, LLVMTypeRef> func_raw_types;
- PtrMap<void *, lbStructFieldRemapping> struct_field_remapping; // Key: LLVMTypeRef or Type *
+ PtrMap<Type *, LLVMTypeRef> types; // mutex: types_mutex
+ PtrMap<void *, lbStructFieldRemapping> struct_field_remapping; // Key: LLVMTypeRef or Type *, mutex: types_mutex
+ PtrMap<Type *, LLVMTypeRef> func_raw_types; // mutex: func_raw_types_mutex
+ RecursiveMutex types_mutex;
+ RecursiveMutex func_raw_types_mutex;
i32 internal_type_level;
RwMutex values_mutex;
diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp
index aa887418f..7d2f574fe 100644
--- a/src/llvm_backend_general.cpp
+++ b/src/llvm_backend_general.cpp
@@ -1498,6 +1498,9 @@ gb_internal LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *t
type = base_type(original_type);
GB_ASSERT(type->kind == Type_Proc);
+ mutex_lock(&m->func_raw_types_mutex);
+ defer (mutex_unlock(&m->func_raw_types_mutex));
+
LLVMTypeRef *found = map_get(&m->func_raw_types, type);
if (found) {
return *found;
@@ -2157,6 +2160,9 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
gb_internal LLVMTypeRef lb_type(lbModule *m, Type *type) {
type = default_type(type);
+ mutex_lock(&m->types_mutex);
+ defer (mutex_unlock(&m->types_mutex));
+
LLVMTypeRef *found = map_get(&m->types, type);
if (found) {
return *found;
diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp
index 19df9ab06..ddae2243b 100644
--- a/src/llvm_backend_utility.cpp
+++ b/src/llvm_backend_utility.cpp
@@ -910,11 +910,18 @@ gb_internal lbValue lb_address_from_load_if_readonly_parameter(lbProcedure *p, l
gb_internal lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t) {
t = base_type(t);
+
LLVMTypeRef struct_type = lb_type(m, t);
+
+ mutex_lock(&m->types_mutex);
+
auto *field_remapping = map_get(&m->struct_field_remapping, cast(void *)struct_type);
if (field_remapping == nullptr) {
field_remapping = map_get(&m->struct_field_remapping, cast(void *)t);
}
+
+ mutex_unlock(&m->types_mutex);
+
GB_ASSERT_MSG(field_remapping != nullptr, "%s", type_to_string(t));
return *field_remapping;
}