summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorTest_User <hax@andrewyu.org>2023-05-06 18:13:30 -0400
committerTest_User <hax@andrewyu.org>2023-05-06 18:13:30 -0400
commitd11d6c901e8dcbc1e008d3856260255056adb0cd (patch)
tree6f8819eb93ba2c639fc016c1b5809e31d7322717 /main.c
parentb98cc288211baa7d98a7e1ccac538f32129a061f (diff)
downloadcoupserv-d11d6c901e8dcbc1e008d3856260255056adb0cd.tar.gz
coupserv-d11d6c901e8dcbc1e008d3856260255056adb0cd.zip
Add locks so it's actually thread-safe, will probably make more efficient use of them later
Diffstat (limited to 'main.c')
-rw-r--r--main.c9
1 files changed, 8 insertions, 1 deletions
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"));