From 81309dcda927ca16e4b49424229d19af013ec8fd Mon Sep 17 00:00:00 2001 From: sBubshait Date: Fri, 25 Oct 2024 15:44:56 +0100 Subject: [PATCH 1/3] Refactor sema_up to follow PintOS styling of if statements --- src/threads/synch.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/threads/synch.c b/src/threads/synch.c index f8e6dd3..4933a58 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -114,13 +114,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_min (&sema->waiters, priority_more, NULL); + list_remove (e); + thread_unblock (list_entry (e, struct thread, elem)); + } sema->value++; intr_set_level (old_level); From 30ab3ae8616ad46cf1d9c142d938ed1a2e8b96fa Mon Sep 17 00:00:00 2001 From: sBubshait Date: Fri, 25 Oct 2024 15:47:19 +0100 Subject: [PATCH 2/3] Update thread_create to only yield CPU to the new thread if necessary --- src/threads/thread.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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; } From c2414ec54d1acd3530dd59e60251cb9b3c27d143 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Fri, 25 Oct 2024 16:04:55 +0100 Subject: [PATCH 3/3] Add priority_less for comparing threads based on priority and Refactor sema up to use list_max for clarity --- src/threads/synch.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/threads/synch.c b/src/threads/synch.c index 4933a58..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: @@ -117,7 +121,7 @@ sema_up (struct semaphore *sema) { /* Enforces wake-up of the highest priority thread waiting for the semaphore. */ - struct list_elem *e = list_min (&sema->waiters, priority_more, NULL); + struct list_elem *e = list_max (&sema->waiters, priority_less, NULL); list_remove (e); thread_unblock (list_entry (e, struct thread, elem)); } @@ -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_