diff options
Diffstat (limited to 'src/queue.c')
| -rw-r--r-- | src/queue.c | 55 |
1 files changed, 55 insertions, 0 deletions
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 <stdlib.h> // for atoi(), malloc() +# include <stdio.h> // 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); +} |
