uint32_t thread_stack_ofs = offsetof (struct thread, stack);
static struct list run_queue;
+static struct thread *idle_thread;
+
+static void
+idle (void *aux UNUSED)
+{
+ for (;;)
+ {
+ /* Wait for an interrupt. */
+ asm ("hlt");
+
+ /* Let someone else run. */
+ intr_disable ();
+ thread_sleep ();
+ intr_enable ();
+ }
+}
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. */
};
static void
-thread_root (void (*function) (void *aux), void *aux)
+kernel_thread (void (*function) (void *aux), void *aux)
{
ASSERT (function != NULL);
-
+
+ intr_enable ();
function (aux);
thread_exit ();
}
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;
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);
return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
}
-static void
-idle (void)
-{
- static int idle = 0;
- if (idle++ == 0)
- printk ("idle\n");
-}
-
void
thread_destroy (struct thread *t)
{
void
schedule_tail (struct thread *prev)
{
- struct thread *cur = thread_current ();
+ 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)
thread_destroy (prev);
-
- intr_enable ();
}
-void
+static void
thread_schedule (void)
{
struct thread *cur, *next, *prev;
cur = thread_current ();
ASSERT (cur->status != THREAD_RUNNING);
- while ((next = find_next_to_run ()) == NULL)
- idle ();
+ next = find_next_to_run ();
+ if (next == NULL)
+ next = idle_thread;
next->status = THREAD_RUNNING;
if (cur != next)
void
thread_yield (void)
{
+ enum if_level old_level;
+
ASSERT (!intr_context ());
- intr_disable ();
+ old_level = intr_disable ();
thread_ready (thread_current ());
thread_schedule ();
-}
-
-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);
+ intr_set_level (old_level);
}
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);
-}