From 88967acdaa49de4b71294cce988687f06c192e19 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Thu, 17 Oct 2024 08:53:53 +0100 Subject: [PATCH] Update cond_wait to insert into waiters list in sorted order of priority --- src/threads/synch.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/threads/synch.c b/src/threads/synch.c index b588656..2431494 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -355,7 +355,14 @@ cond_wait (struct condition *cond, struct lock *lock) ASSERT (lock_held_by_current_thread (lock)); sema_init (&waiter.semaphore, 0); - list_push_back (&cond->waiters, &waiter.elem); + + /* Insert the semaphore_elem into the waiters list in order of priority. + We pass the priority of the current thread as aux to sema_priority_more + because the thread has not been sema'd down yet (See sema_priority_more) */ + int priority = thread_current ()->priority; + list_insert_ordered (&cond->waiters, &waiter.elem, sema_priority_more, + &priority); + lock_release (lock); sema_down (&waiter.semaphore); lock_acquire (lock);