using new thread to cancel waiting

so fixed that shit
This commit is contained in:
Václav Šmejkal 2023-04-12 08:02:11 +02:00
parent 46ea373beb
commit a4da24c4b5
Signed by: ENGO150
GPG Key ID: F6D6DF86242C5A59

View File

@ -23,7 +23,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#include <pthread.h> #include <pthread.h>
@ -332,16 +331,20 @@ void remove_json_syntax_characters(char *text)
} }
} }
void alarm_handler() void *stop_oldest_thread(void *id)
{ {
//CANCEL OLDEST THREAD sleep(WHY2_COMMUNICATION_TIME); //yk wait
pthread_cancel(waiting_head -> thread);
//REMOVE FIRST NODE pthread_t thread = *(pthread_t*) id;
waiting_node_t *buffer = waiting_head; //BUFFER
waiting_head = buffer -> next; //UNLINK if (waiting_head -> thread == thread) //THREAD IS STILL ALIVE, I HOPE
free(waiting_head); {
//KILL THE THREAD
pthread_cancel(thread);
waiting_remove_node(waiting_get_node(thread));
}
return NULL;
} }
//GLOBAL //GLOBAL
@ -407,6 +410,7 @@ void *why2_communicate_thread(void *arg)
void *raw_ptr = NULL; void *raw_ptr = NULL;
char *decoded_buffer; char *decoded_buffer;
pthread_t thread_buffer; pthread_t thread_buffer;
pthread_t thread_deletion_buffer;
why2_bool exiting = 0; why2_bool exiting = 0;
while (!exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT] while (!exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT]
@ -415,11 +419,11 @@ void *why2_communicate_thread(void *arg)
pthread_create(&thread_buffer, NULL, read_socket_raw_thread, &connection); pthread_create(&thread_buffer, NULL, read_socket_raw_thread, &connection);
waiting_push_to_list(thread_buffer); waiting_push_to_list(thread_buffer);
//SET TIMEOUT (DEFAULT IS 5 MINUTES) //RUN DELETION THREAD
signal(SIGALRM, alarm_handler); pthread_create(&thread_deletion_buffer, NULL, stop_oldest_thread, &thread_buffer);
alarm(WHY2_COMMUNICATION_TIME);
pthread_join(thread_buffer, &raw_ptr); pthread_join(thread_buffer, &raw_ptr);
waiting_remove_node(waiting_get_node(thread_buffer));
if (raw_ptr == WHY2_INVALID_POINTER || raw_ptr == NULL) break; //QUIT COMMUNICATION IF INVALID PACKET WAS RECEIVED if (raw_ptr == WHY2_INVALID_POINTER || raw_ptr == NULL) break; //QUIT COMMUNICATION IF INVALID PACKET WAS RECEIVED