From a38cbb70a8b6dc22696f85bef8a8fafedc304025 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Wed, 8 Feb 2023 17:15:23 +0000 Subject: [PATCH] Fixed Thomas's Bug. In short: - When a large amount of input was recieved, the server would hang. - The server would hang on queue->lock for the input queue, in pushQueue(). - Upon debugging, it was revealed that queue->lock was actually false at this time. - GCC had optimized out the actual loop, leaving it to get stuck even though the variable had changed. - Adding the volatile keyword to the lock fixed this issue. --- Makefile | 2 +- src/queue.c | 3 +++ src/queue.h | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 7abf4ec..e1478ab 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,6 @@ clean: rm -f $(clientobj) $(serverobj) SilverMUDClient SilverMUDServer SilverMUDClientDebug SilverMUDServerDebug all: clean SilverMUDClient SilverMUDServer -all: CFLAGS += -Wall -Wextra -Ofast +all: CFLAGS += -Wall -Wextra -Ofast debug: CFLAGS += -Wall -Wextra -pg -ggdb -Og -D debug debug: clean SilverMUDClientDebug SilverMUDServerDebug diff --git a/src/queue.c b/src/queue.c index d137525..74ba8e8 100644 --- a/src/queue.c +++ b/src/queue.c @@ -157,6 +157,9 @@ void popQueue(queue * queue) // Adds data to the back of a queue: void pushQueue(queue * queue, void * data, queueDataType type) { + // Check if the queue is locked: + while (queue->lock); + // Create a new node: queueNode * newNode = malloc(sizeof(queueNode)); newNode->type = type; diff --git a/src/queue.h b/src/queue.h index c2691df..797c173 100644 --- a/src/queue.h +++ b/src/queue.h @@ -37,7 +37,7 @@ typedef struct queueNode typedef struct queue { - bool lock; + volatile bool lock; size_t itemCount; queueNode * front; queueNode * back;