From 78c6fd36e3db13cbb2db8586d69636ddff09a727 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Tue, 22 Oct 2024 21:04:14 +0100 Subject: [PATCH] Refactor sema_up to add comments for clarity, w/ T --- src/threads/synch.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/threads/synch.c b/src/threads/synch.c index 655e014..9900748 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -106,7 +106,7 @@ sema_try_down (struct semaphore *sema) This function may be called from an interrupt handler. */ void -sema_up (struct semaphore *sema) +sema_up (struct semaphore *sema) { enum intr_level old_level; @@ -114,14 +114,19 @@ sema_up (struct semaphore *sema) old_level = intr_disable (); if (!list_empty (&sema->waiters)) - { - struct list_elem *e = list_min (&sema->waiters, priority_more, NULL); - list_remove (e); - thread_unblock (list_entry (e, struct thread, elem)); - } + { + /* Enforces wake-up of the highest priority thread waiting for the + semaphore. */ + struct list_elem *e = list_min (&sema->waiters, priority_more, NULL); + list_remove (e); + thread_unblock (list_entry (e, struct thread, elem)); + } sema->value++; intr_set_level (old_level); + /* Yields the CPU in case the thread that has been woken up has a higher + priority that the current running thread, including the case when called + within an interrupt handler. */ if (intr_context ()) intr_yield_on_return (); else