From d11d6c901e8dcbc1e008d3856260255056adb0cd Mon Sep 17 00:00:00 2001 From: Test_User Date: Sat, 6 May 2023 18:13:30 -0400 Subject: Add locks so it's actually thread-safe, will probably make more efficient use of them later --- main.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'main.c') diff --git a/main.c b/main.c index 76b22e6..b0f7233 100644 --- a/main.c +++ b/main.c @@ -39,16 +39,20 @@ #include "types.h" void *client_loop(void *ign) { + pthread_mutex_lock(&send_lock); while (1) { struct string full_msg = {.data = malloc(0), .len = 0}; - WRITES(1, STRING("Yay\n")); + pthread_mutex_unlock(&send_lock); client_fd = accept(client_listen_fd, NULL, NULL); + pthread_mutex_lock(&send_lock); listen(client_listen_fd, 0); client_connected = 0; client_nick.data = malloc(0); while (1) { char data[512]; + pthread_mutex_unlock(&send_lock); // TODO: proper locking, this works for now but is certainly inefficient uint64_t new_len = read(client_fd, data, 512); + pthread_mutex_lock(&send_lock); if (new_len == 0) { goto disconnect_client; @@ -210,10 +214,12 @@ int main(void) { pthread_create(&client_thread_id, NULL, client_loop, NULL); + pthread_mutex_lock(&send_lock); struct string full_msg = {malloc(0), 0}; while (1) { char data[512]; uint64_t new_len; + pthread_mutex_unlock(&send_lock); { int len; do { @@ -224,6 +230,7 @@ int main(void) { else new_len = len; } + pthread_mutex_lock(&send_lock); if (new_len == 0) { WRITES(1, STRING("Disconnected.\n")); -- cgit v1.2.3