11 uint32_t thread_stack_ofs = offsetof (struct thread, stack);
13 static struct list run_queue;
18 list_init (&run_queue);
21 struct thread_root_frame
23 void *eip; /* Return address. */
24 void (*function) (void *); /* Function to call. */
25 void *aux; /* Auxiliary data for function. */
29 thread_root (struct thread *cur UNUSED, struct thread *next UNUSED,
30 void (*function) (void *aux), void *aux)
32 ASSERT (function != NULL);
38 static struct thread *
39 new_thread (const char *name)
43 ASSERT (name != NULL);
45 t = palloc_get (PAL_ZERO);
48 strlcpy (t->name, name, sizeof t->name);
49 t->stack = (uint8_t *) t + PGSIZE;
50 t->status = THREAD_BLOCKED;
57 alloc_frame (struct thread *t, size_t size)
59 ASSERT (size % sizeof (uint32_t) == 0);
66 thread_create (const char *name, void (*function) (void *aux), void *aux)
69 struct thread_root_frame *rf;
70 struct switch_frame *sf;
72 ASSERT (function != NULL);
74 t = new_thread (name);
76 /* Stack frame for thread_root(). */
77 rf = alloc_frame (t, sizeof *rf);
79 rf->function = function;
82 /* Stack frame for thread_switch(). */
83 sf = alloc_frame (t, sizeof *sf);
84 sf->eip = (void (*) (void)) thread_root;
86 /* Add to run queue. */
96 asm ("movl %%esp, %0\n" : "=g" (esp));
97 return pg_round_down (esp);
102 thread_execute (const char *filename)
104 struct thread *t = new_thread (filename);
108 if (!addrspace_load (&t->addrspace, filename))
109 panic ("%s: program load failed", filename);
110 printk ("%s: loaded\n", filename);
116 thread_ready (struct thread *t)
118 if (t->status != THREAD_READY)
120 list_push_back (&run_queue, &t->rq_elem);
121 t->status = THREAD_READY;
125 static struct thread *
126 find_next_to_run (void)
128 if (list_empty (&run_queue))
131 return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
143 thread_destroy (struct thread *t)
145 ASSERT (t->status == THREAD_DYING);
146 ASSERT (t != thread_current ());
152 thread_schedule (void)
154 struct thread *cur, *next, *prev;
156 ASSERT (intr_get_level () == IF_OFF);
158 cur = thread_current ();
159 ASSERT (cur->status != THREAD_RUNNING);
161 while ((next = find_next_to_run ()) == NULL)
164 next->status = THREAD_RUNNING;
165 prev = thread_switch (cur, next);
167 /* Prevent GCC from reordering anything around the thread
169 asm volatile ("" : : : "memory");
172 addrspace_activate (&cur->addrspace);
175 if (prev != NULL && prev->status == THREAD_DYING)
176 thread_destroy (prev);
184 ASSERT (!intr_context ());
187 thread_ready (thread_current ());
192 thread_start (struct thread *t)
194 ASSERT (intr_get_level () == IF_OFF);
196 if (t->status == THREAD_READY)
197 list_remove (&t->rq_elem);
198 t->status = THREAD_RUNNING;
199 thread_switch (NULL, t);
205 ASSERT (!intr_context ());
208 thread_current ()->status = THREAD_DYING;
215 ASSERT (!intr_context ());
216 ASSERT (intr_get_level () == IF_OFF);
218 thread_current ()->status = THREAD_BLOCKED;
223 tfunc (void *aux UNUSED)
228 if (random_ulong () % 5 == 0)
230 printk ("%s exiting\n", thread_current ()->name);
233 count = random_ulong () % 25 * 10000;
234 printk ("%s waiting %zu: ", thread_current ()->name, count);
235 for (i = 0; i < count; i++);
236 printk ("%s\n", thread_current ()->name);
241 thread_self_test (void)
246 for (i = 0; i < 4; i++)
251 t = thread_create (name, tfunc, NULL);