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 (void (*function) (void *aux), void *aux)
31 ASSERT (function != NULL);
37 static struct thread *
38 new_thread (const char *name)
42 ASSERT (name != NULL);
44 t = palloc_get (PAL_ZERO);
47 strlcpy (t->name, name, sizeof t->name);
48 t->stack = (uint8_t *) t + PGSIZE;
49 t->status = THREAD_BLOCKED;
56 alloc_frame (struct thread *t, size_t size)
58 ASSERT (size % sizeof (uint32_t) == 0);
65 thread_create (const char *name, void (*function) (void *aux), void *aux)
68 struct thread_root_frame *rf;
69 struct switch_thunk_frame *tf;
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 switch_thunk(). */
83 tf = alloc_frame (t, sizeof *tf);
84 tf->eip = (void (*) (void)) thread_root;
86 /* Stack frame for thread_switch(). */
87 sf = alloc_frame (t, sizeof *sf);
88 sf->eip = (void (*) (void)) switch_thunk;
90 /* Add to run queue. */
100 asm ("movl %%esp, %0\n" : "=g" (esp));
101 return pg_round_down (esp);
106 thread_execute (const char *filename)
108 struct thread *t = new_thread (filename);
112 if (!addrspace_load (&t->addrspace, filename))
113 panic ("%s: program load failed", filename);
114 printk ("%s: loaded\n", filename);
120 thread_ready (struct thread *t)
122 if (t->status != THREAD_READY)
124 list_push_back (&run_queue, &t->rq_elem);
125 t->status = THREAD_READY;
129 static struct thread *
130 find_next_to_run (void)
132 if (list_empty (&run_queue))
135 return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
147 thread_destroy (struct thread *t)
149 ASSERT (t->status == THREAD_DYING);
150 ASSERT (t != thread_current ());
156 thread_schedule (void)
158 struct thread *cur, *next, *prev;
160 ASSERT (intr_get_level () == IF_OFF);
162 cur = thread_current ();
163 ASSERT (cur->status != THREAD_RUNNING);
165 while ((next = find_next_to_run ()) == NULL)
168 next->status = THREAD_RUNNING;
169 prev = switch_threads (cur, next);
171 /* Prevent GCC from reordering anything around the thread
173 asm volatile ("" : : : "memory");
176 addrspace_activate (&cur->addrspace);
179 if (prev != NULL && prev->status == THREAD_DYING)
180 thread_destroy (prev);
188 ASSERT (!intr_context ());
191 thread_ready (thread_current ());
196 thread_start (struct thread *t)
198 ASSERT (intr_get_level () == IF_OFF);
200 if (t->status == THREAD_READY)
201 list_remove (&t->rq_elem);
202 t->status = THREAD_RUNNING;
203 switch_threads (NULL, t);
209 ASSERT (!intr_context ());
212 thread_current ()->status = THREAD_DYING;
219 ASSERT (!intr_context ());
220 ASSERT (intr_get_level () == IF_OFF);
222 thread_current ()->status = THREAD_BLOCKED;
227 tfunc (void *aux UNUSED)
232 if (random_ulong () % 5 == 0)
234 printk ("%s exiting\n", thread_current ()->name);
237 count = random_ulong () % 25 * 10000;
238 printk ("%s waiting %zu: ", thread_current ()->name, count);
239 for (i = 0; i < count; i++);
240 printk ("%s\n", thread_current ()->name);
245 thread_self_test (void)
250 for (i = 0; i < 4; i++)
255 t = thread_create (name, tfunc, NULL);