Refactor lock release and sema up to remove unnecessary code

This commit is contained in:
sBubshait
2024-11-06 15:36:56 +00:00
parent 74efa5e652
commit 91cef4d650

View File

@@ -113,6 +113,7 @@ void
sema_up (struct semaphore *sema)
{
enum intr_level old_level;
bool thread_unblocked = false; /* Flag to track if any thread was woken up. */
ASSERT (sema != NULL);
@@ -124,6 +125,7 @@ sema_up (struct semaphore *sema)
struct list_elem *e = list_max (&sema->waiters, priority_less, NULL);
list_remove (e);
thread_unblock (list_entry (e, struct thread, elem));
thread_unblocked = true;
}
sema->value++;
intr_set_level (old_level);
@@ -131,10 +133,13 @@ sema_up (struct semaphore *sema)
/* Yields the CPU in case the thread that has been woken up has a higher
priority that the current running thread, including the case when called
within an interrupt handler. */
if (thread_unblocked)
{
if (intr_context ())
intr_yield_on_return ();
else
thread_yield ();
}
}
static void sema_test_helper (void *sema_);
@@ -347,7 +352,6 @@ lock_release (struct lock *lock)
lock->holder = NULL;
sema_up (&lock->semaphore);
thread_yield ();
}
/* Returns true if the current thread holds LOCK, false