From 343ac55d372f6f6d93c2c7e6a96d46e2ae889a44 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Sun, 20 Oct 2024 19:42:35 +0100 Subject: [PATCH] Implement priority donation helper function with propagation, w/ T --- src/threads/synch.c | 16 +++++++++++++++- src/threads/thread.c | 6 ++++++ src/threads/thread.h | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/threads/synch.c b/src/threads/synch.c index 4bf54a6..3495ca8 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -188,7 +188,21 @@ lock_init (struct lock *lock) } static void -donate_priority (struct thread *donor, struct thread *donee); +donate_priority (struct thread *donor, struct thread *donee) { + while (donee != NULL) + { + if (donor->priority <= donee->priority) + break; + + donee->priority = donor->priority; + if (donee->waiting_lock == NULL) + donee = NULL; + else + donee = donee->waiting_lock->holder; + } + + ready_list_reorder (); +} /* Acquires LOCK, sleeping until it becomes available if necessary. The lock must not already be held by the current diff --git a/src/threads/thread.c b/src/threads/thread.c index d65d7c4..b7bab5b 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -439,6 +439,12 @@ thread_get_recent_cpu (void) return 0; } +void +ready_list_reorder (void) +{ + list_sort (&ready_list, priority_more, NULL); +} + /* Idle thread. Executes when no other thread is ready to run. The idle thread is initially put on the ready list by diff --git a/src/threads/thread.h b/src/threads/thread.h index 4e719a7..083a5ab 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -150,6 +150,8 @@ void thread_set_nice (int); int thread_get_recent_cpu (void); int thread_get_load_avg (void); +void ready_list_reorder (void); + /* Returns true iff the priority of the first list element's thread is greater than that of the second list element's thread. */ list_less_func thread_priority_greater;