Merge branch 'gleb/bsd-priority-donation' into 'master'

Allow priority donation with BSD scheduler.

See merge request lab2425_autumn/pintos_22!18
This commit is contained in:
Demetriades, Themis
2024-10-25 14:19:18 +00:00
2 changed files with 29 additions and 30 deletions

View File

@@ -252,26 +252,17 @@ lock_acquire (struct lock *lock)
struct thread *t = thread_current (); struct thread *t = thread_current ();
if(!thread_mlfqs) enum intr_level old_level = intr_disable ();
{ if (lock->holder != NULL)
{
enum intr_level old_level = intr_disable (); t->waiting_lock = lock;
donate_priority (lock->holder);
if (lock->holder != NULL) }
{ intr_set_level (old_level);
t->waiting_lock = lock;
donate_priority (lock->holder);
}
intr_set_level (old_level);
}
sema_down (&lock->semaphore); sema_down (&lock->semaphore);
lock->holder = thread_current (); lock->holder = thread_current ();
if (!thread_mlfqs) t->waiting_lock = NULL;
t->waiting_lock = NULL;
} }
/* Tries to acquires LOCK and returns true if successful or false /* Tries to acquires LOCK and returns true if successful or false

View File

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