From edbf01ab5f8bca57472150a02543971da231eec1 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 31 Aug 2004 05:57:18 +0000 Subject: [PATCH] Clean up threads.c. --- src/threads/init.c | 18 ++-------- src/threads/thread.c | 83 ++++++++++++-------------------------------- src/threads/thread.h | 9 ++--- 3 files changed, 26 insertions(+), 84 deletions(-) diff --git a/src/threads/init.c b/src/threads/init.c index caacaee..03e0bb8 100644 --- a/src/threads/init.c +++ b/src/threads/init.c @@ -28,7 +28,6 @@ size_t ram_pages; static void ram_init (void); static void gdt_init (void); static void argv_init (void); -void power_off (void); static void main_thread (void *aux UNUSED) @@ -66,8 +65,7 @@ main (void) kbd_init (); /* Do everything else in a system thread. */ - thread_init (); - thread_start (thread_create ("main", main_thread, NULL)); + thread_init ("main", main_thread, NULL); } static uint64_t @@ -113,7 +111,7 @@ make_tss_desc (void *vaddr) 0x67, SYS_SYSTEM, TYPE_TSS_32_A, 0, GRAN_BYTE); } -uint64_t gdt[SEL_CNT]; +static uint64_t gdt[SEL_CNT]; struct tss *tss; @@ -183,15 +181,3 @@ argv_init (void) } argv[argc] = NULL; } - -void -power_off (void) -{ - const char s[] = "Shutdown"; - const char *p; - - printk ("Powering off...\n"); - for (p = s; *p != '\0'; p++) - outb (0x8900, *p); - for (;;); -} diff --git a/src/threads/thread.c b/src/threads/thread.c index 218305c..54f803f 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -30,13 +30,24 @@ idle (void *aux UNUSED) } void -thread_init (void) +thread_init (const char *name, void (*function) (void *aux), void *aux) { + struct thread *initial_thread; + + ASSERT (intr_get_level () == IF_OFF); + list_init (&run_queue); idle_thread = thread_create ("idle", idle, NULL); + + initial_thread = thread_create (name, function, aux); + list_remove (&initial_thread->rq_elem); + initial_thread->status = THREAD_RUNNING; + switch_threads (NULL, initial_thread); + + NOT_REACHED (); } -struct thread_root_frame +struct kernel_thread_frame { void *eip; /* Return address. */ void (*function) (void *); /* Function to call. */ @@ -44,7 +55,7 @@ struct thread_root_frame }; static void -thread_root (void (*function) (void *aux), void *aux) +kernel_thread (void (*function) (void *aux), void *aux) { ASSERT (function != NULL); @@ -84,7 +95,7 @@ struct thread * thread_create (const char *name, void (*function) (void *aux), void *aux) { struct thread *t; - struct thread_root_frame *rf; + struct kernel_thread_frame *kf; struct switch_entry_frame *ef; struct switch_threads_frame *sf; @@ -92,15 +103,15 @@ thread_create (const char *name, void (*function) (void *aux), void *aux) t = new_thread (name); - /* Stack frame for thread_root(). */ - rf = alloc_frame (t, sizeof *rf); - rf->eip = NULL; - rf->function = function; - rf->aux = aux; + /* Stack frame for kernel_thread(). */ + kf = alloc_frame (t, sizeof *kf); + kf->eip = NULL; + kf->function = function; + kf->aux = aux; /* Stack frame for switch_entry(). */ ef = alloc_frame (t, sizeof *ef); - ef->eip = (void (*) (void)) thread_root; + ef->eip = (void (*) (void)) kernel_thread; /* Stack frame for thread_switch(). */ sf = alloc_frame (t, sizeof *sf); @@ -197,14 +208,10 @@ void schedule_tail (struct thread *prev); void schedule_tail (struct thread *prev) { -#ifdef USERPROG - struct thread *cur = thread_current (); -#endif - ASSERT (intr_get_level () == IF_OFF); #ifdef USERPROG - addrspace_activate (&cur->addrspace); + addrspace_activate (&thread_current ()->addrspace); #endif if (prev != NULL && prev->status == THREAD_DYING) @@ -251,18 +258,6 @@ thread_yield (void) intr_set_level (old_level); } -void -thread_start (struct thread *t) -{ - ASSERT (intr_get_level () == IF_OFF); - - if (t->status == THREAD_READY) - list_remove (&t->rq_elem); - t->status = THREAD_RUNNING; - switch_threads (NULL, t); - NOT_REACHED (); -} - void thread_exit (void) { @@ -283,37 +278,3 @@ thread_sleep (void) thread_current ()->status = THREAD_BLOCKED; thread_schedule (); } - -static void -tfunc (void *aux UNUSED) -{ - for (;;) - { - size_t count, i; - if (random_ulong () % 5 == 0) - { - printk ("%s exiting\n", thread_current ()->name); - break; - } - count = random_ulong () % 25 * 10000; - printk ("%s waiting %zu: ", thread_current ()->name, count); - for (i = 0; i < count; i++); - printk ("%s\n", thread_current ()->name); - } -} - -void -thread_self_test (void) -{ - struct thread *t; - int i; - - for (i = 0; i < 4; i++) - { - char name[2]; - name[0] = 'a' + i; - name[1] = 0; - t = thread_create (name, tfunc, NULL); - } - thread_start (t); -} diff --git a/src/threads/thread.h b/src/threads/thread.h index efa3a02..4d65e1b 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -28,10 +28,8 @@ struct thread #endif }; -void thread_init (void); - -struct thread *thread_create (const char *name, - void (*function) (void *aux), void *aux); +void thread_init (const char *name, void (*) (void *aux), void *) NO_RETURN; +struct thread *thread_create (const char *name, void (*) (void *aux), void *); void thread_destroy (struct thread *); struct thread *thread_current (void); @@ -39,13 +37,10 @@ struct thread *thread_current (void); bool thread_execute (const char *filename); #endif -void thread_start (struct thread *) NO_RETURN; void thread_ready (struct thread *); void thread_exit (void) NO_RETURN; void thread_yield (void); void thread_sleep (void); -void thread_self_test (void); - #endif /* thread.h */ -- 2.30.2