From 53b296d6c4fab1ac436de6962733f4df7249d950 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Thu, 17 Oct 2024 08:36:29 +0100 Subject: [PATCH] Add function to compare the priority of two semaphores --- src/threads/synch.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/threads/synch.c b/src/threads/synch.c index ffdb588..078f142 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -265,6 +265,27 @@ struct semaphore_elem struct semaphore semaphore; /* This semaphore. */ }; +/* Function that compares the two *semaphores* associated with the provided + list_elem structures. [i.e., takes list_elem of semaphore_elem, and] + Returns true if the thread associated with the semaphore associated with a_ + has a higher priority than that of b_. */ +static bool +sema_priority_more(const struct list_elem *a_, const struct list_elem *b_, + void *aux UNUSED) +{ + struct semaphore_elem *a = list_entry(a_, struct semaphore_elem, elem); + struct semaphore_elem *b = list_entry(b_, struct semaphore_elem, elem); + + /* Get the highest priority thread from the waiters list of each semaphore. By + design, this is the first element in the list (See sema_down). */ + struct thread *a_thread = + list_entry(list_front(&a->semaphore.waiters), struct thread, elem); + struct thread *b_thread = + list_entry(list_front(&b->semaphore.waiters), struct thread, elem); + + return a_thread->priority > b_thread->priority; +} + /* Initializes condition variable COND. A condition variable allows one piece of code to signal a condition and cooperating code to receive the signal and act upon it. */