From 47beba686ff9d2aab63738fba7176a5235703d05 Mon Sep 17 00:00:00 2001 From: yctct Date: Sun, 13 Sep 2026 18:58:14 +0200 Subject: Add all files, first commit --- src/queue.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/queue.c (limited to 'src/queue.c') diff --git a/src/queue.c b/src/queue.c new file mode 100644 index 0000000..76907d9 --- /dev/null +++ b/src/queue.c @@ -0,0 +1,55 @@ +#include "codexion.h" +# include // for atoi(), malloc() +# include // for printf() + +void heapify(t_fork *fork) +{ + t_ticket t1; + t_ticket t2; + + (void)pthread_mutex_lock(&fork->mutex_pq); + if (fork->pq.size < 2) + { + (void)pthread_mutex_unlock(&fork->mutex_pq); + return ; + } + (void)pthread_mutex_unlock(&fork->mutex_pq); + t1 = fork->pq.items[0]; + t2 = fork->pq.items[1]; + if (t1.num > t2.num) + swap(t1, t2); +} + +void enqueue(t_fork *fork, t_ticket ticket) +{ + int queue_size; + + (void)pthread_mutex_lock(&fork->mutex_pq); + queue_size = fork->pq.size; + (void)pthread_mutex_unlock(&fork->mutex_pq); + if (queue_size == MAX) + { + printf("Priority queue is full\n"); + return ; + } + (void)pthread_mutex_lock(&fork->mutex_pq); + fork->pq.items[fork->pq.size++] = ticket; + (void)pthread_mutex_unlock(&fork->mutex_pq); +} + +void dequeue(t_fork *fork) +{ + int queue_size; + + (void)pthread_mutex_lock(&fork->mutex_pq); + queue_size = fork->pq.size; + (void)pthread_mutex_unlock(&fork->mutex_pq); + if (!queue_size) + { + printf("Priority queue is empty\n"); + return ; + } + (void)pthread_mutex_lock(&fork->mutex_pq); + fork->pq.items[0] = fork->pq.items[--fork->pq.size]; + (void)pthread_mutex_unlock(&fork->mutex_pq); +} -- cgit v1.2.3