X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fthreads%2Fthread.c;h=1b56f98508e16922de88d299cf02ec2f6f0a22f1;hb=fcf89ebe9fe61d0a1739bd48b7dd90905aab461d;hp=52f0f8d4fc73d8f65818163aaca9c7229cf8e97e;hpb=b413e78bacc4a0331191e581cd060281ba47c54a;p=pintos-anon diff --git a/src/threads/thread.c b/src/threads/thread.c index 52f0f8d..1b56f98 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -1,15 +1,15 @@ -#include "thread.h" +#include "threads/thread.h" +#include #include -#include "debug.h" -#include "interrupt.h" -#include "intr-stubs.h" -#include "lib.h" -#include "mmu.h" -#include "palloc.h" -#include "random.h" -#include "switch.h" +#include +#include +#include "threads/interrupt.h" +#include "threads/intr-stubs.h" +#include "threads/mmu.h" +#include "threads/palloc.h" +#include "threads/switch.h" #ifdef USERPROG -#include "gdt.h" +#include "userprog/gdt.h" #endif /* Value for struct thread's `magic' member. @@ -19,12 +19,16 @@ /* List of processes in THREAD_READY state, that is, processes that are ready to run but not actually running. */ -static struct list run_queue; +static struct list ready_list; /* Idle thread. */ static struct thread *idle_thread; /* Thread. */ static void idle (void *aux UNUSED); /* Thread function. */ +/* Initial thread. + This is the thread running main(). */ +static struct thread *initial_thread; + /* Stack frame for kernel_thread(). */ struct kernel_thread_frame { @@ -57,17 +61,15 @@ void schedule_tail (struct thread *prev); void thread_init (void) { - struct thread *t; - ASSERT (intr_get_level () == INTR_OFF); /* Set up a thread structure for the running thread. */ - t = running_thread (); - init_thread (t, "main"); - t->status = THREAD_RUNNING; + initial_thread = running_thread (); + init_thread (initial_thread, "main"); + initial_thread->status = THREAD_RUNNING; /* Initialize run queue. */ - list_init (&run_queue); + list_init (&ready_list); } /* Starts preemptive thread scheduling by enabling interrupts. @@ -182,7 +184,7 @@ thread_unblock (struct thread *t) old_level = intr_disable (); if (t->status == THREAD_BLOCKED) { - list_push_back (&run_queue, &t->rq_elem); + list_push_back (&ready_list, &t->elem); t->status = THREAD_READY; } intr_set_level (old_level); @@ -241,7 +243,7 @@ thread_yield (void) ASSERT (!intr_context ()); old_level = intr_disable (); - list_push_back (&run_queue, &cur->rq_elem); + list_push_back (&ready_list, &cur->elem); cur->status = THREAD_READY; schedule (); intr_set_level (old_level); @@ -361,10 +363,10 @@ alloc_frame (struct thread *t, size_t size) static struct thread * next_thread_to_run (void) { - if (list_empty (&run_queue)) + if (list_empty (&ready_list)) return idle_thread; else - return list_entry (list_pop_front (&run_queue), struct thread, rq_elem); + return list_entry (list_pop_front (&ready_list), struct thread, elem); } /* Destroys T, which must be in the dying state and must not be @@ -379,7 +381,8 @@ destroy_thread (struct thread *t) #ifdef USERPROG addrspace_destroy (t); #endif - palloc_free (t); + if (t != initial_thread) + palloc_free (t); } /* Completes a thread switch by activating the new thread's page