From 9d7b9651ddcdd60072dead25e63d5ceff2dcc314 Mon Sep 17 00:00:00 2001 From: Matei Mantu <matei.mantu@stud.acs.upb.ro> Date: Wed, 29 Nov 2023 11:46:02 +0200 Subject: [PATCH] Refactored process_neighbours, looks better now --- src/.gdb_history | 4 ++-- src/os_threadpool.c | 4 ++-- src/parallel.c | 18 ++++++------------ 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/.gdb_history b/src/.gdb_history index f8384f2..4cc2b6d 100644 --- a/src/.gdb_history +++ b/src/.gdb_history @@ -14,8 +14,6 @@ r r r r -r -r b main r n @@ -254,3 +252,5 @@ r r r r +r +r diff --git a/src/os_threadpool.c b/src/os_threadpool.c index 388a4f8..1a4ea5d 100644 --- a/src/os_threadpool.c +++ b/src/os_threadpool.c @@ -71,7 +71,7 @@ os_task_t *dequeue_task(os_threadpool_t *tp) os_task_t *t; pthread_mutex_lock(&tp->queue_lock); - if (queue_is_empty(tp) && tp->active > 0) + while (queue_is_empty(tp) && tp->active > 0) pthread_cond_wait(&tp->active_cond, &tp->queue_lock); if (!queue_is_empty(tp)) { t = list_entry(tp->head.next, os_task_t, list); @@ -89,7 +89,7 @@ static void *thread_loop_function(void *arg) os_threadpool_t *tp = (os_threadpool_t *) arg; pthread_mutex_lock(&main_mutex); - if (!main_done) + while (!main_done) pthread_cond_wait(&main_cond, &main_mutex); pthread_mutex_unlock(&main_mutex); while (1) { diff --git a/src/parallel.c b/src/parallel.c index d2d3408..c4c21f8 100644 --- a/src/parallel.c +++ b/src/parallel.c @@ -25,8 +25,10 @@ void os_destroy_arg(void *arg) free(arg); } +static void process_node(unsigned int idx); + /* Define graph task argument. */ -void process_neighbours(void *arg) +static void process_neighbours(void *arg) { unsigned int idx = *(unsigned int *)arg; os_node_t *node = graph->nodes[idx]; @@ -36,17 +38,9 @@ void process_neighbours(void *arg) pthread_mutex_unlock(&sum_lock); pthread_mutex_lock(&graph_lock); - for (unsigned int i = 0; i < node->num_neighbours; ++i) { - if (graph->visited[node->neighbours[i]] == NOT_VISITED) { - graph->visited[node->neighbours[i]] = DONE; - unsigned int *new_arg = malloc(sizeof(unsigned int)); - - *new_arg = node->neighbours[i]; - os_task_t *new_task = create_task(process_neighbours, (void *)new_arg, os_destroy_arg); - - enqueue_task(tp, new_task); - } - } + for (unsigned int i = 0; i < node->num_neighbours; ++i) + if (graph->visited[node->neighbours[i]] == NOT_VISITED) + process_node(node->neighbours[i]); pthread_mutex_unlock(&graph_lock); } -- GitLab