5 #include "intr-stubs.h"
12 uint32_t thread_stack_ofs = offsetof (struct thread, stack);
14 static struct list run_queue;
19 list_init (&run_queue);
22 struct thread_root_frame
24 void *eip; /* Return address. */
25 void (*function) (void *); /* Function to call. */
26 void *aux; /* Auxiliary data for function. */
30 thread_root (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_entry_frame *ef;
71 struct switch_threads_frame *sf;
73 ASSERT (function != NULL);
75 t = new_thread (name);
77 /* Stack frame for thread_root(). */
78 rf = alloc_frame (t, sizeof *rf);
80 rf->function = function;
83 /* Stack frame for switch_entry(). */
84 ef = alloc_frame (t, sizeof *ef);
85 ef->eip = (void (*) (void)) thread_root;
87 /* Stack frame for thread_switch(). */
88 sf = alloc_frame (t, sizeof *sf);
89 sf->eip = switch_entry;
91 /* Add to run queue. */
101 asm ("movl %%esp, %0\n" : "=g" (esp));
102 return pg_round_down (esp);
107 thread_execute (const char *filename)
110 struct intr_frame *if_;
111 struct switch_entry_frame *ef;
112 struct switch_threads_frame *sf;
113 void (*start) (void);
115 ASSERT (filename != NULL);
117 t = new_thread (filename);
121 if (!addrspace_load (&t->addrspace, filename, &start))
122 panic ("%s: program load failed", filename);
124 /* Interrupt frame. */
125 if_ = alloc_frame (t, sizeof *if_);
130 if_->eflags = FLAG_IF | 2;
131 if_->esp = PHYS_BASE;
134 /* Stack frame for switch_entry(). */
135 ef = alloc_frame (t, sizeof *ef);
138 /* Stack frame for thread_switch(). */
139 sf = alloc_frame (t, sizeof *sf);
140 sf->eip = switch_entry;
142 /* Add to run queue. */
150 thread_ready (struct thread *t)
152 if (t->status != THREAD_READY)
154 list_push_back (&run_queue, &t->rq_elem);
155 t->status = THREAD_READY;
159 static struct thread *
160 find_next_to_run (void)
162 if (list_empty (&run_queue))
165 return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
177 thread_destroy (struct thread *t)
179 ASSERT (t->status == THREAD_DYING);
180 ASSERT (t != thread_current ());
185 void schedule_tail (struct thread *prev);
188 schedule_tail (struct thread *prev)
190 struct thread *cur = thread_current ();
192 ASSERT (intr_get_level () == IF_OFF);
195 addrspace_activate (&cur->addrspace);
198 if (prev != NULL && prev->status == THREAD_DYING)
199 thread_destroy (prev);
203 thread_schedule (void)
205 struct thread *cur, *next, *prev;
207 ASSERT (intr_get_level () == IF_OFF);
209 cur = thread_current ();
210 ASSERT (cur->status != THREAD_RUNNING);
212 while ((next = find_next_to_run ()) == NULL)
215 next->status = THREAD_RUNNING;
218 prev = switch_threads (cur, next);
220 /* Prevent GCC from reordering anything around the thread
222 asm volatile ("" : : : "memory");
224 schedule_tail (prev);
231 enum if_level old_level;
233 ASSERT (!intr_context ());
235 old_level = intr_disable ();
236 thread_ready (thread_current ());
238 intr_set_level (old_level);
242 thread_start (struct thread *t)
244 ASSERT (intr_get_level () == IF_OFF);
246 if (t->status == THREAD_READY)
247 list_remove (&t->rq_elem);
248 t->status = THREAD_RUNNING;
249 switch_threads (NULL, t);
255 ASSERT (!intr_context ());
258 thread_current ()->status = THREAD_DYING;
266 ASSERT (!intr_context ());
267 ASSERT (intr_get_level () == IF_OFF);
269 thread_current ()->status = THREAD_BLOCKED;
274 tfunc (void *aux UNUSED)
279 if (random_ulong () % 5 == 0)
281 printk ("%s exiting\n", thread_current ()->name);
284 count = random_ulong () % 25 * 10000;
285 printk ("%s waiting %zu: ", thread_current ()->name, count);
286 for (i = 0; i < count; i++);
287 printk ("%s\n", thread_current ()->name);
292 thread_self_test (void)
297 for (i = 0; i < 4; i++)
302 t = thread_create (name, tfunc, NULL);