5 #include "intr-stubs.h"
12 uint32_t thread_stack_ofs = offsetof (struct thread, stack);
14 static struct list run_queue;
15 static struct thread *idle_thread;
18 idle (void *aux UNUSED)
22 /* Wait for an interrupt. */
25 /* Let someone else run. */
35 list_init (&run_queue);
36 idle_thread = thread_create ("idle", idle, NULL);
39 struct thread_root_frame
41 void *eip; /* Return address. */
42 void (*function) (void *); /* Function to call. */
43 void *aux; /* Auxiliary data for function. */
47 thread_root (void (*function) (void *aux), void *aux)
49 ASSERT (function != NULL);
56 static struct thread *
57 new_thread (const char *name)
61 ASSERT (name != NULL);
63 t = palloc_get (PAL_ZERO);
66 strlcpy (t->name, name, sizeof t->name);
67 t->stack = (uint8_t *) t + PGSIZE;
68 t->status = THREAD_BLOCKED;
75 alloc_frame (struct thread *t, size_t size)
77 ASSERT (size % sizeof (uint32_t) == 0);
84 thread_create (const char *name, void (*function) (void *aux), void *aux)
87 struct thread_root_frame *rf;
88 struct switch_entry_frame *ef;
89 struct switch_threads_frame *sf;
91 ASSERT (function != NULL);
93 t = new_thread (name);
95 /* Stack frame for thread_root(). */
96 rf = alloc_frame (t, sizeof *rf);
98 rf->function = function;
101 /* Stack frame for switch_entry(). */
102 ef = alloc_frame (t, sizeof *ef);
103 ef->eip = (void (*) (void)) thread_root;
105 /* Stack frame for thread_switch(). */
106 sf = alloc_frame (t, sizeof *sf);
107 sf->eip = switch_entry;
109 /* Add to run queue. */
116 thread_current (void)
119 asm ("movl %%esp, %0\n" : "=g" (esp));
120 return pg_round_down (esp);
125 thread_execute (const char *filename)
128 struct intr_frame *if_;
129 struct switch_entry_frame *ef;
130 struct switch_threads_frame *sf;
131 void (*start) (void);
133 ASSERT (filename != NULL);
135 t = new_thread (filename);
139 if (!addrspace_load (&t->addrspace, filename, &start))
140 panic ("%s: program load failed", filename);
142 /* Interrupt frame. */
143 if_ = alloc_frame (t, sizeof *if_);
148 if_->eflags = FLAG_IF | 2;
149 if_->esp = PHYS_BASE;
152 /* Stack frame for switch_entry(). */
153 ef = alloc_frame (t, sizeof *ef);
156 /* Stack frame for thread_switch(). */
157 sf = alloc_frame (t, sizeof *sf);
158 sf->eip = switch_entry;
160 /* Add to run queue. */
168 thread_ready (struct thread *t)
170 if (t->status != THREAD_READY)
172 list_push_back (&run_queue, &t->rq_elem);
173 t->status = THREAD_READY;
177 static struct thread *
178 find_next_to_run (void)
180 if (list_empty (&run_queue))
183 return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
187 thread_destroy (struct thread *t)
189 ASSERT (t->status == THREAD_DYING);
190 ASSERT (t != thread_current ());
195 void schedule_tail (struct thread *prev);
198 schedule_tail (struct thread *prev)
200 struct thread *cur = thread_current ();
202 ASSERT (intr_get_level () == IF_OFF);
205 addrspace_activate (&cur->addrspace);
208 if (prev != NULL && prev->status == THREAD_DYING)
209 thread_destroy (prev);
213 thread_schedule (void)
215 struct thread *cur, *next, *prev;
217 ASSERT (intr_get_level () == IF_OFF);
219 cur = thread_current ();
220 ASSERT (cur->status != THREAD_RUNNING);
222 next = find_next_to_run ();
226 next->status = THREAD_RUNNING;
229 prev = switch_threads (cur, next);
231 /* Prevent GCC from reordering anything around the thread
233 asm volatile ("" : : : "memory");
235 schedule_tail (prev);
242 enum if_level old_level;
244 ASSERT (!intr_context ());
246 old_level = intr_disable ();
247 thread_ready (thread_current ());
249 intr_set_level (old_level);
253 thread_start (struct thread *t)
255 ASSERT (intr_get_level () == IF_OFF);
257 if (t->status == THREAD_READY)
258 list_remove (&t->rq_elem);
259 t->status = THREAD_RUNNING;
260 switch_threads (NULL, t);
267 ASSERT (!intr_context ());
270 thread_current ()->status = THREAD_DYING;
278 ASSERT (!intr_context ());
279 ASSERT (intr_get_level () == IF_OFF);
281 thread_current ()->status = THREAD_BLOCKED;
286 tfunc (void *aux UNUSED)
291 if (random_ulong () % 5 == 0)
293 printk ("%s exiting\n", thread_current ()->name);
296 count = random_ulong () % 25 * 10000;
297 printk ("%s waiting %zu: ", thread_current ()->name, count);
298 for (i = 0; i < count; i++);
299 printk ("%s\n", thread_current ()->name);
304 thread_self_test (void)
309 for (i = 0; i < 4; i++)
314 t = thread_create (name, tfunc, NULL);