Allow priority donation with BSD scheduler.

- Recalculate priority with donations after BSD priority updates
- Remove mlfqs checks from lock_acquire
This commit is contained in:
2024-10-25 14:03:55 +01:00
parent af31968e67
commit 6e072a557f
2 changed files with 29 additions and 30 deletions

View File

@@ -252,25 +252,16 @@ lock_acquire (struct lock *lock)
struct thread *t = thread_current ();
if(!thread_mlfqs)
{
enum intr_level old_level = intr_disable ();
if (lock->holder != NULL)
{
t->waiting_lock = lock;
donate_priority (lock->holder);
}
intr_set_level (old_level);
}
sema_down (&lock->semaphore);
lock->holder = thread_current ();
if (!thread_mlfqs)
t->waiting_lock = NULL;
}

View File

@@ -74,6 +74,7 @@ static bool is_thread (struct thread *) UNUSED;
static void *alloc_frame (struct thread *, size_t size);
static int calculate_bsd_priority (fp32_t recent_cpu, int nice);
static void update_recent_cpu (struct thread *t, void *aux UNUSED);
static void recalculate_priority (struct thread *t);
static void schedule (void);
void thread_schedule_tail (struct thread *prev);
static tid_t allocate_tid (void);
@@ -178,8 +179,11 @@ thread_tick (void)
}
/* Recent cpu was updated, update priority. */
if (ticks % PRI_UPDATE_FREQ == 0)
t->priority = calculate_bsd_priority (t->recent_cpu, t->nice);
if (t != idle_thread && ticks % PRI_UPDATE_FREQ == 0)
{
t->base_priority = calculate_bsd_priority (t->recent_cpu, t->nice);
recalculate_priority (t);
}
}
/* Enforce preemption. */
@@ -443,7 +447,7 @@ thread_set_priority (int new_base_priority)
return;
t->base_priority = new_base_priority;
thread_recalculate_priority ();
recalculate_priority (t);
thread_yield ();
}
@@ -467,17 +471,21 @@ update_recent_cpu (struct thread *t, void *aux UNUSED)
t->recent_cpu
= fp_add_int (fp_mul (recent_cpu_coeff, curr_recent_cpu), t->nice);
// recent_cpu was updated, update priority.
t->priority = calculate_bsd_priority (t->recent_cpu, t->nice);
t->base_priority = calculate_bsd_priority (t->recent_cpu, t->nice);
recalculate_priority (t);
}
/* Recalculates the effective priority of the current thread. */
void
thread_recalculate_priority (void)
{
struct thread *t = thread_current ();
recalculate_priority (t);
}
if (thread_mlfqs)
return;
static void
recalculate_priority (struct thread *t)
{
enum intr_level old_level = intr_disable ();
t->priority = t->base_priority;
@@ -495,7 +503,6 @@ thread_recalculate_priority (void)
t->priority = max_donated_priority;
}
intr_set_level (old_level);
}
/* Sets the current thread's nice value to NICE. */
@@ -506,10 +513,11 @@ thread_set_nice (int nice)
struct thread *t = thread_current ();
t->nice = nice;
int priority = calculate_bsd_priority (t->recent_cpu, t->nice);
t->base_priority = calculate_bsd_priority (t->recent_cpu, t->nice);
recalculate_priority (t);
struct thread *next_t
= list_entry (list_begin (&ready_list), struct thread, elem);
if (priority < next_t->priority)
if (t->priority < next_t->priority)
thread_yield ();
}
@@ -638,14 +646,14 @@ init_thread (struct thread *t, const char *name, int nice, int priority,
t->stack = (uint8_t *) t + PGSIZE;
t->magic = THREAD_MAGIC;
t->base_priority = priority;
t->base_priority
= thread_mlfqs ? calculate_bsd_priority (recent_cpu, nice) : priority;
list_init (&t->donors_list);
t->waiting_lock = NULL;
t->nice = nice;
t->recent_cpu = recent_cpu;
t->priority = thread_mlfqs ? calculate_bsd_priority (recent_cpu, nice)
: t->base_priority;
t->priority = t->base_priority;
old_level = intr_disable ();
list_push_back (&all_list, &t->allelem);