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);
39 static struct thread *
40 new_thread (const char *name)
44 ASSERT (name != NULL);
46 t = palloc_get (PAL_ZERO);
49 strlcpy (t->name, name, sizeof t->name);
50 t->stack = (uint8_t *) t + PGSIZE;
51 t->status = THREAD_BLOCKED;
58 alloc_frame (struct thread *t, size_t size)
60 ASSERT (size % sizeof (uint32_t) == 0);
67 thread_create (const char *name, void (*function) (void *aux), void *aux)
70 struct thread_root_frame *rf;
71 struct switch_entry_frame *ef;
72 struct switch_threads_frame *sf;
74 ASSERT (function != NULL);
76 t = new_thread (name);
78 /* Stack frame for thread_root(). */
79 rf = alloc_frame (t, sizeof *rf);
81 rf->function = function;
84 /* Stack frame for switch_entry(). */
85 ef = alloc_frame (t, sizeof *ef);
86 ef->eip = (void (*) (void)) thread_root;
88 /* Stack frame for thread_switch(). */
89 sf = alloc_frame (t, sizeof *sf);
90 sf->eip = switch_entry;
92 /* Add to run queue. */
102 asm ("movl %%esp, %0\n" : "=g" (esp));
103 return pg_round_down (esp);
108 thread_execute (const char *filename)
111 struct intr_frame *if_;
112 struct switch_entry_frame *ef;
113 struct switch_threads_frame *sf;
114 void (*start) (void);
116 ASSERT (filename != NULL);
118 t = new_thread (filename);
122 if (!addrspace_load (&t->addrspace, filename, &start))
123 panic ("%s: program load failed", filename);
125 /* Interrupt frame. */
126 if_ = alloc_frame (t, sizeof *if_);
131 if_->eflags = FLAG_IF | 2;
132 if_->esp = PHYS_BASE;
135 /* Stack frame for switch_entry(). */
136 ef = alloc_frame (t, sizeof *ef);
139 /* Stack frame for thread_switch(). */
140 sf = alloc_frame (t, sizeof *sf);
141 sf->eip = switch_entry;
143 /* Add to run queue. */
151 thread_ready (struct thread *t)
153 if (t->status != THREAD_READY)
155 list_push_back (&run_queue, &t->rq_elem);
156 t->status = THREAD_READY;
160 static struct thread *
161 find_next_to_run (void)
163 if (list_empty (&run_queue))
166 return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
178 thread_destroy (struct thread *t)
180 ASSERT (t->status == THREAD_DYING);
181 ASSERT (t != thread_current ());
186 void schedule_tail (struct thread *prev);
189 schedule_tail (struct thread *prev)
191 struct thread *cur = thread_current ();
193 ASSERT (intr_get_level () == IF_OFF);
196 addrspace_activate (&cur->addrspace);
199 if (prev != NULL && prev->status == THREAD_DYING)
200 thread_destroy (prev);
204 thread_schedule (void)
206 struct thread *cur, *next, *prev;
208 ASSERT (intr_get_level () == IF_OFF);
210 cur = thread_current ();
211 ASSERT (cur->status != THREAD_RUNNING);
213 while ((next = find_next_to_run ()) == NULL)
216 next->status = THREAD_RUNNING;
219 prev = switch_threads (cur, next);
221 /* Prevent GCC from reordering anything around the thread
223 asm volatile ("" : : : "memory");
225 schedule_tail (prev);
232 enum if_level old_level;
234 ASSERT (!intr_context ());
236 old_level = intr_disable ();
237 thread_ready (thread_current ());
239 intr_set_level (old_level);
243 thread_start (struct thread *t)
245 ASSERT (intr_get_level () == IF_OFF);
247 if (t->status == THREAD_READY)
248 list_remove (&t->rq_elem);
249 t->status = THREAD_RUNNING;
250 switch_threads (NULL, t);
257 ASSERT (!intr_context ());
260 thread_current ()->status = THREAD_DYING;
268 ASSERT (!intr_context ());
269 ASSERT (intr_get_level () == IF_OFF);
271 thread_current ()->status = THREAD_BLOCKED;
276 tfunc (void *aux UNUSED)
281 if (random_ulong () % 5 == 0)
283 printk ("%s exiting\n", thread_current ()->name);
286 count = random_ulong () % 25 * 10000;
287 printk ("%s waiting %zu: ", thread_current ()->name, count);
288 for (i = 0; i < count; i++);
289 printk ("%s\n", thread_current ()->name);
294 thread_self_test (void)
299 for (i = 0; i < 4; i++)
304 t = thread_create (name, tfunc, NULL);