aboutsummaryrefslogtreecommitdiff
path: root/src/parser.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-12-22 12:45:23 +0000
committergingerBill <bill@gingerbill.org>2022-12-22 12:45:23 +0000
commit41b32f0da4b40295771e2a9a521b89464d98201b (patch)
tree2ec16d095e5b22ef22bfbec7d6bd38f5e878628e /src/parser.cpp
parentc53b2198a86fff66225a53f22693d9b3bf6c28e5 (diff)
Clean up mutex usage in the parser
Diffstat (limited to 'src/parser.cpp')
-rw-r--r--src/parser.cpp62
1 files changed, 30 insertions, 32 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
index 436498c51..cb9713985 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -4858,13 +4858,10 @@ gb_internal bool init_parser(Parser *p) {
GB_ASSERT(p != nullptr);
string_set_init(&p->imported_files, heap_allocator());
array_init(&p->packages, heap_allocator());
- array_init(&p->package_imports, heap_allocator());
- mutex_init(&p->wait_mutex);
- mutex_init(&p->import_mutex);
- mutex_init(&p->file_add_mutex);
+ mutex_init(&p->imported_files_mutex);
mutex_init(&p->file_decl_mutex);
mutex_init(&p->packages_mutex);
- mpmc_init(&p->file_error_queue, heap_allocator(), 1024);
+ mutex_init(&p->file_error_mutex);
return true;
}
@@ -4879,20 +4876,12 @@ gb_internal void destroy_parser(Parser *p) {
array_free(&pkg->files);
array_free(&pkg->foreign_files);
}
-#if 0
- for_array(i, p->package_imports) {
- // gb_free(heap_allocator(), p->package_imports[i].text);
- }
-#endif
array_free(&p->packages);
- array_free(&p->package_imports);
string_set_destroy(&p->imported_files);
- mutex_destroy(&p->wait_mutex);
- mutex_destroy(&p->import_mutex);
- mutex_destroy(&p->file_add_mutex);
+ mutex_destroy(&p->imported_files_mutex);
mutex_destroy(&p->file_decl_mutex);
mutex_destroy(&p->packages_mutex);
- mpmc_destroy(&p->file_error_queue);
+ mutex_destroy(&p->file_error_mutex);
}
@@ -4909,7 +4898,18 @@ gb_internal WORKER_TASK_PROC(parser_worker_proc) {
ParserWorkerData *wd = cast(ParserWorkerData *)data;
ParseFileError err = process_imported_file(wd->parser, wd->imported_file);
if (err != ParseFile_None) {
- mpmc_enqueue(&wd->parser->file_error_queue, err);
+ auto *node = gb_alloc_item(permanent_allocator(), ParseFileErrorNode);
+ node->err = err;
+
+ mutex_lock(&wd->parser->file_error_mutex);
+ if (wd->parser->file_error_tail != nullptr) {
+ wd->parser->file_error_tail->next = node;
+ }
+ wd->parser->file_error_tail = node;
+ if (wd->parser->file_error_head == nullptr) {
+ wd->parser->file_error_head = node;
+ }
+ mutex_unlock(&wd->parser->file_error_mutex);
}
return cast(isize)err;
}
@@ -4926,7 +4926,6 @@ gb_internal void parser_add_file_to_process(Parser *p, AstPackage *pkg, FileInfo
gb_internal WORKER_TASK_PROC(foreign_file_worker_proc) {
ForeignFileWorkerData *wd = cast(ForeignFileWorkerData *)data;
- Parser *p = wd->parser;
ImportedFile *imp = &wd->imported_file;
AstPackage *pkg = imp->pkg;
@@ -4946,9 +4945,9 @@ gb_internal WORKER_TASK_PROC(foreign_file_worker_proc) {
// TODO(bill): Actually do something with it
break;
}
- mutex_lock(&p->file_add_mutex);
+ mutex_lock(&pkg->foreign_files_mutex);
array_add(&pkg->foreign_files, foreign_file);
- mutex_unlock(&p->file_add_mutex);
+ mutex_unlock(&pkg->foreign_files_mutex);
return 0;
}
@@ -4968,7 +4967,7 @@ gb_internal void parser_add_foreign_file_to_process(Parser *p, AstPackage *pkg,
gb_internal AstPackage *try_add_import_path(Parser *p, String const &path, String const &rel_path, TokenPos pos, PackageKind kind = Package_Normal) {
String const FILE_EXT = str_lit(".odin");
- MUTEX_GUARD_BLOCK(&p->import_mutex) {
+ MUTEX_GUARD_BLOCK(&p->imported_files_mutex) {
if (string_set_update(&p->imported_files, path)) {
return nullptr;
}
@@ -4979,6 +4978,9 @@ gb_internal AstPackage *try_add_import_path(Parser *p, String const &path, Strin
pkg->fullpath = path;
array_init(&pkg->files, heap_allocator());
pkg->foreign_files.allocator = heap_allocator();
+ mutex_init(&pkg->files_mutex);
+ mutex_init(&pkg->foreign_files_mutex);
+
// NOTE(bill): Single file initial package
if (kind == Package_Init && string_ends_with(path, FILE_EXT)) {
@@ -5716,10 +5718,9 @@ gb_internal ParseFileError process_imported_file(Parser *p, ImportedFile importe
}
if (parse_file(p, file)) {
- mutex_lock(&p->file_add_mutex);
- defer (mutex_unlock(&p->file_add_mutex));
-
- array_add(&pkg->files, file);
+ MUTEX_GUARD_BLOCK(&pkg->files_mutex) {
+ array_add(&pkg->files, file);
+ }
if (pkg->name.len == 0) {
pkg->name = file->package_name;
@@ -5733,8 +5734,8 @@ gb_internal ParseFileError process_imported_file(Parser *p, ImportedFile importe
}
}
- p->total_line_count += file->tokenizer.line_count;
- p->total_token_count += file->tokens.count;
+ p->total_line_count.fetch_add(file->tokenizer.line_count);
+ p->total_token_count.fetch_add(file->tokens.count);
}
return ParseFile_None;
@@ -5771,9 +5772,6 @@ gb_internal ParseFileError parse_packages(Parser *p, String init_filename) {
{ // Add these packages serially and then process them parallel
- mutex_lock(&p->wait_mutex);
- defer (mutex_unlock(&p->wait_mutex));
-
TokenPos init_pos = {};
{
String s = get_fullpath_core(heap_allocator(), str_lit("runtime"));
@@ -5808,9 +5806,9 @@ gb_internal ParseFileError parse_packages(Parser *p, String init_filename) {
global_thread_pool_wait();
- for (ParseFileError err = ParseFile_None; mpmc_dequeue(&p->file_error_queue, &err); /**/) {
- if (err != ParseFile_None) {
- return err;
+ for (ParseFileErrorNode *node = p->file_error_head; node != nullptr; node = node->next) {
+ if (node->err != ParseFile_None) {
+ return node->err;
}
}