From 1dfbe97b0a639c91d7727acc443ee2a9c2057920 Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Sat, 25 Jul 2020 22:12:24 +0200
Subject: Fix thread pool task count not increasing, incorrect
 ParserFileScopeReference

---
 include/std/thread_pool.h |  2 +-
 src/parser.c              |  3 ++-
 src/std/buffer.c          |  3 +--
 src/std/thread_pool.c     | 10 ++++++----
 4 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/include/std/thread_pool.h b/include/std/thread_pool.h
index f8acf83..ef73bad 100644
--- a/include/std/thread_pool.h
+++ b/include/std/thread_pool.h
@@ -33,7 +33,7 @@ typedef struct {
     amal_mutex task_select_mutex;
     Buffer queued_tasks;
     bool dead;
-    int num_finished_queued_tasks;
+    int num_taken_tasks;
 } amal_thread_pool;
 
 typedef struct {
diff --git a/src/parser.c b/src/parser.c
index d0b3c0a..e622b5f 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -1084,7 +1084,8 @@ void parser_queue_file(Parser *self, BufferView path, ParserFileScopeReference *
         BufferView import_path_canonical = create_buffer_view(file_scope->canonical_path.data, file_scope->canonical_path.size);
 
         if(hash_map_get(&self->imports_by_name, import_path_canonical, &parser_file_scope_index)) {
-            *parser_file_scope = buffer_get(&self->imports, parser_file_scope_index, sizeof(ParserFileScopeReference*));
+            ParserFileScopeReference **file_sc = buffer_get(&self->imports, parser_file_scope_index, sizeof(ParserFileScopeReference*));
+            am_memcpy(*parser_file_scope, file_sc, sizeof(ParserFileScopeReference*));
             return;
         }
 
diff --git a/src/std/buffer.c b/src/std/buffer.c
index 4ec9c29..fbfb9da 100644
--- a/src/std/buffer.c
+++ b/src/std/buffer.c
@@ -70,8 +70,7 @@ int buffer_expand(Buffer *self, usize size) {
 }
 
 void* buffer_get(Buffer *self, usize index, usize type_size) {
-    usize real_index;
-    real_index = index * type_size;
+    usize real_index = index * type_size;
     assert(real_index < self->size);
     return &self->data[real_index];
 }
diff --git a/src/std/thread_pool.c b/src/std/thread_pool.c
index 8f6e180..1e2addf 100644
--- a/src/std/thread_pool.c
+++ b/src/std/thread_pool.c
@@ -7,8 +7,10 @@
 static CHECK_RESULT int thread_pool_take_task(amal_thread_pool *self, amal_thread_pool_task **result) {
     *result = NULL;
     cleanup_if_error(amal_mutex_lock(&self->task_select_mutex, "thread_pool_take_task"));
-    if(self->num_finished_queued_tasks < (int)buffer_get_size(&self->queued_tasks, amal_thread_pool_task) && !self->dead)
-        *result = buffer_get(&self->queued_tasks, self->num_finished_queued_tasks, sizeof(amal_thread_pool_task));
+    if((self->num_taken_tasks < (int)buffer_get_size(&self->queued_tasks, amal_thread_pool_task)) && !self->dead) {
+        *result = buffer_get(&self->queued_tasks, self->num_taken_tasks, sizeof(amal_thread_pool_task));
+        self->num_taken_tasks++;
+    }
     cleanup:
     amal_mutex_tryunlock(&self->task_select_mutex);
     return 0;
@@ -80,7 +82,7 @@ int thread_pool_init(amal_thread_pool *self, int num_threads) {
     }
 
     self->dead = bool_false;
-    self->num_finished_queued_tasks = 0;
+    self->num_taken_tasks = 0;
     self->threads = NULL;
 
     ignore_result_int(buffer_init(&self->queued_tasks, NULL));
@@ -174,7 +176,7 @@ bool thread_pool_join_all_tasks(amal_thread_pool *self) {
     died = self->dead;
     self->dead = bool_false;
     buffer_clear(&self->queued_tasks);
-    self->num_finished_queued_tasks = 0;
+    self->num_taken_tasks = 0;
     return !died;
 }
 
-- 
cgit v1.2.3-70-g09d2