From 3c0f26efeeb289c28224a81a6f195a79bde8c1d2 Mon Sep 17 00:00:00 2001 From: nova Date: Sun, 10 May 2026 19:12:39 +0200 Subject: [PATCH] attempted multithreadded rendering --- main.c | 56 +++++++++++++++++++++++++++++++++++-------------- threading.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 17 deletions(-) diff --git a/main.c b/main.c index c4e0ebf..fa701b1 100644 --- a/main.c +++ b/main.c @@ -31,6 +31,12 @@ WINDOW *win_l; WINDOW *win_m; WINDOW *win_r; +extern pthread_mutex_t mutex_top; +extern pthread_mutex_t mutex_btm; +extern pthread_mutex_t mutex_lft; +extern pthread_mutex_t mutex_mid; +extern pthread_mutex_t mutex_rgt; + char *input; /*used in user_interactions*/ char *terminal_width_empty_line; /* used in user_interactions */ @@ -154,21 +160,41 @@ void render_pass(){ } if (status & STATUS_UPDATE_SCREEN_MASK) { - werase(win_t); - werase(win_l); - werase(win_m); - werase(win_r); - werase(win_b); - window_top(win_t); - window_lft(win_l); - window_mid(win_m); - window_rgt(win_r); - window_btm(win_b); - wnoutrefresh(win_t); - wnoutrefresh(win_l); - wnoutrefresh(win_m); - wnoutrefresh(win_r); - wnoutrefresh(win_b); + if (pthread_mutex_trylock(&mutex_top) == 0) { + wnoutrefresh(win_t); + pthread_mutex_unlock(&mutex_top); + } else { + mvwaddstr(win_t, terminal_height/2, terminal_width/4, "LOADING"); + status |= STATUS_UPDATE_SCREEN_GENERIC; + } + if (pthread_mutex_trylock(&mutex_rgt) == 0) { + wnoutrefresh(win_r); + pthread_mutex_unlock(&mutex_rgt); + } else { + mvwaddstr(win_r, terminal_height/2, terminal_width/4, "LOADING"); + status |= STATUS_UPDATE_SCREEN_GENERIC; + } + if (pthread_mutex_lock(&mutex_mid) == 0) { + wnoutrefresh(win_m); + pthread_mutex_unlock(&mutex_mid); + } else { + mvwaddstr(win_m, terminal_height/2, terminal_width/4, "LOADING"); + status |= STATUS_UPDATE_SCREEN_GENERIC; + } + if (pthread_mutex_trylock(&mutex_lft) == 0) { + wnoutrefresh(win_l); + pthread_mutex_unlock(&mutex_lft); + } else { + mvwaddstr(win_l, terminal_height/2, terminal_width/4, "LOADING"); + status |= STATUS_UPDATE_SCREEN_GENERIC; + } + if (pthread_mutex_trylock(&mutex_btm) == 0) { + wnoutrefresh(win_b); + pthread_mutex_unlock(&mutex_btm); + } else { + mvwaddstr(win_b, terminal_height/2, terminal_width/4, "LOADING"); + status |= STATUS_UPDATE_SCREEN_GENERIC; + } doupdate(); status &= ~(STATUS_UPDATE_SCREEN_MASK - STATUS_UPDATE_SCREEN_DIR_CHANGE); } diff --git a/threading.c b/threading.c index 37157b3..aceeb0a 100644 --- a/threading.c +++ b/threading.c @@ -33,6 +33,11 @@ char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used f char *btm_buffer; char *top_buffer; /* current path */ +extern WINDOW *win_t; +extern WINDOW *win_b; +extern WINDOW *win_l; +extern WINDOW *win_m; +extern WINDOW *win_r; unsigned long top_width; @@ -81,12 +86,23 @@ void *thread_mid(){ mid_dir.current_file = NULL; } } + + + /* rendering */ + werase(win_m); + if (mid_dir.file_count == 0) { + mvwaddstr(win_m, 0, 0, "empty"); + } else { + print_dir(win_m, 1, &mid_dir); + } + + pthread_mutex_unlock(&mutex_mid); + pthread_cond_signal(&cond_rgt); btm_status = local_status; pthread_cond_signal(&cond_btm); - pthread_mutex_unlock(&mutex_mid); long i; for (i = 0; i < tmp.file_count; i++) { @@ -140,6 +156,10 @@ void *thread_lft(){ lft_dir.file_count = 0; } + /* rendering */ + werase(win_l); + print_dir(win_l, 0, &lft_dir); + pthread_mutex_unlock(&mutex_lft); free(path); pthread_mutex_unlock(&mutex_lft); @@ -203,6 +223,19 @@ void *thread_rgt(){ } + /* rendering */ + werase(win_r); + if (!rgt_dir.current_file) { + mvwaddstr(win_r, 0, 0, "not accessible"); + }else if (rgt_dir.current_file->file_type == FILE_TYPE_OPEN_FILE) { + mvwaddnstr(win_r, 0, 0, rgt_buffer, (terminal_width/2) * terminal_width); + } else if ((rgt_dir.current_file->permissions & S_IRUSR) == 0) { + mvwaddstr(win_r, 0, 0, "not accessible"); + } else { + print_dir(win_r, 0, &rgt_dir); + } + + pthread_mutex_unlock(&mutex_rgt); long i; for (i = 0; i < tmp.file_count; i++) { @@ -234,7 +267,20 @@ void *thread_top(){ top_buffer = malloc(strlen(global_path)+1); memcpy(top_buffer, global_path, strlen(global_path)+1); top_width = strlen(top_buffer); - /*printing of mid_dir.current_file->file_name is done directly in window.c window_top()*/ + + + /* rendering */ + werase(win_t); + if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/ + wattron(win_t, COLOR_PAIR(COLOR_PATH)); + mvwaddstr(win_t, 0, 0, top_buffer); + mvwaddch(win_t, 0, strlen(top_buffer), '/'); + wattroff(win_t, COLOR_PAIR(COLOR_PATH)); + if (mid_dir.file_count != 0 && !(mid_dir.current_file == NULL)) { + mvwaddstr(win_t, 0, strlen(top_buffer)+1, mid_dir.current_file->file_name); + } + } + pthread_mutex_unlock(&mutex_top); } @@ -347,6 +393,16 @@ void *thread_btm(){ + /* rendering */ + werase(win_b); + if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/ + mvwprintw(win_b, 0, 0, "%s", btm_buffer); + } + pthread_mutex_unlock(&mutex_btm); + /*the printing of the input char is done in user_interactions*/ + /*the printing of all possible inputs are done in user_interactions */ + + pthread_mutex_unlock(&mutex_btm); } pthread_exit(0);