From 53571814844976749c8ed27fe6a77c6dd49e627b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 25 Feb 2018 13:45:44 +0000 Subject: [PATCH] Multithreaded parser (windows only) --- src/main.cpp | 1 + src/parser.cpp | 40 +++++++++++++++++++--------------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d9249b40e..46a1b4e70 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#define USE_THREADED_PARSER 1 // #define NO_ARRAY_BOUNDS_CHECK // #define NO_POINTER_ARITHMETIC diff --git a/src/parser.cpp b/src/parser.cpp index 123230823..1dec8bfdb 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4258,20 +4258,12 @@ ParseFileError parse_files(Parser *p, String init_filename) { array_add(&p->imports, init_imported_file); p->init_fullpath = init_fullpath; -/* + // IMPORTANT TODO(bill): Figure out why this doesn't work on *nix sometimes #if USE_THREADED_PARSER && defined(GB_SYSTEM_WINDOWS) isize thread_count = gb_max(build_context.thread_count, 1); if (thread_count > 1) { - Array worker_threads = {}; - array_init_count(&worker_threads, heap_allocator(), thread_count); - defer (array_free(&worker_threads)); - - for_array(i, p->imports) { - gbThread *t = &worker_threads[i]; - gb_thread_init(t); - } - isize curr_import_index = 0; + isize volatile curr_import_index = 0; // NOTE(bill): Make sure that these are in parsed in this order for (isize i = 0; i < shared_file_count; i++) { @@ -4282,6 +4274,18 @@ ParseFileError parse_files(Parser *p, String init_filename) { curr_import_index++; } + Array worker_threads = {}; + array_init_count(&worker_threads, heap_allocator(), thread_count); + defer (array_free(&worker_threads)); + + for_array(i, worker_threads) { + gbThread *t = &worker_threads[i]; + gb_thread_init(t); + } + defer (for_array(i, worker_threads) { + gb_thread_destroy(&worker_threads[i]); + }); + for (;;) { bool are_any_alive = false; for_array(i, worker_threads) { @@ -4291,9 +4295,6 @@ ParseFileError parse_files(Parser *p, String init_filename) { } else if (curr_import_index < p->imports.count) { auto err = cast(ParseFileError)t->return_value; if (err != ParseFile_None) { - for_array(i, worker_threads) { - gb_thread_destroy(&worker_threads[i]); - } return err; } t->user_index = curr_import_index++; @@ -4306,9 +4307,6 @@ ParseFileError parse_files(Parser *p, String init_filename) { } } - for_array(i, worker_threads) { - gb_thread_destroy(&worker_threads[i]); - } } else { for_array(i, p->imports) { ParseFileError err = parse_import(p, p->imports[i]); @@ -4317,15 +4315,15 @@ ParseFileError parse_files(Parser *p, String init_filename) { } } } -#else */ - isize import_index = 0; - for (; import_index < p->imports.count; import_index++) { - ParseFileError err = parse_import(p, p->imports[import_index]); +#else + + for_array(i, p->imports) { + ParseFileError err = parse_import(p, p->imports[i]); if (err != ParseFile_None) { return err; } } -// #endif +#endif for_array(i, p->files) { p->total_token_count += p->files[i]->tokens.count;