Update sema_up to yield CPU if unblocked has higher priority

This commit is contained in:
sBubshait
2024-10-17 07:15:10 +01:00
parent 24900545d4
commit 0d6fb2f167

View File

@@ -114,11 +114,22 @@ sema_up (struct semaphore *sema)
ASSERT (sema != NULL); ASSERT (sema != NULL);
old_level = intr_disable (); old_level = intr_disable ();
if (!list_empty (&sema->waiters))
thread_unblock (list_entry (list_pop_front (&sema->waiters), /* Wake up (unblock) the highest priority thread from the waiters list */
struct thread, elem)); struct thread *t = NULL;
if (!list_empty (&sema->waiters))
{
t = list_entry (list_pop_front (&sema->waiters), struct thread, elem);
thread_unblock (t);
}
sema->value++; sema->value++;
intr_set_level (old_level); intr_set_level (old_level);
/* If the unblocked thread has higher priority than the current running thread
then yield the CPU to the unblocked thread */
if (!intr_context() && t != NULL && t->priority > thread_get_priority ())
thread_yield ();
} }
static void sema_test_helper (void *sema_); static void sema_test_helper (void *sema_);