From a875d5fcb4eec1aeb97b5ab47ae963a9f8f3e5ae Mon Sep 17 00:00:00 2001 From: Themis Demetriades Date: Wed, 23 Oct 2024 13:29:45 +0100 Subject: [PATCH] Update donate_priority to only attempt to sort position of the donee that isn't waiting for a lock --- src/threads/synch.c | 11 ++++++++--- src/threads/thread.c | 18 +++++++++++++++--- src/threads/thread.h | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/threads/synch.c b/src/threads/synch.c index 1123249..462bda7 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -214,12 +214,17 @@ donate_priority (struct thread *donor, struct thread *donee) { /* Also stop propagation of donation once a donee is reached with no donees of its own (sink node in WFG). */ if (donee->waiting_lock == NULL) - donee = NULL; + { + + /* Only the sink node of the WFG isn't waiting for a lock and + could be on the ready list. Thus, as its priority changed, + it must be reinserted into the list. */ + ready_list_reinsert (donee); + donee = NULL; + } else donee = donee->waiting_lock->holder; } - - ready_list_reorder (); } /* Acquires LOCK, sleeping until it becomes available if diff --git a/src/threads/thread.c b/src/threads/thread.c index ff66100..178cdf9 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -465,10 +465,22 @@ thread_get_recent_cpu (void) return 0; } +/* Reinsert thread t into the ready list at its correct position + in descending order of priority. Used when this thread's priority + may have changed. */ void -ready_list_reorder (void) +ready_list_reinsert (struct thread *t) { - list_sort (&ready_list, priority_more, NULL); + enum intr_level old_level = intr_disable (); + + /* If the thread isn't ready to run, do nothing. */ + if (t->status == THREAD_READY) + { + list_remove (&t->elem); + list_insert_ordered (&ready_list, &t->elem, priority_more, NULL); + } + + intr_set_level (old_level); } /* Idle thread. Executes when no other thread is ready to run. @@ -558,7 +570,7 @@ init_thread (struct thread *t, const char *name, int priority) t->stack = (uint8_t *) t + PGSIZE; t->base_priority = priority; t->magic = THREAD_MAGIC; - + list_init (&t->donors_list); t->priority = t->base_priority; t->waiting_lock = NULL; diff --git a/src/threads/thread.h b/src/threads/thread.h index de9df61..ffeb2b3 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -151,6 +151,6 @@ void thread_set_nice (int); int thread_get_recent_cpu (void); int thread_get_load_avg (void); -void ready_list_reorder (void); +void ready_list_reinsert (struct thread *t); #endif /* threads/thread.h */