Merge branch 'task1/priority-scheduling' into task1/merged/priority-scheduling
# Conflicts: # .gitignore # src/threads/synch.c # src/threads/thread.c
This commit is contained in:
@@ -114,21 +114,12 @@ sema_up (struct semaphore *sema)
|
|||||||
ASSERT (sema != NULL);
|
ASSERT (sema != NULL);
|
||||||
|
|
||||||
old_level = intr_disable ();
|
old_level = intr_disable ();
|
||||||
|
|
||||||
/* Wake up (unblock) the highest priority thread from the waiters list */
|
|
||||||
struct thread *t = NULL;
|
|
||||||
if (!list_empty (&sema->waiters))
|
if (!list_empty (&sema->waiters))
|
||||||
{
|
thread_unblock (list_entry (list_pop_front (&sema->waiters),
|
||||||
t = list_entry (list_pop_front (&sema->waiters), struct thread, elem);
|
struct thread, elem));
|
||||||
thread_unblock (t);
|
|
||||||
}
|
|
||||||
|
|
||||||
sema->value++;
|
sema->value++;
|
||||||
intr_set_level (old_level);
|
intr_set_level (old_level);
|
||||||
|
|
||||||
/* If the unblocked thread has higher priority than the current running thread
|
|
||||||
then yield the CPU to the unblocked thread */
|
|
||||||
if (!intr_context() && t != NULL && t->priority > thread_get_priority ())
|
|
||||||
thread_yield ();
|
thread_yield ();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,14 +346,7 @@ cond_wait (struct condition *cond, struct lock *lock)
|
|||||||
ASSERT (lock_held_by_current_thread (lock));
|
ASSERT (lock_held_by_current_thread (lock));
|
||||||
|
|
||||||
sema_init (&waiter.semaphore, 0);
|
sema_init (&waiter.semaphore, 0);
|
||||||
|
list_push_back (&cond->waiters, &waiter.elem);
|
||||||
/* Insert the semaphore_elem into the waiters list in order of priority.
|
|
||||||
We pass the priority of the current thread as aux to sema_priority_more
|
|
||||||
because the thread has not been sema'd down yet (See sema_priority_more) */
|
|
||||||
int priority = thread_current ()->priority;
|
|
||||||
list_insert_ordered (&cond->waiters, &waiter.elem, sema_priority_more,
|
|
||||||
&priority);
|
|
||||||
|
|
||||||
lock_release (lock);
|
lock_release (lock);
|
||||||
sema_down (&waiter.semaphore);
|
sema_down (&waiter.semaphore);
|
||||||
lock_acquire (lock);
|
lock_acquire (lock);
|
||||||
|
|||||||
@@ -219,9 +219,6 @@ thread_create (const char *name, int priority,
|
|||||||
|
|
||||||
/* Add to run queue. */
|
/* Add to run queue. */
|
||||||
thread_unblock (t);
|
thread_unblock (t);
|
||||||
|
|
||||||
/* Yield if the new thread has a higher priority than the current thread. */
|
|
||||||
if (priority > thread_get_priority ())
|
|
||||||
thread_yield ();
|
thread_yield ();
|
||||||
|
|
||||||
return tid;
|
return tid;
|
||||||
@@ -386,32 +383,9 @@ thread_set_priority (int new_priority)
|
|||||||
|
|
||||||
thread_current ()->priority = new_priority;
|
thread_current ()->priority = new_priority;
|
||||||
|
|
||||||
enum intr_level old_level = intr_disable ();
|
if (new_priority < old_priority)
|
||||||
|
|
||||||
/* If the thread is in the ready list, the list must be reordered to maintain
|
|
||||||
the priority order. */
|
|
||||||
if (thread_current ()->status == THREAD_READY) {
|
|
||||||
/* Remove from the ready list and reinsert it in priority order. */
|
|
||||||
list_remove (&thread_current ()->elem);
|
|
||||||
list_insert_ordered (&ready_list, &thread_current ()->elem, priority_more,
|
|
||||||
NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_priority < old_priority && !list_empty (&ready_list)) {
|
|
||||||
/* If the new priority is lower than the old priority, check if the current
|
|
||||||
thread no longer has the highest priority. If it doesn't, yield the CPU.
|
|
||||||
*/
|
|
||||||
|
|
||||||
struct thread *next_thread =
|
|
||||||
list_entry (list_front (&ready_list), struct thread, elem);
|
|
||||||
|
|
||||||
if (next_thread->priority > new_priority) {
|
|
||||||
thread_yield ();
|
thread_yield ();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
intr_set_level (old_level);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns the current thread's priority. */
|
/* Returns the current thread's priority. */
|
||||||
int
|
int
|
||||||
|
|||||||
@@ -141,4 +141,7 @@ void thread_set_nice (int);
|
|||||||
int thread_get_recent_cpu (void);
|
int thread_get_recent_cpu (void);
|
||||||
int thread_get_load_avg (void);
|
int thread_get_load_avg (void);
|
||||||
|
|
||||||
|
/* Returns true iff the priority of the first list element's thread is greater
|
||||||
|
than that of the second list element's thread. */
|
||||||
|
list_less_func thread_priority_greater;
|
||||||
#endif /* threads/thread.h */
|
#endif /* threads/thread.h */
|
||||||
|
|||||||
Reference in New Issue
Block a user