Update lock_acquire to attempt donation of priorities, w/ S

This commit is contained in:
Themis Demetriades
2024-10-20 19:19:43 +01:00
parent 5a651f1279
commit f98e4bc81c

View File

@@ -187,6 +187,9 @@ lock_init (struct lock *lock)
sema_init (&lock->semaphore, 1); sema_init (&lock->semaphore, 1);
} }
static void
donate_priority (struct thread *donor, struct thread *donee);
/* Acquires LOCK, sleeping until it becomes available if /* Acquires LOCK, sleeping until it becomes available if
necessary. The lock must not already be held by the current necessary. The lock must not already be held by the current
thread. thread.
@@ -202,8 +205,18 @@ lock_acquire (struct lock *lock)
ASSERT (!intr_context ()); ASSERT (!intr_context ());
ASSERT (!lock_held_by_current_thread (lock)); ASSERT (!lock_held_by_current_thread (lock));
struct thread *t = thread_current ();
/* TODO: If a high-priority thread holding a lock sleeps, this may cause a
race condition here or break the assumption that donation must occur. */
if (lock->holder != NULL)
{
t->waiting_lock = lock;
}
sema_down (&lock->semaphore); sema_down (&lock->semaphore);
lock->holder = thread_current (); lock->holder = thread_current ();
t->waiting_lock = NULL;
} }
/* Tries to acquires LOCK and returns true if successful or false /* Tries to acquires LOCK and returns true if successful or false