From dae5b0d097f6424867ecfde3a6a5a3f38f740c36 Mon Sep 17 00:00:00 2001 From: Themis Demetriades Date: Sun, 20 Oct 2024 22:50:31 +0100 Subject: [PATCH] Update semaphore priority scheduling to use linear search on unordered list w/ S --- src/threads/synch.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/threads/synch.c b/src/threads/synch.c index cdfb2dc..b74303d 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -68,8 +68,7 @@ sema_down (struct semaphore *sema) old_level = intr_disable (); while (sema->value == 0) { - list_insert_ordered(&sema->waiters, &thread_current ()->elem, - priority_more, NULL); + list_push_back (&sema->waiters, &thread_current ()->elem); thread_block (); } sema->value--; @@ -114,9 +113,12 @@ sema_up (struct semaphore *sema) ASSERT (sema != NULL); old_level = intr_disable (); - if (!list_empty (&sema->waiters)) - thread_unblock (list_entry (list_pop_front (&sema->waiters), - struct thread, elem)); + if (!list_empty (&sema->waiters)) + { + struct list_elem *e = list_min (&sema->waiters, priority_more, NULL); + list_remove (e); + thread_unblock (list_entry (e, struct thread, elem)); + } sema->value++; intr_set_level (old_level);