fixed some race conditions

This commit is contained in:
nova
2025-07-07 19:11:48 +02:00
parent bc2989b2d9
commit b2b100727f
2 changed files with 37 additions and 28 deletions

5
main.c
View File

@ -73,6 +73,11 @@ int main(){
pthread_cancel(thread_m);
pthread_cancel(thread_r);
pthread_cancel(thread_b);
pthread_join(thread_b, NULL);
pthread_join(thread_r, NULL);
pthread_join(thread_m, NULL);
pthread_join(thread_l, NULL);
pthread_join(thread_t, NULL);
threading = 0;
}
pthread_create(&thread_t, NULL, thread_top, win_t); /*top bar*/

View File

@ -16,6 +16,9 @@ pthread_mutex_t mutex_lft;
pthread_mutex_t mutex_mid;
pthread_mutex_t mutex_rgt;
pthread_mutex_t mutex_selection;
pthread_mutex_t mutex_wait;
pthread_cond_t cond_wait;
int wait_count; /* this is used to determine how many threads are waiting for cont_wait */
file *rgt_content;
file *mid_content;
@ -40,11 +43,9 @@ unsigned int status_mid;
extern unsigned int status;
extern unsigned int terminal_width;
void *thread_rgt(void *data);
void *thread_mid(void *data){
pthread_mutex_lock(&mutex_mid);
status_mid = 0;
char *path;
@ -81,6 +82,13 @@ void *thread_mid(void *data){
file_current->file_size = mid_content[selected_file_current].file_size;
file_current->file_type = mid_content[selected_file_current].file_type;
file_current->permissions = mid_content[selected_file_current].permissions;
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);
break;
}
}
@ -88,7 +96,6 @@ void *thread_mid(void *data){
}
free(path);
status_mid=1;
pthread_mutex_unlock(&mutex_mid);
pthread_exit(NULL);
}
@ -122,15 +129,13 @@ void *thread_lft(void *data){
}
void *thread_rgt(void *data){
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_rgt);
while(1) {
if (!pthread_mutex_trylock(&mutex_mid)) {
if (status_mid == 1) {
break;
}
}
}
free(rgt_content);
rgt_content = malloc(sizeof(file));
@ -185,30 +190,27 @@ void *thread_top(void *data){
pthread_exit(NULL);
}
void *thread_btm(void *data){
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_btm);
while(1) {
if (!pthread_mutex_trylock(&mutex_mid)) {
if (status_mid == 1) {
break;
}
}
}
pthread_mutex_unlock(&mutex_mid);
free(btm_buffer);
int buffer_width = terminal_width;
btm_buffer = malloc(buffer_width);
memset(btm_buffer, 0, buffer_width);
btm_buffer[0] = (file_current->permissions & S_IRUSR) ? 'r' : '-';
btm_buffer[1] = (file_current->permissions & S_IWUSR) ? 'w' : '-';
btm_buffer[2] = (file_current->permissions & S_IXUSR) ? 'x' : '-';
btm_buffer[3] = (file_current->permissions & S_IRGRP) ? 'r' : '-';
btm_buffer[4] = (file_current->permissions & S_IWGRP) ? 'w' : '-';
btm_buffer[5] = (file_current->permissions & S_IXGRP) ? 'x' : '-';
btm_buffer[6] = (file_current->permissions & S_IROTH) ? 'r' : '-';
btm_buffer[7] = (file_current->permissions & S_IWOTH) ? 'w' : '-';
btm_buffer[8] = (file_current->permissions & S_IXOTH) ? 'x' : '-';
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);
@ -235,6 +237,8 @@ void threading_init(){
pthread_mutex_init(&mutex_mid, NULL);
pthread_mutex_init(&mutex_lft, NULL);
pthread_mutex_init(&mutex_selection, NULL);
pthread_mutex_init(&mutex_wait, NULL);
pthread_cond_init(&cond_wait, NULL);
selected_file_current = 0;
selected_file_last = 0;
}