diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-03-01 05:04:45 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 2af04d6ec602b2068d35d5b976f070a1b065f307 (patch) | |
tree | 97965d941453a9d437a80f6fb022378586233b3d /src/compiler.c | |
parent | cff67f93caeb3f98261860904dd232f6b551299e (diff) |
Fix compiler join thread, fix compiliation with clang
Diffstat (limited to 'src/compiler.c')
-rw-r--r-- | src/compiler.c | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/src/compiler.c b/src/compiler.c index 3e04ef2..b8bca1c 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -158,22 +158,56 @@ static CHECK_RESULT int amal_compiler_load_file_select_thread(amal_compiler *sel return result; } +static CHECK_RESULT int amal_compiler_all_threads_done(amal_compiler *self, bool *done) { + int i; + int result; + result = AMAL_COMPILER_OK; + *done = bool_false; + + cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_all_threads_done")); + for(i = 0; i < self->usable_thread_count; ++i) { + ParserThreadData *parser_thread_data; + parser_thread_data = &self->threads[i]; + if(parser_thread_data->status == PARSER_THREAD_STATUS_RUNNING) { + result = AMAL_COMPILER_ERR; + goto cleanup; + } + } + + *done = bool_true; + cleanup: + amal_mutex_tryunlock(&self->mutex); + return result; +} + static CHECK_RESULT int amal_compiler_load_file_join_threads(amal_compiler *self) { int i; - int _; int result; void *thread_return_data; ParserThreadData *parser_thread_data; result = AMAL_COMPILER_ERR; - (void)_; assert(amal_thread_is_main()); - for(i = 0; i < self->usable_thread_count; ++i) { - cleanup_if_error(amal_mutex_lock(&self->mutex, "amal_compiler_load_file_join_threads, waiting for workers")); - parser_thread_data = &self->threads[i]; - amal_mutex_tryunlock(&self->mutex); - amal_log_debug("Joining thread %d, status %d", i, parser_thread_data->status); - _ = parser_thread_data_join(parser_thread_data, &thread_return_data); + for(;;) { + bool done; + /* + Joining running threads. After checking one running thread another one might start up, + so this is mostly to wait for threads to finish and to sleep without doing work. + The check after that (amal_compiler_all_threads_done) check that all threads are done correctly + */ + for(i = 0; i < self->usable_thread_count; ++i) { + result = amal_mutex_lock(&self->mutex, "amal_compiler_load_file_join_threads, waiting for workers"); + parser_thread_data = &self->threads[i]; + amal_mutex_tryunlock(&self->mutex); + if(result != 0) + goto cleanup; + amal_log_debug("Joining thread %d, status %d", i, parser_thread_data->status); + ignore_result_int(parser_thread_data_join(parser_thread_data, &thread_return_data)); + } + + cleanup_if_error(amal_compiler_all_threads_done(self, &done)); + if(done) + break; } result = AMAL_COMPILER_OK; @@ -209,4 +243,4 @@ int amal_compiler_load_file(amal_compiler *self, BufferView filepath) { cleanup: amal_mutex_tryunlock(&self->mutex); return result; -}
\ No newline at end of file +} |