Merge branch 'master' into 'BSD-merged', fixing merge conflicts
# Conflicts: # src/threads/thread.c
This commit is contained in:
@@ -78,6 +78,8 @@ static bool thread_priority_less (const struct list_elem *a,
|
||||
static void schedule (void);
|
||||
void thread_schedule_tail (struct thread *prev);
|
||||
static tid_t allocate_tid (void);
|
||||
static bool donor_priority_less (const struct list_elem *a_,
|
||||
const struct list_elem *b_, void *aux UNUSED);
|
||||
|
||||
/* Initializes the threading system by transforming the code
|
||||
that's currently running into a thread. This can't work in
|
||||
@@ -252,6 +254,7 @@ thread_create (const char *name, int priority,
|
||||
|
||||
/* Add to run queue. */
|
||||
thread_unblock (t);
|
||||
thread_yield ();
|
||||
|
||||
return tid;
|
||||
}
|
||||
@@ -289,10 +292,13 @@ thread_unblock (struct thread *t)
|
||||
|
||||
old_level = intr_disable ();
|
||||
ASSERT (t->status == THREAD_BLOCKED);
|
||||
|
||||
if (thread_mlfqs)
|
||||
list_insert_ordered (&ready_list, &t->elem, thread_priority_less, NULL);
|
||||
else
|
||||
list_push_back (&ready_list, &t->elem);
|
||||
/* Insert the thread back into the ready list in priority order. */
|
||||
list_insert_ordered(&ready_list, &t->elem, priority_more, NULL);
|
||||
|
||||
t->status = THREAD_READY;
|
||||
intr_set_level (old_level);
|
||||
}
|
||||
@@ -362,14 +368,17 @@ thread_yield (void)
|
||||
ASSERT (!intr_context ());
|
||||
|
||||
old_level = intr_disable ();
|
||||
|
||||
if (cur != idle_thread)
|
||||
{
|
||||
if (thread_mlfqs)
|
||||
list_insert_ordered (&ready_list, &cur->elem, thread_priority_less,
|
||||
NULL);
|
||||
else
|
||||
list_push_back (&ready_list, &cur->elem);
|
||||
/* Insert the thread back into the ready list in priority order. */
|
||||
list_insert_ordered(&ready_list, &cur->elem, priority_more, NULL);
|
||||
}
|
||||
|
||||
cur->status = THREAD_READY;
|
||||
schedule ();
|
||||
intr_set_level (old_level);
|
||||
@@ -392,22 +401,66 @@ thread_foreach (thread_action_func *func, void *aux)
|
||||
}
|
||||
}
|
||||
|
||||
/* Sets the current thread's priority to NEW_PRIORITY. */
|
||||
void
|
||||
thread_set_priority (int new_priority)
|
||||
/* 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 higher priority than that of b_. */
|
||||
bool
|
||||
priority_more (const struct list_elem *a_, const struct list_elem *b_,
|
||||
void *aux UNUSED)
|
||||
{
|
||||
if (thread_mlfqs)
|
||||
return;
|
||||
thread_current ()->priority = new_priority;
|
||||
|
||||
struct thread *a = list_entry (a_, struct thread, elem);
|
||||
struct thread *b = list_entry (b_, struct thread, elem);
|
||||
|
||||
return a->priority > b->priority;
|
||||
|
||||
}
|
||||
|
||||
/* Returns the current thread's priority. */
|
||||
/* Function that compares the two threads associated with the provided
|
||||
pointers to their 'donor_elem' member. Returns true if the thread associated
|
||||
with a_ has a lower priority than that of b_. */
|
||||
static bool
|
||||
donor_priority_less (const struct list_elem *a_, const struct list_elem *b_,
|
||||
void *aux UNUSED)
|
||||
{
|
||||
struct thread *a = list_entry (a_, struct thread, donor_elem);
|
||||
struct thread *b = list_entry (b_, struct thread, donor_elem);
|
||||
|
||||
return a->priority < b->priority;
|
||||
}
|
||||
|
||||
/* Sets the current thread's base priority to new_base_priority.
|
||||
Updates the current thread's effective priority if necessary. */
|
||||
void
|
||||
thread_set_priority (int new_base_priority)
|
||||
{
|
||||
|
||||
if (thread_mlfqs)
|
||||
return;
|
||||
|
||||
ASSERT (new_base_priority >= PRI_MIN);
|
||||
ASSERT (new_base_priority <= PRI_MAX);
|
||||
|
||||
struct thread *t = thread_current ();
|
||||
|
||||
/* If the base priority is unchanged, do nothing. */
|
||||
if (new_base_priority == t->base_priority)
|
||||
return;
|
||||
|
||||
t->base_priority = new_base_priority;
|
||||
thread_recalculate_priority ();
|
||||
|
||||
thread_yield ();
|
||||
}
|
||||
|
||||
/* Returns the current thread's effective priority. */
|
||||
int
|
||||
thread_get_priority (void)
|
||||
{
|
||||
return thread_current ()->priority;
|
||||
}
|
||||
|
||||
|
||||
/* Updates recent_cpu for a thread. */
|
||||
static void
|
||||
thread_update_recent_cpu (struct thread *t, void *aux UNUSED)
|
||||
@@ -420,6 +473,31 @@ thread_update_recent_cpu (struct thread *t, void *aux UNUSED)
|
||||
= 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);
|
||||
|
||||
/* Recalculates the effective priority of the current thread. */
|
||||
void
|
||||
thread_recalculate_priority (void)
|
||||
{
|
||||
struct thread *t = thread_current ();
|
||||
|
||||
enum intr_level old_level = intr_disable ();
|
||||
t->priority = t->base_priority;
|
||||
|
||||
/* If there are no donors to the current thread, then the effective
|
||||
priority is just the base priority. */
|
||||
if (!list_empty (&t->donors_list))
|
||||
{
|
||||
int max_donated_priority =
|
||||
list_entry (list_max (&t->donors_list, donor_priority_less, NULL),
|
||||
struct thread, donor_elem)->priority;
|
||||
|
||||
/* The effective priority is the max donated priority if this is
|
||||
higher than the base priority. */
|
||||
if (max_donated_priority > t->priority)
|
||||
t->priority = max_donated_priority;
|
||||
}
|
||||
intr_set_level (old_level);
|
||||
|
||||
}
|
||||
|
||||
/* Sets the current thread's nice value to NICE. */
|
||||
@@ -458,6 +536,22 @@ thread_get_recent_cpu (void)
|
||||
return fp_round (fp_mul_int (thread_current ()->recent_cpu, 100));
|
||||
}
|
||||
|
||||
/* Reinsert thread t into the ready list at its correct position
|
||||
in descending order of priority. Used when this thread's priority
|
||||
may have changed. Must be called with interrupts disabled. */
|
||||
void
|
||||
ready_list_reinsert (struct thread *t)
|
||||
{
|
||||
ASSERT (intr_get_level () == INTR_OFF);
|
||||
|
||||
/* If the thread isn't ready to run, do nothing. */
|
||||
if (t->status != THREAD_READY)
|
||||
return;
|
||||
|
||||
list_remove (&t->elem);
|
||||
list_insert_ordered (&ready_list, &t->elem, priority_more, NULL);
|
||||
}
|
||||
|
||||
/* Idle thread. Executes when no other thread is ready to run.
|
||||
|
||||
The idle thread is initially put on the ready list by
|
||||
@@ -544,11 +638,19 @@ init_thread (struct thread *t, const char *name, int nice, int priority,
|
||||
t->status = THREAD_BLOCKED;
|
||||
strlcpy (t->name, name, sizeof t->name);
|
||||
t->stack = (uint8_t *) t + PGSIZE;
|
||||
|
||||
t->priority
|
||||
= thread_mlfqs ? calculate_bsd_priority (recent_cpu, nice) : priority;
|
||||
t->nice = nice;
|
||||
t->recent_cpu = recent_cpu;
|
||||
|
||||
t->base_priority = priority;
|
||||
|
||||
t->magic = THREAD_MAGIC;
|
||||
|
||||
list_init (&t->donors_list);
|
||||
t->priority = t->base_priority;
|
||||
t->waiting_lock = NULL;
|
||||
|
||||
old_level = intr_disable ();
|
||||
list_push_back (&all_list, &t->allelem);
|
||||
|
||||
Reference in New Issue
Block a user