1
0
mirror of https://gittea.dev/nova/th.git synced 2025-10-21 10:20:15 -04:00

removed the recreation and destruction of threads

This commit is contained in:
nova
2025-08-23 21:44:03 +02:00
parent a0a102e5f6
commit 963a018419
2 changed files with 208 additions and 185 deletions

27
main.c
View File

@@ -59,6 +59,11 @@ int main(){
char threading = 0; char threading = 0;
terminal_width_empty_line = malloc(terminal_width); terminal_width_empty_line = malloc(terminal_width);
pthread_create(&thread_t, NULL, thread_top, &status); /*top bar*/
pthread_create(&thread_l, NULL, thread_lft, &status); /*parent_content slash win_l*/
pthread_create(&thread_m, NULL, thread_mid, &status); /*current_content slash win_m*/
pthread_create(&thread_r, NULL, thread_rgt, &status); /*child_content slash win_r*/
pthread_create(&thread_b, NULL, thread_btm, &status); /*bottom bar*/
while(!(status & STATUS_QUIT_PROGRAM)){ while(!(status & STATUS_QUIT_PROGRAM)){
getmaxyx(stdscr, terminal_height, terminal_width); getmaxyx(stdscr, terminal_height, terminal_width);
@@ -68,28 +73,16 @@ int main(){
temp_width = terminal_width; temp_width = terminal_width;
temp_heigth = terminal_height; temp_heigth = terminal_height;
} }
if (status & STATUS_RUN_BACKEND && threading) {
pthread_cancel(thread_b);
pthread_cancel(thread_r);
pthread_cancel(thread_m);
pthread_cancel(thread_l);
pthread_cancel(thread_t);
}
if (threading) { if (threading) {
status &= ~(STATUS_RELOAD_DIRECTORY); status &= ~(STATUS_RELOAD_DIRECTORY);
pthread_join(thread_t, NULL);
pthread_join(thread_l, NULL);
pthread_join(thread_m, NULL);
pthread_join(thread_b, NULL);
pthread_join(thread_r, NULL);
threading = 0; threading = 0;
} }
if (status & STATUS_RUN_BACKEND) { if (status & STATUS_RUN_BACKEND) {
pthread_create(&thread_t, NULL, thread_top, &status); /*top bar*/ pthread_cond_signal(&cond_top);
pthread_create(&thread_l, NULL, thread_lft, &status); /*parent_content slash win_l*/ pthread_cond_signal(&cond_lft);
pthread_create(&thread_m, NULL, thread_mid, &status); /*current_content slash win_m*/ pthread_cond_signal(&cond_mid);
pthread_create(&thread_b, NULL, thread_btm, &status); /*bottom bar*/ pthread_cond_signal(&cond_rgt);
pthread_create(&thread_r, NULL, thread_rgt, &status); /*child_content slash win_r*/ pthread_cond_signal(&cond_btm);
status &= ~(STATUS_RUN_BACKEND); status &= ~(STATUS_RUN_BACKEND);
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
threading = 1; threading = 1;

View File

@@ -20,6 +20,12 @@ pthread_mutex_t mutex_rgt;
pthread_mutex_t mutex_selection; pthread_mutex_t mutex_selection;
pthread_mutex_t mutex_wait; pthread_mutex_t mutex_wait;
pthread_cond_t cond_wait; pthread_cond_t cond_wait;
pthread_cond_t cond_mid;
pthread_cond_t cond_rgt;
pthread_cond_t cond_lft;
pthread_cond_t cond_top;
pthread_cond_t cond_btm;
volatile char wait_count; /* this is used to determine how many threads are waiting for cont_wait */ volatile char wait_count; /* this is used to determine how many threads are waiting for cont_wait */
file *rgt_content; file *rgt_content;
@@ -42,219 +48,238 @@ unsigned long top_width;
volatile unsigned long selected_file_current = 0; volatile unsigned long selected_file_current = 0;
volatile unsigned long selected_file_last = 0; volatile unsigned long selected_file_last = 0;
extern unsigned int terminal_width; extern unsigned int terminal_width;
extern unsigned int status;
void *thread_mid(void *data){ void *thread_mid(void *data){
unsigned int status = *(unsigned int*)data; unsigned int local_status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_mid);
while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_mid);
pthread_cond_wait(&cond_mid, &mutex_mid);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
mid_content = malloc(sizeof(file));
mid_content->file_name = "cannot open directory";
mid_file_count = 1;
} else {
char *path; if (local_status & STATUS_RELOAD_DIRECTORY) {
if((path=getcwd(NULL, 0)) == NULL) { unsigned long i = 0;
mid_content = malloc(sizeof(file)); for (i = 0; i < mid_file_count; i++) {
mid_content->file_name = "cannot open directory"; free(mid_content[i].file_name);
mid_file_count = 1;
} else {
if (status & STATUS_RELOAD_DIRECTORY) {
unsigned long i = 0;
for (i = 0; i < mid_file_count; i++) {
free(mid_content[i].file_name);
}
free(mid_content);
mid_file_count = get_dir_size(path);
if (mid_file_count > 0) {
mid_content = malloc(mid_file_count * sizeof(file));
memset(mid_content, '\0', mid_file_count * sizeof(file));
get_dir_content(path, &mid_file_count, mid_content);
} else {
selected_file_current = 0;
mid_content = malloc(sizeof(file));
mid_content->status = FILE_STATUS_DIR_EMPTY;
mid_content->file_type = 0;
mid_content->file_size = 0;
mid_content->permissions = 0;
mid_content->color_pair = 0;
mid_content->file_name = "empty dir";
file_current->file_name = mid_content->file_name;
file_current->file_size = mid_content->file_size;
file_current->file_type = mid_content->file_type;
file_current->color_pair = mid_content->color_pair;
file_current->permissions = mid_content->permissions;
file_current->status = mid_content->status;
mid_file_count = 0;
while(wait_count < 2){
/*wait for thread_rgt and thread_btm to lock*/
} }
pthread_mutex_lock(&mutex_wait); free(mid_content);
pthread_cond_broadcast(&cond_wait); mid_file_count = get_dir_size(path);
pthread_mutex_unlock(&mutex_wait); if (mid_file_count > 0) {
mid_content = malloc(mid_file_count * sizeof(file));
memset(mid_content, '\0', mid_file_count * sizeof(file));
get_dir_content(path, &mid_file_count, mid_content);
} else {
selected_file_current = 0;
mid_content = malloc(sizeof(file));
mid_content->status = FILE_STATUS_DIR_EMPTY;
mid_content->file_type = 0;
mid_content->file_size = 0;
mid_content->permissions = 0;
mid_content->color_pair = 0;
mid_content->file_name = "empty dir";
free(path); file_current->file_name = mid_content->file_name;
pthread_mutex_unlock(&mutex_mid); file_current->file_size = mid_content->file_size;
pthread_exit(0); file_current->file_type = mid_content->file_type;
file_current->color_pair = mid_content->color_pair;
file_current->permissions = mid_content->permissions;
file_current->status = mid_content->status;
mid_file_count = 0;
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);
free(path);
pthread_mutex_unlock(&mutex_mid);
pthread_exit(0);
}
pthread_mutex_lock(&mutex_selection);
update_selected_file();
pthread_mutex_unlock(&mutex_selection);
} }
pthread_mutex_lock(&mutex_selection);
update_selected_file();
pthread_mutex_unlock(&mutex_selection); 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);
} }
free(path);
pthread_mutex_unlock(&mutex_mid);
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);
} }
free(path);
pthread_mutex_unlock(&mutex_mid);
pthread_exit(0); pthread_exit(0);
} }
void *thread_lft(void *data){ void *thread_lft(void *data){
unsigned int status = *(unsigned int*)data; unsigned int local_status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_lft);
char *path; while(!(status & STATUS_QUIT_PROGRAM)){
if((path=getcwd(NULL, 0)) == NULL) { pthread_mutex_lock(&mutex_lft);
lft_content = malloc(sizeof(file)); pthread_cond_wait(&cond_lft, &mutex_lft);
lft_content[0].file_name = "cannot open directory";
lft_file_count = 1;
} else {
path[strrchr(path, '/')-path] = '\0'; char *path;
path[0] = '/'; if((path=getcwd(NULL, 0)) == NULL) {
lft_content = malloc(sizeof(file));
lft_content[0].file_name = "cannot open directory";
lft_file_count = 1;
} else {
path[strrchr(path, '/')-path] = '\0';
path[0] = '/';
if (local_status & STATUS_RELOAD_DIRECTORY) {
lft_file_count = get_dir_size(path);
free(lft_content);
lft_content = malloc(lft_file_count * sizeof(file));
memset(lft_content, '\0', lft_file_count * sizeof(file));
get_dir_content(path, &lft_file_count, lft_content);
}
if (status & STATUS_RELOAD_DIRECTORY) {
lft_file_count = get_dir_size(path);
free(lft_content);
lft_content = malloc(lft_file_count * sizeof(file));
memset(lft_content, '\0', lft_file_count * sizeof(file));
get_dir_content(path, &lft_file_count, lft_content);
} }
free(path);
pthread_mutex_unlock(&mutex_lft);
} }
free(path);
pthread_mutex_unlock(&mutex_lft);
pthread_exit(0); pthread_exit(0);
} }
void *thread_rgt(void *data){ void *thread_rgt(void *data){
unsigned int status = *(unsigned int*)data; unsigned int local_status = *(unsigned int*)data;
(void)status; (void)local_status;
pthread_mutex_lock(&mutex_rgt); while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_wait); pthread_mutex_lock(&mutex_rgt);
wait_count++; pthread_cond_wait(&cond_rgt, &mutex_rgt);
pthread_cond_wait(&cond_wait, &mutex_wait); pthread_mutex_lock(&mutex_wait);
wait_count--; wait_count++;
pthread_mutex_unlock(&mutex_wait); pthread_cond_wait(&cond_wait, &mutex_wait);
wait_count--;
pthread_mutex_unlock(&mutex_wait);
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
char *path = malloc(strlen(file_current->file_name) + 1); char *path = malloc(strlen(file_current->file_name) + 1);
strcpy(path, file_current->file_name); strcpy(path, file_current->file_name);
unsigned char file_current_type = file_current->file_type; unsigned char file_current_type = file_current->file_type;
unsigned long file_current_size = file_current->file_size; unsigned long file_current_size = file_current->file_size;
char file_current_status = file_current->status; char file_current_local_status = file_current->status;
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
if (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) { if (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) {
images_clear(); images_clear();
unsigned long i = 0; unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) { for (i = 0; i < rgt_file_count; i++) {
free(rgt_content[i].file_name); free(rgt_content[i].file_name);
} }
free(rgt_content); free(rgt_content);
rgt_file_count = get_dir_size(path); rgt_file_count = get_dir_size(path);
rgt_content = malloc(rgt_file_count * sizeof(file)); rgt_content = malloc(rgt_file_count * sizeof(file));
memset(rgt_content, '\0', rgt_file_count * sizeof(file)); memset(rgt_content, '\0', rgt_file_count * sizeof(file));
get_dir_content(path, &rgt_file_count, rgt_content); get_dir_content(path, &rgt_file_count, rgt_content);
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN; rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
free(rgt_buffer); free(rgt_buffer);
rgt_buffer = malloc(sizeof(char)); rgt_buffer = malloc(sizeof(char));
rgt_buffer[0] = '\0'; rgt_buffer[0] = '\0';
} else {
unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) {
free(rgt_content[i].file_name);
}
free(rgt_content);
rgt_file_count = 0;
rgt_content = malloc(sizeof(file));
free(rgt_buffer);
if (file_current_status & FILE_STATUS_DIR_EMPTY) {
rgt_buffer = "empty dir";
} else { } else {
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
rgt_content->status = FILE_STATUS_HOVER;
rgt_buffer = preview_file(path, file_current_size);
}
}
free(path);
pthread_mutex_unlock(&mutex_rgt); unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) {
free(rgt_content[i].file_name);
}
free(rgt_content);
rgt_file_count = 0;
rgt_content = malloc(sizeof(file));
free(rgt_buffer);
if (file_current_local_status & FILE_STATUS_DIR_EMPTY) {
rgt_buffer = "empty dir";
} else {
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
rgt_content->status = FILE_STATUS_HOVER;
rgt_buffer = preview_file(path, file_current_size);
}
}
free(path);
pthread_mutex_unlock(&mutex_rgt);
}
pthread_exit(0); pthread_exit(0);
} }
void *thread_top(void *data){ void *thread_top(void *data){
unsigned int status = *(unsigned int*)data; unsigned int local_status = *(unsigned int*)data;
(void)status; (void)local_status;
pthread_mutex_lock(&mutex_top); while(!(status & STATUS_QUIT_PROGRAM)){
free(top_buffer); pthread_mutex_lock(&mutex_top);
pthread_cond_wait(&cond_top, &mutex_top);
char *path; free(top_buffer);
if((path=getcwd(NULL, 0)) == NULL) {
top_buffer = malloc(sizeof("cannot open directory")); char *path;
top_width = sizeof("cannot open directory"); if((path=getcwd(NULL, 0)) == NULL) {
top_buffer = "cannot open directory"; top_buffer = malloc(sizeof("cannot open directory"));
} else { top_width = sizeof("cannot open directory");
top_buffer = getcwd(NULL, 0); top_buffer = "cannot open directory";
top_width = strlen(top_buffer); } else {
top_buffer = getcwd(NULL, 0);
top_width = strlen(top_buffer);
}
free(path);
pthread_mutex_unlock(&mutex_top);
} }
free(path);
pthread_mutex_unlock(&mutex_top);
pthread_exit(0); pthread_exit(0);
} }
void *thread_btm(void *data){ void *thread_btm(void *data){
unsigned int status = *(unsigned int*)data; unsigned int local_status = *(unsigned int*)data;
(void)status; (void)local_status;
pthread_mutex_lock(&mutex_btm); while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_wait); pthread_mutex_lock(&mutex_btm);
wait_count++; pthread_cond_wait(&cond_btm, &mutex_btm);
pthread_cond_wait(&cond_wait, &mutex_wait);
wait_count--;
pthread_mutex_unlock(&mutex_wait);
free(btm_buffer); pthread_mutex_lock(&mutex_wait);
int buffer_width = terminal_width; wait_count++;
btm_buffer = malloc(buffer_width); pthread_cond_wait(&cond_wait, &mutex_wait);
memset(btm_buffer, 0, buffer_width); wait_count--;
btm_buffer[0] = (S_ISDIR(file_current->permissions)) ? 'd' : '-'; pthread_mutex_unlock(&mutex_wait);
btm_buffer[1] = (file_current->permissions & S_IRUSR) ? 'r' : '-';
btm_buffer[2] = (file_current->permissions & S_IWUSR) ? 'w' : '-'; free(btm_buffer);
btm_buffer[3] = (file_current->permissions & S_IXUSR) ? 'x' : '-'; int buffer_width = terminal_width;
btm_buffer[4] = (file_current->permissions & S_IRGRP) ? 'r' : '-'; btm_buffer = malloc(buffer_width);
btm_buffer[5] = (file_current->permissions & S_IWGRP) ? 'w' : '-'; memset(btm_buffer, 0, buffer_width);
btm_buffer[6] = (file_current->permissions & S_IXGRP) ? 'x' : '-'; btm_buffer[0] = (S_ISDIR(file_current->permissions)) ? 'd' : '-';
btm_buffer[7] = (file_current->permissions & S_IROTH) ? 'r' : '-'; btm_buffer[1] = (file_current->permissions & S_IRUSR) ? 'r' : '-';
btm_buffer[8] = (file_current->permissions & S_IWOTH) ? 'w' : '-'; btm_buffer[2] = (file_current->permissions & S_IWUSR) ? 'w' : '-';
btm_buffer[9] = (file_current->permissions & S_IXOTH) ? 'x' : '-'; 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); pthread_mutex_unlock(&mutex_btm);
}
pthread_exit(0); pthread_exit(0);
} }
@@ -295,6 +320,11 @@ void threading_init(){
vol = pthread_mutex_init(&mutex_selection, NULL); vol = pthread_mutex_init(&mutex_selection, NULL);
vol = pthread_mutex_init(&mutex_wait, NULL); vol = pthread_mutex_init(&mutex_wait, NULL);
vol = pthread_cond_init(&cond_wait, NULL); vol = pthread_cond_init(&cond_wait, NULL);
vol = pthread_cond_init(&cond_rgt, NULL);
vol = pthread_cond_init(&cond_lft, NULL);
vol = pthread_cond_init(&cond_mid, NULL);
vol = pthread_cond_init(&cond_top, NULL);
vol = pthread_cond_init(&cond_btm, NULL);
vol; vol;
selected_file_current = 0; selected_file_current = 0;
selected_file_last = 0; selected_file_last = 0;