From b2b100727ff3f09fc48ad371e2c43eb684bee0f2 Mon Sep 17 00:00:00 2001 From: nova Date: Mon, 7 Jul 2025 19:11:48 +0200 Subject: [PATCH] fixed some race conditions --- main.c | 5 +++++ threading.c | 60 ++++++++++++++++++++++++++++------------------------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/main.c b/main.c index 662a288..54f558f 100644 --- a/main.c +++ b/main.c @@ -73,6 +73,11 @@ int main(){ pthread_cancel(thread_m); pthread_cancel(thread_r); pthread_cancel(thread_b); + pthread_join(thread_b, NULL); + pthread_join(thread_r, NULL); + pthread_join(thread_m, NULL); + pthread_join(thread_l, NULL); + pthread_join(thread_t, NULL); threading = 0; } pthread_create(&thread_t, NULL, thread_top, win_t); /*top bar*/ diff --git a/threading.c b/threading.c index a1647b0..308103a 100644 --- a/threading.c +++ b/threading.c @@ -16,6 +16,9 @@ pthread_mutex_t mutex_lft; pthread_mutex_t mutex_mid; pthread_mutex_t mutex_rgt; pthread_mutex_t mutex_selection; +pthread_mutex_t mutex_wait; +pthread_cond_t cond_wait; +int wait_count; /* this is used to determine how many threads are waiting for cont_wait */ file *rgt_content; file *mid_content; @@ -40,11 +43,9 @@ unsigned int status_mid; extern unsigned int status; extern unsigned int terminal_width; -void *thread_rgt(void *data); void *thread_mid(void *data){ pthread_mutex_lock(&mutex_mid); - status_mid = 0; char *path; @@ -81,6 +82,13 @@ void *thread_mid(void *data){ file_current->file_size = mid_content[selected_file_current].file_size; file_current->file_type = mid_content[selected_file_current].file_type; file_current->permissions = mid_content[selected_file_current].permissions; + + while(wait_count < 2){ + /*wait for thread_rgt and thread_btm to lock*/ + } + pthread_mutex_lock(&mutex_wait); + pthread_cond_broadcast(&cond_wait); + pthread_mutex_unlock(&mutex_wait); break; } } @@ -88,7 +96,6 @@ void *thread_mid(void *data){ } free(path); - status_mid=1; pthread_mutex_unlock(&mutex_mid); pthread_exit(NULL); } @@ -122,15 +129,13 @@ void *thread_lft(void *data){ } void *thread_rgt(void *data){ + pthread_mutex_lock(&mutex_wait); + wait_count++; + pthread_cond_wait(&cond_wait, &mutex_wait); + wait_count--; + pthread_mutex_unlock(&mutex_wait); pthread_mutex_lock(&mutex_rgt); - while(1) { - if (!pthread_mutex_trylock(&mutex_mid)) { - if (status_mid == 1) { - break; - } - } - } free(rgt_content); rgt_content = malloc(sizeof(file)); @@ -185,30 +190,27 @@ void *thread_top(void *data){ pthread_exit(NULL); } void *thread_btm(void *data){ + pthread_mutex_lock(&mutex_wait); + wait_count++; + pthread_cond_wait(&cond_wait, &mutex_wait); + wait_count--; + pthread_mutex_unlock(&mutex_wait); pthread_mutex_lock(&mutex_btm); - while(1) { - if (!pthread_mutex_trylock(&mutex_mid)) { - if (status_mid == 1) { - break; - } - } - } - pthread_mutex_unlock(&mutex_mid); - free(btm_buffer); int buffer_width = terminal_width; btm_buffer = malloc(buffer_width); memset(btm_buffer, 0, buffer_width); - btm_buffer[0] = (file_current->permissions & S_IRUSR) ? 'r' : '-'; - btm_buffer[1] = (file_current->permissions & S_IWUSR) ? 'w' : '-'; - btm_buffer[2] = (file_current->permissions & S_IXUSR) ? 'x' : '-'; - btm_buffer[3] = (file_current->permissions & S_IRGRP) ? 'r' : '-'; - btm_buffer[4] = (file_current->permissions & S_IWGRP) ? 'w' : '-'; - btm_buffer[5] = (file_current->permissions & S_IXGRP) ? 'x' : '-'; - btm_buffer[6] = (file_current->permissions & S_IROTH) ? 'r' : '-'; - btm_buffer[7] = (file_current->permissions & S_IWOTH) ? 'w' : '-'; - btm_buffer[8] = (file_current->permissions & S_IXOTH) ? 'x' : '-'; + btm_buffer[0] = (S_ISDIR(file_current->permissions)) ? 'd' : '-'; + btm_buffer[1] = (file_current->permissions & S_IRUSR) ? 'r' : '-'; + btm_buffer[2] = (file_current->permissions & S_IWUSR) ? 'w' : '-'; + btm_buffer[3] = (file_current->permissions & S_IXUSR) ? 'x' : '-'; + btm_buffer[4] = (file_current->permissions & S_IRGRP) ? 'r' : '-'; + btm_buffer[5] = (file_current->permissions & S_IWGRP) ? 'w' : '-'; + btm_buffer[6] = (file_current->permissions & S_IXGRP) ? 'x' : '-'; + btm_buffer[7] = (file_current->permissions & S_IROTH) ? 'r' : '-'; + btm_buffer[8] = (file_current->permissions & S_IWOTH) ? 'w' : '-'; + btm_buffer[9] = (file_current->permissions & S_IXOTH) ? 'x' : '-'; pthread_mutex_unlock(&mutex_btm); @@ -235,6 +237,8 @@ void threading_init(){ pthread_mutex_init(&mutex_mid, NULL); pthread_mutex_init(&mutex_lft, NULL); pthread_mutex_init(&mutex_selection, NULL); + pthread_mutex_init(&mutex_wait, NULL); + pthread_cond_init(&cond_wait, NULL); selected_file_current = 0; selected_file_last = 0; }