| | | 1 | | /* This file is a template for queue.c. Content will be filled by yagiz on 2025-12-29. */ |
| | | 2 | | #include <stdio.h> |
| | | 3 | | #include <stdlib.h> |
| | | 4 | | #include "queue.h" |
| | | 5 | | |
| | | 6 | | // Internal Helper for Mutex |
| | 9 | 7 | | static void lockMutex(Mutex *m) { |
| | | 8 | | #if defined(_WIN32) || defined(_WIN64) |
| | | 9 | | EnterCriticalSection(m); |
| | | 10 | | #else |
| | 9 | 11 | | pthread_mutex_lock(m); |
| | | 12 | | #endif |
| | 9 | 13 | | } |
| | | 14 | | |
| | 9 | 15 | | static void unlockMutex(Mutex *m) { |
| | | 16 | | #if defined(_WIN32) || defined(_WIN64) |
| | | 17 | | LeaveCriticalSection(m); |
| | | 18 | | #else |
| | 9 | 19 | | pthread_mutex_unlock(m); |
| | | 20 | | #endif |
| | 9 | 21 | | } |
| | | 22 | | |
| | 2 | 23 | | static void initMutex(Mutex *m) { |
| | | 24 | | #if defined(_WIN32) || defined(_WIN64) |
| | | 25 | | InitializeCriticalSection(m); |
| | | 26 | | #else |
| | 2 | 27 | | pthread_mutex_init(m, NULL); |
| | | 28 | | #endif |
| | 2 | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | static void destroyMutex(Mutex *m) { |
| | | 32 | | #if defined(_WIN32) || defined(_WIN64) |
| | | 33 | | DeleteCriticalSection(m); |
| | | 34 | | #else |
| | 0 | 35 | | pthread_mutex_destroy(m); |
| | | 36 | | #endif |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | // Function to initialize the queue |
| | 2 | 40 | | void initQueue(Queue *q) { |
| | 2 | 41 | | q->head = 0; |
| | 2 | 42 | | q->tail = 0; |
| | 2 | 43 | | q->count = 0; |
| | | 44 | | // Initialize standard mutex |
| | 2 | 45 | | initMutex(&q->lock); |
| | 2 | 46 | | } |
| | | 47 | | |
| | | 48 | | // Helper to destroy queue resources |
| | 0 | 49 | | void destroyQueue(Queue *q) { |
| | 0 | 50 | | destroyMutex(&q->lock); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | // Is Queue empty? (Not thread-safe on its own, usually called within lock or for checks) |
| | | 54 | | // But for strict safety, we can lock it too. |
| | 3 | 55 | | bool isQueueEmpty(Queue *q) { |
| | 3 | 56 | | return (q->count == 0); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // Is Queue full? |
| | 2 | 60 | | bool isQueueFull(Queue *q) { |
| | 2 | 61 | | return (q->count == MAX_QUEUE_SIZE); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | // Enqueue element (Thread-safe) |
| | 2 | 65 | | bool enqueue(Queue *q, SensorData data) { |
| | 2 | 66 | | lockMutex(&q->lock); |
| | | 67 | | |
| | 2 | 68 | | if (isQueueFull(q)) { |
| | 0 | 69 | | printf("ERROR: Queue is full! Data lost: %.2f\n", data.value); |
| | 0 | 70 | | unlockMutex(&q->lock); |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | // Write data to tail |
| | 2 | 75 | | q->items[q->tail] = data; |
| | | 76 | | // Circular Buffer logic |
| | 2 | 77 | | q->tail = (q->tail + 1) % MAX_QUEUE_SIZE; |
| | 2 | 78 | | q->count++; |
| | 2 | 79 | | unlockMutex(&q->lock); |
| | 2 | 80 | | return true; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Dequeue element (Thread-safe) |
| | 3 | 84 | | bool dequeue(Queue *q, SensorData *outData) { |
| | 3 | 85 | | lockMutex(&q->lock); |
| | | 86 | | |
| | 3 | 87 | | if (isQueueEmpty(q)) { |
| | | 88 | | // printf("Queue is empty.\n"); // Logging might be noisy |
| | 1 | 89 | | unlockMutex(&q->lock); |
| | 1 | 90 | | return false; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // Read data from head |
| | 2 | 94 | | *outData = q->items[q->head]; |
| | | 95 | | // Circular Buffer logic |
| | 2 | 96 | | q->head = (q->head + 1) % MAX_QUEUE_SIZE; |
| | 2 | 97 | | q->count--; |
| | 2 | 98 | | unlockMutex(&q->lock); |
| | 2 | 99 | | return true; |
| | | 100 | | } |
| | | 101 | | |
| | 4 | 102 | | int getQueueSize(Queue *q) { |
| | | 103 | | int size; |
| | 4 | 104 | | lockMutex(&q->lock); |
| | 4 | 105 | | size = q->count; |
| | 4 | 106 | | unlockMutex(&q->lock); |
| | 4 | 107 | | return size; |
| | | 108 | | } |