From dbb7fd56e3b42369de23a3954b0007e8094fdd38 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Thu, 17 Oct 2024 06:38:27 +0100 Subject: [PATCH] Update set priority to reorder the list if priority is changed --- src/threads/thread.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/threads/thread.c b/src/threads/thread.c index f364679..52d8577 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -391,6 +391,15 @@ thread_set_priority (int new_priority) enum intr_level old_level = intr_disable (); + /* If the thread is in the ready list, the list must be reordered to maintain + the priority order. */ + if (thread_current ()->status == THREAD_READY) { + /* Remove from the ready list and reinsert it in priority order. */ + list_remove (&thread_current ()->elem); + list_insert_ordered (&ready_list, &thread_current ()->elem, priority_more, + NULL); + } + 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.