From 5c09ff0149f937a95cd15d2d18558a27d447a1e5 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Thu, 17 Oct 2024 06:27:59 +0100 Subject: [PATCH] Updated set priority to yield if priority is lowered --- src/threads/thread.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/threads/thread.c b/src/threads/thread.c index f681f92..7366641 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -388,6 +388,19 @@ thread_set_priority (int new_priority) return; thread_current ()->priority = new_priority; + + if (new_priority < old_priority && !list_empty (&ready_list)) { + /* If the new priority is lower than the old priority, check if the current + thread no longer has the highest priority. If it doesn't, yield the CPU. + */ + + struct thread *next_thread = + list_entry (list_front (&ready_list), struct thread, elem); + + if (next_thread->priority > new_priority) { + thread_yield(); + } + } } /* Returns the current thread's priority. */