From 5a651f12794dc5fb04febe6ccc21159656a154fe Mon Sep 17 00:00:00 2001 From: sBubshait Date: Sun, 20 Oct 2024 18:06:07 +0100 Subject: [PATCH] Update implementation of thread set priority to account for donations, w/ T --- src/threads/thread.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/threads/thread.c b/src/threads/thread.c index 3fcfe1e..d65d7c4 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -370,21 +370,35 @@ priority_more (const struct list_elem *a_, const struct list_elem *b_, return a->priority > b->priority; } -/* Sets the current thread's priority to NEW_PRIORITY. */ +/* Sets the current thread's base priority to new_base_priority. + Updates the current thread's effective priority if necessary. */ void -thread_set_priority (int new_priority) +thread_set_priority (int new_base_priority) { - ASSERT (new_priority >= PRI_MIN); - ASSERT (new_priority <= PRI_MAX); + ASSERT (new_base_priority >= PRI_MIN); + ASSERT (new_base_priority <= PRI_MAX); + struct thread *t = thread_current (); + + int old_base_priority = t->base_priority; int old_priority = thread_get_priority (); - if (new_priority == old_priority) + if (new_base_priority == old_base_priority) return; - thread_current ()->priority = new_priority; - if (new_priority < old_priority && !list_empty (&ready_list)) - thread_yield (); + t->base_priority = new_base_priority; + t->priority = new_base_priority; + + if (!list_empty (&t->donors_list) && new_base_priority < old_priority) { + int max_donated_priority = + list_entry (list_max (&t->donors_list, priority_more, NULL), + struct thread, donor_elem)->priority; + + if(new_base_priority < max_donated_priority) + t->priority = new_base_priority; + } + + thread_yield (); } /* Returns the current thread's priority. */