From 60acc2e58daa9de37f59c2dfbe121c3efc1f4643 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Thu, 24 Oct 2024 21:46:39 +0100 Subject: [PATCH] refactor thread_mlfqs checks in synch.c --- src/threads/synch.c | 77 +++++++++++++++++++++----------------------- src/threads/thread.c | 32 ++++++++++-------- 2 files changed, 55 insertions(+), 54 deletions(-) diff --git a/src/threads/synch.c b/src/threads/synch.c index 0ceb746..3fe957c 100644 --- a/src/threads/synch.c +++ b/src/threads/synch.c @@ -305,53 +305,50 @@ lock_release (struct lock *lock) ASSERT (lock != NULL); ASSERT (lock_held_by_current_thread (lock)); - if (!thread_mlfqs) - { - struct thread *current_thread = thread_current (); - struct thread *max_donor = NULL; + struct thread *current_thread = thread_current (); + struct thread *max_donor = NULL; - struct list orphan_list; - list_init (&orphan_list); + struct list orphan_list; + list_init (&orphan_list); - enum intr_level old_level = intr_disable (); - /* Loop through current thread's donors, removing the ones waiting for the - lock being released and keeping track of them (within orphan_list). - Also identifies the highest priority donor thread among them. */ - struct list_elem *tail = list_tail (¤t_thread->donors_list); - struct list_elem *e = list_begin (¤t_thread->donors_list); - while (e != tail) - { - struct thread *donor = list_entry (e, struct thread, donor_elem); - struct list_elem *next = list_next (e); + enum intr_level old_level = intr_disable (); + /* Loop through current thread's donors, removing the ones waiting for the + lock being released and keeping track of them (within orphan_list). + Also identifies the highest priority donor thread among them. */ + struct list_elem *tail = list_tail (¤t_thread->donors_list); + struct list_elem *e = list_begin (¤t_thread->donors_list); + while (e != tail) + { + struct thread *donor = list_entry (e, struct thread, donor_elem); + struct list_elem *next = list_next (e); - /* Excludes donors that aren't waiting for the lock being released, - and tracks the rest. */ - if (donor->waiting_lock == lock) - { - list_remove (e); - list_push_back (&orphan_list, e); + /* Excludes donors that aren't waiting for the lock being released, + and tracks the rest. */ + if (donor->waiting_lock == lock) + { + list_remove (e); + list_push_back (&orphan_list, e); - /* Identify highest priority donor. */ - if (max_donor == NULL || donor->priority > max_donor->priority) - max_donor = donor; - } + /* Identify highest priority donor. */ + if (max_donor == NULL || donor->priority > max_donor->priority) + max_donor = donor; + } - e = next; - } + e = next; + } - /* If there exists a maximum donor thread waiting for this lock to be - released, transfer the remaining orphaned donors to its donor list. */ - if (max_donor != NULL) - { - while (!list_empty (&orphan_list)) - list_push_back (&max_donor->donors_list, list_pop_front (&orphan_list)); - } + /* If there exists a maximum donor thread waiting for this lock to be + released, transfer the remaining orphaned donors to its donor list. */ + if (max_donor != NULL) + { + while (!list_empty (&orphan_list)) + list_push_back (&max_donor->donors_list, list_pop_front (&orphan_list)); + } - intr_set_level (old_level); - /* Removal of donors to this thread may change its effective priority, - so recalculate. */ - thread_recalculate_priority (); - } + intr_set_level (old_level); + /* Removal of donors to this thread may change its effective priority, + so recalculate. */ + thread_recalculate_priority (); lock->holder = NULL; sema_up (&lock->semaphore); diff --git a/src/threads/thread.c b/src/threads/thread.c index 4afbe68..795866e 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -157,23 +157,26 @@ thread_tick (void) /* Update system load_avg and all threads recent_cpu every second. */ int64_t ticks = timer_ticks (); - if (thread_mlfqs && (ticks % TIMER_FREQ == 0)) + if (thread_mlfqs) { - size_t ready = threads_ready (); if (t != idle_thread) - ready++; - fp32_t old_coeff = fp_div_int (fp_mul_int(load_avg, 59), 60); - fp32_t new_coeff = fp_div_int (fp_from_int (ready), 60); - load_avg = fp_add (old_coeff, new_coeff); + { + t->recent_cpu = fp_add_int (t->recent_cpu, 1); + } - thread_foreach (thread_update_recent_cpu, NULL); - } + if (ticks % TIMER_FREQ == 0) + { + size_t ready = threads_ready (); + if (t != idle_thread) + ready++; + fp32_t old_coeff = fp_div_int (fp_mul_int(load_avg, 59), 60); + fp32_t new_coeff = fp_div_int (fp_from_int (ready), 60); + load_avg = fp_add (old_coeff, new_coeff); - /* Update current thread's recent_cpu. */ - if (thread_mlfqs && (t != idle_thread)) - { - t->recent_cpu = fp_add_int (t->recent_cpu, 1); - if (ticks % 4 == 0) // recent_cpu was updated, update priority. + thread_foreach (thread_update_recent_cpu, NULL); + } + + if (ticks % TIME_SLICE == 0) // recent_cpu was updated, update priority. t->priority = calculate_bsd_priority (t->recent_cpu, t->nice); } @@ -470,7 +473,8 @@ thread_recalculate_priority (void) { struct thread *t = thread_current (); - ASSERT(!thread_mlfqs) + if (thread_mlfqs) + return; enum intr_level old_level = intr_disable (); t->priority = t->base_priority;