#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); }