th/threading.c

140 lines
3.1 KiB
C

#include <curses.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "defines.h"
#include "backend.h"
pthread_mutex_t mutex_top;
pthread_mutex_t mutex_btm;
pthread_mutex_t mutex_lft;
pthread_mutex_t mutex_mid;
pthread_mutex_t mutex_rgt;
/* contains entire directory as 2d array
* may be changed in future to only include parts of the dir (currently includes entire dir) */
file *rgt_content;
file *mid_content;
file *lft_content;
char *top_content; /* current path */
unsigned long rgt_file_count;
unsigned long mid_file_count;
unsigned long lft_file_count;
unsigned long top_width;
extern unsigned int status;
void *thread_mid(void *data){
pthread_mutex_lock(&mutex_mid);
free(mid_content);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
mid_content = malloc(sizeof(file));
mid_content[1].file_name_width = sizeof("cannot open directory");
mid_content[1].file_name = "cannot open directory";
mid_file_count = 1;
} else {
mid_file_count = (unsigned long)get_dir_size(path);
mid_content = malloc(mid_file_count * sizeof(file));
memset(mid_content, ' ', mid_file_count * sizeof(file));
get_dir_content(path, &mid_file_count, mid_content);
}
free(path);
pthread_mutex_unlock(&mutex_mid);
pthread_exit(NULL);
}
void *thread_lft(void *data){
pthread_mutex_lock(&mutex_lft);
free(lft_content);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
lft_content = malloc(sizeof(file));
lft_content[1].file_name_width = sizeof("cannot open directory");
lft_content[1].file_name = "cannot open directory";
lft_file_count = 1;
} else {
char *parent ;
if((parent = malloc(strlen(path)+strlen("/..")+1)) != NULL){
parent[0] = '\0'; /* ensures empty string */
strcat(parent, path);
strcat(parent, "/..");
}
lft_file_count = (unsigned long)get_dir_size(parent);
lft_content = malloc(lft_file_count * sizeof(file));
memset(lft_content, ' ', lft_file_count * sizeof(file));
get_dir_content(parent, &lft_file_count, lft_content);
free(parent);
}
free(path);
pthread_mutex_unlock(&mutex_lft);
pthread_exit(NULL);
}
void *thread_rgt(void *data){
pthread_exit(NULL);
}
void *thread_top(void *data){
pthread_mutex_lock(&mutex_top);
free(top_content);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
top_content = malloc(sizeof("cannot open directory"));
top_width = sizeof("cannot open directory");
top_content = "cannot open directory";
} else {
top_content = getcwd(NULL, 0);
top_width = strlen(top_content);
}
free(path);
pthread_mutex_unlock(&mutex_top);
pthread_exit(NULL);
}
void *thread_btm(void *data){
pthread_exit(NULL);
}
void threading_init(){
rgt_content = malloc(sizeof(char));
mid_content = malloc(sizeof(file));
lft_content = malloc(sizeof(file));
top_content = malloc(sizeof(char));
pthread_mutex_init(&mutex_top, NULL);
pthread_mutex_init(&mutex_mid, NULL);
pthread_mutex_init(&mutex_lft, NULL);
}
void threading_free(){
free(rgt_content);
free(mid_content);
free(lft_content);
free(top_content);
pthread_mutex_destroy(&mutex_top);
pthread_mutex_destroy(&mutex_mid);
pthread_mutex_destroy(&mutex_lft);
}