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:
27
main.c
27
main.c
@@ -59,6 +59,11 @@ int main(){
|
||||
char threading = 0;
|
||||
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)){
|
||||
getmaxyx(stdscr, terminal_height, terminal_width);
|
||||
@@ -68,28 +73,16 @@ int main(){
|
||||
temp_width = terminal_width;
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
if (status & STATUS_RUN_BACKEND) {
|
||||
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_b, NULL, thread_btm, &status); /*bottom bar*/
|
||||
pthread_create(&thread_r, NULL, thread_rgt, &status); /*child_content slash win_r*/
|
||||
pthread_cond_signal(&cond_top);
|
||||
pthread_cond_signal(&cond_lft);
|
||||
pthread_cond_signal(&cond_mid);
|
||||
pthread_cond_signal(&cond_rgt);
|
||||
pthread_cond_signal(&cond_btm);
|
||||
status &= ~(STATUS_RUN_BACKEND);
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
threading = 1;
|
||||
|
366
threading.c
366
threading.c
@@ -20,6 +20,12 @@ pthread_mutex_t mutex_rgt;
|
||||
pthread_mutex_t mutex_selection;
|
||||
pthread_mutex_t mutex_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 */
|
||||
|
||||
file *rgt_content;
|
||||
@@ -42,219 +48,238 @@ unsigned long top_width;
|
||||
volatile unsigned long selected_file_current = 0;
|
||||
volatile unsigned long selected_file_last = 0;
|
||||
extern unsigned int terminal_width;
|
||||
extern unsigned int status;
|
||||
|
||||
|
||||
void *thread_mid(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
unsigned int local_status = *(unsigned int*)data;
|
||||
|
||||
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((path=getcwd(NULL, 0)) == NULL) {
|
||||
mid_content = malloc(sizeof(file));
|
||||
mid_content->file_name = "cannot open directory";
|
||||
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*/
|
||||
if (local_status & STATUS_RELOAD_DIRECTORY) {
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < mid_file_count; i++) {
|
||||
free(mid_content[i].file_name);
|
||||
}
|
||||
pthread_mutex_lock(&mutex_wait);
|
||||
pthread_cond_broadcast(&cond_wait);
|
||||
pthread_mutex_unlock(&mutex_wait);
|
||||
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";
|
||||
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
pthread_exit(0);
|
||||
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);
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
pthread_exit(0);
|
||||
}
|
||||
void *thread_lft(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
pthread_mutex_lock(&mutex_lft);
|
||||
unsigned int local_status = *(unsigned int*)data;
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
lft_content = malloc(sizeof(file));
|
||||
lft_content[0].file_name = "cannot open directory";
|
||||
lft_file_count = 1;
|
||||
} else {
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_lft);
|
||||
pthread_cond_wait(&cond_lft, &mutex_lft);
|
||||
|
||||
path[strrchr(path, '/')-path] = '\0';
|
||||
path[0] = '/';
|
||||
char *path;
|
||||
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);
|
||||
|
||||
|
||||
}
|
||||
void *thread_rgt(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
(void)status;
|
||||
unsigned int local_status = *(unsigned int*)data;
|
||||
(void)local_status;
|
||||
|
||||
pthread_mutex_lock(&mutex_rgt);
|
||||
pthread_mutex_lock(&mutex_wait);
|
||||
wait_count++;
|
||||
pthread_cond_wait(&cond_wait, &mutex_wait);
|
||||
wait_count--;
|
||||
pthread_mutex_unlock(&mutex_wait);
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_rgt);
|
||||
pthread_cond_wait(&cond_rgt, &mutex_rgt);
|
||||
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_selection);
|
||||
char *path = malloc(strlen(file_current->file_name) + 1);
|
||||
strcpy(path, file_current->file_name);
|
||||
unsigned char file_current_type = file_current->file_type;
|
||||
unsigned long file_current_size = file_current->file_size;
|
||||
char file_current_status = file_current->status;
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
char *path = malloc(strlen(file_current->file_name) + 1);
|
||||
strcpy(path, file_current->file_name);
|
||||
unsigned char file_current_type = file_current->file_type;
|
||||
unsigned long file_current_size = file_current->file_size;
|
||||
char file_current_local_status = file_current->status;
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
|
||||
if (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) {
|
||||
images_clear();
|
||||
if (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) {
|
||||
images_clear();
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < rgt_file_count; i++) {
|
||||
free(rgt_content[i].file_name);
|
||||
}
|
||||
free(rgt_content);
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < rgt_file_count; i++) {
|
||||
free(rgt_content[i].file_name);
|
||||
}
|
||||
free(rgt_content);
|
||||
|
||||
rgt_file_count = get_dir_size(path);
|
||||
rgt_content = malloc(rgt_file_count * sizeof(file));
|
||||
memset(rgt_content, '\0', rgt_file_count * sizeof(file));
|
||||
get_dir_content(path, &rgt_file_count, rgt_content);
|
||||
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
|
||||
rgt_file_count = get_dir_size(path);
|
||||
rgt_content = malloc(rgt_file_count * sizeof(file));
|
||||
memset(rgt_content, '\0', rgt_file_count * sizeof(file));
|
||||
get_dir_content(path, &rgt_file_count, rgt_content);
|
||||
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
|
||||
|
||||
free(rgt_buffer);
|
||||
rgt_buffer = malloc(sizeof(char));
|
||||
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";
|
||||
free(rgt_buffer);
|
||||
rgt_buffer = malloc(sizeof(char));
|
||||
rgt_buffer[0] = '\0';
|
||||
} 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);
|
||||
}
|
||||
void *thread_top(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
(void)status;
|
||||
unsigned int local_status = *(unsigned int*)data;
|
||||
(void)local_status;
|
||||
|
||||
pthread_mutex_lock(&mutex_top);
|
||||
free(top_buffer);
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_top);
|
||||
pthread_cond_wait(&cond_top, &mutex_top);
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
top_buffer = malloc(sizeof("cannot open directory"));
|
||||
top_width = sizeof("cannot open directory");
|
||||
top_buffer = "cannot open directory";
|
||||
} else {
|
||||
top_buffer = getcwd(NULL, 0);
|
||||
top_width = strlen(top_buffer);
|
||||
free(top_buffer);
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
top_buffer = malloc(sizeof("cannot open directory"));
|
||||
top_width = sizeof("cannot open directory");
|
||||
top_buffer = "cannot open directory";
|
||||
} 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);
|
||||
}
|
||||
void *thread_btm(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
(void)status;
|
||||
unsigned int local_status = *(unsigned int*)data;
|
||||
(void)local_status;
|
||||
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
pthread_mutex_lock(&mutex_wait);
|
||||
wait_count++;
|
||||
pthread_cond_wait(&cond_wait, &mutex_wait);
|
||||
wait_count--;
|
||||
pthread_mutex_unlock(&mutex_wait);
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
pthread_cond_wait(&cond_btm, &mutex_btm);
|
||||
|
||||
free(btm_buffer);
|
||||
int buffer_width = terminal_width;
|
||||
btm_buffer = malloc(buffer_width);
|
||||
memset(btm_buffer, 0, buffer_width);
|
||||
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_lock(&mutex_wait);
|
||||
wait_count++;
|
||||
pthread_cond_wait(&cond_wait, &mutex_wait);
|
||||
wait_count--;
|
||||
pthread_mutex_unlock(&mutex_wait);
|
||||
|
||||
free(btm_buffer);
|
||||
int buffer_width = terminal_width;
|
||||
btm_buffer = malloc(buffer_width);
|
||||
memset(btm_buffer, 0, buffer_width);
|
||||
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);
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
}
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
@@ -295,6 +320,11 @@ void threading_init(){
|
||||
vol = pthread_mutex_init(&mutex_selection, NULL);
|
||||
vol = pthread_mutex_init(&mutex_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;
|
||||
selected_file_current = 0;
|
||||
selected_file_last = 0;
|
||||
|
Reference in New Issue
Block a user