diff --git a/src/threads/synch.c b/src/threads/synch.c index 387f060..374817f 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -265,43 +265,25 @@ struct semaphore_elem priority of the first semaphore. This is useful when the thread has not been sema'd down yet. */ static bool -sema_priority_more(const struct list_elem *a_, const struct list_elem *b_, - void *aux) +sema_priority_more(const struct list_elem *a, const struct list_elem *b, + 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. - Otherwise, get the priority of the first semaphore. */ - if (aux != NULL) - a_priority = *(int *) aux; + if (inserting_telem == NULL) + { + te_a = list_front ( + &list_entry (a, struct semaphore_elem, elem)->semaphore.waiters); + } else { - struct semaphore_elem *a = list_entry(a_, struct semaphore_elem, elem); - - /* 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; + te_a = inserting_telem; } - struct semaphore_elem *b = list_entry(b_, struct semaphore_elem, elem); - - /* 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; + return priority_more (te_a, te_b, NULL); } /* 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)); 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); sema_down (&waiter.semaphore); lock_acquire (lock);