diff options
| author | gingerBill <bill@gingerbill.org> | 2023-01-04 15:15:12 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2023-01-04 15:15:12 +0000 |
| commit | faa735d0c745ddc0b550e2a54f10588c873841b7 (patch) | |
| tree | 1e61f7f70aa130c77c2b42de9cba60de6c09c35f /src | |
| parent | d4e18109da5fa051d689be84a6ecf1e77348c74e (diff) | |
Localize gen_types mutexes
Diffstat (limited to 'src')
| -rw-r--r-- | src/check_type.cpp | 119 | ||||
| -rw-r--r-- | src/checker.hpp | 9 | ||||
| -rw-r--r-- | src/path.cpp | 12 |
3 files changed, 75 insertions, 65 deletions
diff --git a/src/check_type.cpp b/src/check_type.cpp index 05fdbf4d3..fd4e965d4 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -257,63 +257,67 @@ gb_internal bool check_custom_align(CheckerContext *ctx, Ast *node, i64 *align_) gb_internal Entity *find_polymorphic_record_entity(CheckerContext *ctx, Type *original_type, isize param_count, Array<Operand> const &ordered_operands, bool *failure) { - mutex_lock(&ctx->info->gen_types_mutex); - defer (mutex_unlock(&ctx->info->gen_types_mutex)); + rw_mutex_shared_lock(&ctx->info->gen_types_mutex); // @@global auto *found_gen_types = map_get(&ctx->info->gen_types, original_type); - if (found_gen_types != nullptr) { - // GB_ASSERT_MSG(ordered_operands.count >= param_count, "%td >= %td", ordered_operands.count, param_count); - - for_array(i, *found_gen_types) { - Entity *e = (*found_gen_types)[i]; - Type *t = base_type(e->type); - TypeTuple *tuple = get_record_polymorphic_params(t); - GB_ASSERT(param_count == tuple->variables.count); - - bool skip = false; - - for (isize j = 0; j < param_count; j++) { - Entity *p = tuple->variables[j]; - Operand o = {}; - if (j < ordered_operands.count) { - o = ordered_operands[j]; + if (found_gen_types == nullptr) { + rw_mutex_shared_unlock(&ctx->info->gen_types_mutex); // @@global + return nullptr; + } + + rw_mutex_shared_lock(&found_gen_types->mutex); // @@local + defer (rw_mutex_shared_unlock(&found_gen_types->mutex)); // @@local + + rw_mutex_shared_unlock(&ctx->info->gen_types_mutex); // @@global + + for (Entity *e : found_gen_types->types) { + Type *t = base_type(e->type); + TypeTuple *tuple = get_record_polymorphic_params(t); + GB_ASSERT(param_count == tuple->variables.count); + + bool skip = false; + + for (isize j = 0; j < param_count; j++) { + Entity *p = tuple->variables[j]; + Operand o = {}; + if (j < ordered_operands.count) { + o = ordered_operands[j]; + } + if (o.expr == nullptr) { + continue; + } + Entity *oe = entity_of_node(o.expr); + if (p == oe) { + // NOTE(bill): This is the same type, make sure that it will be be same thing and use that + // Saves on a lot of checking too below + continue; + } + + if (p->kind == Entity_TypeName) { + if (is_type_polymorphic(o.type)) { + // NOTE(bill): Do not add polymorphic version to the gen_types + skip = true; + break; } - if (o.expr == nullptr) { - continue; + if (!are_types_identical(o.type, p->type)) { + skip = true; + break; } - Entity *oe = entity_of_node(o.expr); - if (p == oe) { - // NOTE(bill): This is the same type, make sure that it will be be same thing and use that - // Saves on a lot of checking too below - continue; + } else if (p->kind == Entity_Constant) { + if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) { + skip = true; + break; } - - if (p->kind == Entity_TypeName) { - if (is_type_polymorphic(o.type)) { - // NOTE(bill): Do not add polymorphic version to the gen_types - skip = true; - break; - } - if (!are_types_identical(o.type, p->type)) { - skip = true; - break; - } - } else if (p->kind == Entity_Constant) { - if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) { - skip = true; - break; - } - if (!are_types_identical(o.type, p->type)) { - skip = true; - break; - } - } else { - GB_PANIC("Unknown entity kind"); + if (!are_types_identical(o.type, p->type)) { + skip = true; + break; } + } else { + GB_PANIC("Unknown entity kind"); } - if (!skip) { - return e; - } + } + if (!skip) { + return e; } } return nullptr; @@ -346,16 +350,19 @@ gb_internal void add_polymorphic_record_entity(CheckerContext *ctx, Ast *node, T // TODO(bill): Is this even correct? Or should the metadata be copied? e->TypeName.objc_metadata = original_type->Named.type_name->TypeName.objc_metadata; - mutex_lock(&ctx->info->gen_types_mutex); + rw_mutex_lock(&ctx->info->gen_types_mutex); auto *found_gen_types = map_get(&ctx->info->gen_types, original_type); if (found_gen_types) { - array_add(found_gen_types, e); + rw_mutex_lock(&found_gen_types->mutex); + array_add(&found_gen_types->types, e); + rw_mutex_unlock(&found_gen_types->mutex); } else { - auto array = array_make<Entity *>(heap_allocator()); - array_add(&array, e); - map_set(&ctx->info->gen_types, original_type, array); + GenTypesData gen_types = {}; + gen_types.types = array_make<Entity *>(heap_allocator()); + array_add(&gen_types.types, e); + map_set(&ctx->info->gen_types, original_type, gen_types); } - mutex_unlock(&ctx->info->gen_types_mutex); + rw_mutex_unlock(&ctx->info->gen_types_mutex); } gb_internal Type *check_record_polymorphic_params(CheckerContext *ctx, Ast *polymorphic_params, diff --git a/src/checker.hpp b/src/checker.hpp index 821d43922..92926689e 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -327,6 +327,11 @@ struct GenProcsData { RwMutex mutex; }; +struct GenTypesData { + Array<Entity *> types; + RwMutex mutex; +}; + // CheckerInfo stores all the symbol information for a type-checked program struct CheckerInfo { Checker *checker; @@ -364,9 +369,9 @@ struct CheckerInfo { RecursiveMutex lazy_mutex; // Mutex required for lazy type checking of specific files BlockingMutex gen_procs_mutex; - RecursiveMutex gen_types_mutex; + RwMutex gen_types_mutex; PtrMap<Ast *, GenProcsData *> gen_procs; // Key: Ast * | Identifier -> Entity - PtrMap<Type *, Array<Entity *> > gen_types; + PtrMap<Type *, GenTypesData > gen_types; BlockingMutex type_info_mutex; // NOT recursive Array<Type *> type_info_types; diff --git a/src/path.cpp b/src/path.cpp index 500a40cc2..4b426fc87 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -222,7 +222,6 @@ gb_internal i64 get_file_size(String path) { gb_internal ReadDirectoryError read_directory(String path, Array<FileInfo> *fi) {
GB_ASSERT(fi != nullptr);
- gbAllocator a = heap_allocator();
while (path.len > 0) {
Rune end = path[path.len-1];
@@ -239,9 +238,7 @@ gb_internal ReadDirectoryError read_directory(String path, Array<FileInfo> *fi) return ReadDirectory_InvalidPath;
}
{
- char *c_str = alloc_cstring(a, path);
- defer (gb_free(a, c_str));
-
+ char *c_str = alloc_cstring(temporary_allocator(), path);
gbFile f = {};
gbFileError file_err = gb_file_open(&f, c_str);
defer (gb_file_close(&f));
@@ -258,6 +255,7 @@ gb_internal ReadDirectoryError read_directory(String path, Array<FileInfo> *fi) }
+ gbAllocator a = heap_allocator();
char *new_path = gb_alloc_array(a, char, path.len+3);
defer (gb_free(a, new_path));
@@ -280,8 +278,8 @@ gb_internal ReadDirectoryError read_directory(String path, Array<FileInfo> *fi) do {
wchar_t *filename_w = file_data.cFileName;
- i64 size = cast(i64)file_data.nFileSizeLow;
- size |= (cast(i64)file_data.nFileSizeHigh) << 32;
+ u64 size = cast(u64)file_data.nFileSizeLow;
+ size |= (cast(u64)file_data.nFileSizeHigh) << 32;
String name = string16_to_string(a, make_string16_c(filename_w));
if (name == "." || name == "..") {
gb_free(a, name.text);
@@ -299,7 +297,7 @@ gb_internal ReadDirectoryError read_directory(String path, Array<FileInfo> *fi) FileInfo info = {};
info.name = name;
info.fullpath = path_to_full_path(a, filepath);
- info.size = size;
+ info.size = cast(i64)size;
info.is_dir = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
array_add(fi, info);
} while (FindNextFileW(find_file, &file_data));
|