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:
sBubshait
2024-10-17 19:30:19 +01:00
3 changed files with 12 additions and 51 deletions

View File

@@ -219,10 +219,7 @@ thread_create (const char *name, int priority,
/* Add to run queue. */
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;
}
@@ -335,7 +332,7 @@ thread_yield (void)
old_level = intr_disable ();
/* Insert the thread back into the ready list in priority order. */
if (cur != idle_thread)
if (cur != idle_thread)
list_insert_ordered(&ready_list, &cur->elem, priority_more, NULL);
cur->status = THREAD_READY;
@@ -386,31 +383,8 @@ thread_set_priority (int new_priority)
thread_current ()->priority = new_priority;
enum intr_level old_level = intr_disable ();
/* 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();
}
}
intr_set_level (old_level);
if (new_priority < old_priority)
thread_yield ();
}
/* Returns the current thread's priority. */