diff --git a/src/threads/synch.c b/src/threads/synch.c index f8e6dd3..09fc71f 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -32,6 +32,10 @@ #include "threads/interrupt.h" #include "threads/thread.h" +static bool +priority_less (const struct list_elem *a_, const struct list_elem *b_, + void *aux UNUSED); + /* Initializes semaphore SEMA to VALUE. A semaphore is a nonnegative integer along with two atomic operators for manipulating it: @@ -114,13 +118,13 @@ sema_up (struct semaphore *sema) old_level = intr_disable (); if (!list_empty (&sema->waiters)) - { - /* Enforces wake-up of the highest priority thread waiting for the - semaphore. */ - struct list_elem *e = list_min (&sema->waiters, priority_more, NULL); - list_remove (e); - thread_unblock (list_entry (e, struct thread, elem)); - } + { + /* Enforces wake-up of the highest priority thread waiting for the + semaphore. */ + struct list_elem *e = list_max (&sema->waiters, priority_less, NULL); + list_remove (e); + thread_unblock (list_entry (e, struct thread, elem)); + } sema->value++; intr_set_level (old_level); @@ -364,6 +368,19 @@ struct semaphore_elem struct semaphore semaphore; /* This semaphore. */ }; +/* Function that compares the two threads associated with the provided + pointers to their 'elem' member. Returns true if the thread associated + with a_ has a lower priority than that of b_. */ +static bool +priority_less (const struct list_elem *a_, const struct list_elem *b_, + void *aux UNUSED) +{ + struct thread *a = list_entry (a_, struct thread, elem); + struct thread *b = list_entry (b_, struct thread, elem); + + return a->priority < b->priority; +} + /* Function that compares the two *semaphores* associated with the provided list_elem structures. [i.e., takes list_elem of semaphore_elem, and] Returns true if the thread associated with the semaphore associated with a_ diff --git a/src/threads/thread.c b/src/threads/thread.c index 9e2fa41..d488677 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -261,7 +261,11 @@ thread_create (const char *name, int priority, /* Add to run queue. */ thread_unblock (t); - thread_yield (); + + /* Yield if the newly created thread has higher priority than the current + thread. */ + if (t->priority > thread_current ()->priority) + thread_yield (); return tid; }