now each thread can queue its own rendering

This commit is contained in:
nova
2026-05-10 21:44:02 +02:00
parent 58f99eb2cf
commit 689565f732
5 changed files with 86 additions and 69 deletions

79
main.c
View File

@@ -36,6 +36,7 @@ extern pthread_mutex_t mutex_btm;
extern pthread_mutex_t mutex_lft;
extern pthread_mutex_t mutex_mid;
extern pthread_mutex_t mutex_rgt;
extern volatile char render_queue[5];
char *input; /*used in user_interactions*/
char *terminal_width_empty_line; /* used in user_interactions */
@@ -141,6 +142,7 @@ void render_pass(){
if (status & STATUS_UPDATE_SCREEN_CLEAR) {
clear();
status &= ~STATUS_UPDATE_SCREEN_CLEAR;
}
if (status & STATUS_UPDATE_SCREEN_RESIZE) {
@@ -158,48 +160,43 @@ void render_pass(){
mvwin(win_b, terminal_height-1, 0);
}
if (status & STATUS_UPDATE_SCREEN_MASK) {
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);
/* render_queue is modified both by this and its associated thread without mutexes, this is intentional.
* reason being that since rendering is done in the same thread as the logic,
* using mutexes actually slows this down more than the result of an unknown state.
* an unknown state may be an incomplete render, which in my tests wont crash, or one too many renders.
* both of these chances are non distructive.
* doing rendering async is done because
* A: its funny
* B: previous render pipelines may have caused win_r to lack behind
* C: slowdown as a result of too many renders */
if (render_queue[RENDER_QUEUE_TOP] || status & (STATUS_UPDATE_SCREEN_RESIZE | STATUS_UPDATE_SCREEN_CLEAR)) {
wnoutrefresh(win_t);
render_queue[RENDER_QUEUE_TOP] = 0;
status |= STATUS_UPDATE_ASYNC_REFRESH;
}
if (status & STATUS_UPDATE_SCREEN_DIR_CHANGE) {
status &= ~STATUS_UPDATE_SCREEN_DIR_CHANGE;
status |= STATUS_UPDATE_SCREEN_GENERIC;
if (render_queue[RENDER_QUEUE_LFT] || status & (STATUS_UPDATE_SCREEN_RESIZE | STATUS_UPDATE_SCREEN_CLEAR)) {
wnoutrefresh(win_l);
render_queue[RENDER_QUEUE_LFT] = 0;
status |= STATUS_UPDATE_ASYNC_REFRESH;
}
if (render_queue[RENDER_QUEUE_MID] || status & (STATUS_UPDATE_SCREEN_RESIZE | STATUS_UPDATE_SCREEN_CLEAR)) {
wnoutrefresh(win_m);
render_queue[RENDER_QUEUE_MID] = 0;
status |= STATUS_UPDATE_ASYNC_REFRESH;
}
if (render_queue[RENDER_QUEUE_RGT] || status & (STATUS_UPDATE_SCREEN_RESIZE | STATUS_UPDATE_SCREEN_CLEAR)) {
wnoutrefresh(win_r);
render_queue[RENDER_QUEUE_RGT] = 0;
status |= STATUS_UPDATE_ASYNC_REFRESH;
}
if (render_queue[RENDER_QUEUE_BTM] || status & (STATUS_UPDATE_SCREEN_RESIZE | STATUS_UPDATE_SCREEN_CLEAR)) {
wnoutrefresh(win_b);
render_queue[RENDER_QUEUE_BTM] = 0;
status |= STATUS_UPDATE_ASYNC_REFRESH;
}
if (status & STATUS_UPDATE_SCREEN_MASK) {
doupdate();
status &= ~STATUS_UPDATE_SCREEN_MASK;
}
}
/*this function exists for things done at startup (initialization, reading config, etc)*/