Implement priority scheduling for condition variables

This commit is contained in:
Themis Demetriades
2024-10-17 08:55:29 +01:00
parent c32a69a368
commit 810a5376b9

View File

@@ -266,6 +266,35 @@ cond_init (struct condition *cond)
list_init (&cond->waiters); list_init (&cond->waiters);
} }
/* Returns true iff the priority of the only thread in the first singleton
semaphore is greater than the priority of the only thread in the second
singleton semaphore.
If used for insertion, the third argument must be a pointer to struct
list_elem for the thread being inserted, otherwise must be NULL. */
static bool
singleton_sema_priority_greater (const struct list_elem *a,
const struct list_elem *b,
void *insertingThread)
{
struct list_elem *te_a, *te_b;
te_b = list_front (
&list_entry (b, struct semaphore_elem, elem)->semaphore.waiters);
if (insertingThread == NULL)
{
te_a = list_front (
&list_entry (a, struct semaphore_elem, elem)->semaphore.waiters);
}
else
{
te_a = insertingThread;
}
return thread_priority_greater (te_a, te_b, NULL);
}
/* Atomically releases LOCK and waits for COND to be signaled by /* Atomically releases LOCK and waits for COND to be signaled by
some other piece of code. After COND is signaled, LOCK is some other piece of code. After COND is signaled, LOCK is
reacquired before returning. LOCK must be held before calling reacquired before returning. LOCK must be held before calling
@@ -297,7 +326,9 @@ cond_wait (struct condition *cond, struct lock *lock)
ASSERT (lock_held_by_current_thread (lock)); ASSERT (lock_held_by_current_thread (lock));
sema_init (&waiter.semaphore, 0); sema_init (&waiter.semaphore, 0);
list_push_back (&cond->waiters, &waiter.elem); list_insert_ordered (&cond->waiters, &waiter.elem,
singleton_sema_priority_greater,
&thread_current ()->elem);
lock_release (lock); lock_release (lock);
sema_down (&waiter.semaphore); sema_down (&waiter.semaphore);
lock_acquire (lock); lock_acquire (lock);