Update implementation of thread set priority to account for donations, w/ T

This commit is contained in:
sBubshait
2024-10-20 18:06:07 +01:00
parent fcc8cbb71e
commit 5a651f1279

View File

@@ -370,21 +370,35 @@ priority_more (const struct list_elem *a_, const struct list_elem *b_,
return a->priority > b->priority; 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 void
thread_set_priority (int new_priority) thread_set_priority (int new_base_priority)
{ {
ASSERT (new_priority >= PRI_MIN); ASSERT (new_base_priority >= PRI_MIN);
ASSERT (new_priority <= PRI_MAX); ASSERT (new_base_priority <= PRI_MAX);
struct thread *t = thread_current ();
int old_base_priority = t->base_priority;
int old_priority = thread_get_priority (); int old_priority = thread_get_priority ();
if (new_priority == old_priority) if (new_base_priority == old_base_priority)
return; return;
thread_current ()->priority = new_priority;
if (new_priority < old_priority && !list_empty (&ready_list)) t->base_priority = new_base_priority;
thread_yield (); 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. */ /* Returns the current thread's priority. */