Merge 'task1/priority-donation' into 'master' #14
@@ -265,43 +265,25 @@ struct semaphore_elem
|
|||||||
priority of the first semaphore. This is useful when the thread has not been
|
priority of the first semaphore. This is useful when the thread has not been
|
||||||
sema'd down yet. */
|
sema'd down yet. */
|
||||||
static bool
|
static bool
|
||||||
sema_priority_more(const struct list_elem *a_, const struct list_elem *b_,
|
sema_priority_more(const struct list_elem *a, const struct list_elem *b,
|
||||||
void *aux)
|
void *inserting_telem)
|
||||||
{
|
{
|
||||||
int a_priority, b_priority;
|
struct list_elem *te_a, *te_b;
|
||||||
|
|
||||||
|
te_b = list_front (
|
||||||
|
&list_entry (b, struct semaphore_elem, elem)->semaphore.waiters);
|
||||||
|
|
||||||
/* If an aux is provided, then use it as the priority of the first semaphore.
|
if (inserting_telem == NULL)
|
||||||
Otherwise, get the priority of the first semaphore. */
|
{
|
||||||
if (aux != NULL)
|
te_a = list_front (
|
||||||
a_priority = *(int *) aux;
|
&list_entry (a, struct semaphore_elem, elem)->semaphore.waiters);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
struct semaphore_elem *a = list_entry(a_, struct semaphore_elem, elem);
|
te_a = inserting_telem;
|
||||||
|
|
||||||
/* If waiters list is empty, return false (i.e., a has lower priority) */
|
|
||||||
if (list_empty(&a->semaphore.waiters))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
/* Otherwise, get the thread with the highest priority from the waiters
|
|
||||||
list. By design, this is the first one in the list (See sema_down). */
|
|
||||||
struct thread *a_thread =
|
|
||||||
list_entry(list_front(&a->semaphore.waiters), struct thread, elem);
|
|
||||||
|
|
||||||
a_priority = a_thread->priority;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct semaphore_elem *b = list_entry(b_, struct semaphore_elem, elem);
|
return priority_more (te_a, te_b, NULL);
|
||||||
|
|
||||||
/* If waiters list is empty, return true (i.e., a has higher priority) */
|
|
||||||
if (list_empty(&b->semaphore.waiters))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
struct thread *b_thread =
|
|
||||||
list_entry(list_front(&b->semaphore.waiters), struct thread, elem);
|
|
||||||
|
|
||||||
b_priority = b_thread->priority;
|
|
||||||
|
|
||||||
return a_priority > b_priority;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initializes condition variable COND. A condition variable
|
/* Initializes condition variable COND. A condition variable
|
||||||
@@ -346,7 +328,8 @@ 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, sema_priority_more,
|
||||||
|
&thread_current ()->elem);
|
||||||
lock_release (lock);
|
lock_release (lock);
|
||||||
sema_down (&waiter.semaphore);
|
sema_down (&waiter.semaphore);
|
||||||
lock_acquire (lock);
|
lock_acquire (lock);
|
||||||
|
|||||||
Reference in New Issue
Block a user