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. */
33 thread_init (const char *name, void (*function) (void *aux), void *aux)
35 struct thread *initial_thread;
37 ASSERT (intr_get_level () == IF_OFF);
39 list_init (&run_queue);
40 idle_thread = thread_create ("idle", idle, NULL);
42 initial_thread = thread_create (name, function, aux);
43 list_remove (&initial_thread->rq_elem);
44 initial_thread->status = THREAD_RUNNING;
45 switch_threads (NULL, initial_thread);
50 struct kernel_thread_frame
52 void *eip; /* Return address. */
53 void (*function) (void *); /* Function to call. */
54 void *aux; /* Auxiliary data for function. */
58 kernel_thread (void (*function) (void *aux), void *aux)
60 ASSERT (function != NULL);
67 static struct thread *
68 new_thread (const char *name)
72 ASSERT (name != NULL);
74 t = palloc_get (PAL_ZERO);
77 strlcpy (t->name, name, sizeof t->name);
78 t->stack = (uint8_t *) t + PGSIZE;
79 t->status = THREAD_BLOCKED;
86 alloc_frame (struct thread *t, size_t size)
88 ASSERT (size % sizeof (uint32_t) == 0);
95 thread_create (const char *name, void (*function) (void *aux), void *aux)
98 struct kernel_thread_frame *kf;
99 struct switch_entry_frame *ef;
100 struct switch_threads_frame *sf;
102 ASSERT (function != NULL);
104 t = new_thread (name);
106 /* Stack frame for kernel_thread(). */
107 kf = alloc_frame (t, sizeof *kf);
109 kf->function = function;
112 /* Stack frame for switch_entry(). */
113 ef = alloc_frame (t, sizeof *ef);
114 ef->eip = (void (*) (void)) kernel_thread;
116 /* Stack frame for thread_switch(). */
117 sf = alloc_frame (t, sizeof *sf);
118 sf->eip = switch_entry;
120 /* Add to run queue. */
127 thread_current (void)
130 asm ("movl %%esp, %0\n" : "=g" (esp));
131 return pg_round_down (esp);
136 thread_execute (const char *filename)
139 struct intr_frame *if_;
140 struct switch_entry_frame *ef;
141 struct switch_threads_frame *sf;
142 void (*start) (void);
144 ASSERT (filename != NULL);
146 t = new_thread (filename);
150 if (!addrspace_load (&t->addrspace, filename, &start))
151 panic ("%s: program load failed", filename);
153 /* Interrupt frame. */
154 if_ = alloc_frame (t, sizeof *if_);
159 if_->eflags = FLAG_IF | 2;
160 if_->esp = PHYS_BASE;
163 /* Stack frame for switch_entry(). */
164 ef = alloc_frame (t, sizeof *ef);
167 /* Stack frame for thread_switch(). */
168 sf = alloc_frame (t, sizeof *sf);
169 sf->eip = switch_entry;
171 /* Add to run queue. */
179 thread_ready (struct thread *t)
181 if (t->status != THREAD_READY)
183 list_push_back (&run_queue, &t->rq_elem);
184 t->status = THREAD_READY;
188 static struct thread *
189 find_next_to_run (void)
191 if (list_empty (&run_queue))
194 return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
198 thread_destroy (struct thread *t)
200 ASSERT (t->status == THREAD_DYING);
201 ASSERT (t != thread_current ());
206 void schedule_tail (struct thread *prev);
209 schedule_tail (struct thread *prev)
211 ASSERT (intr_get_level () == IF_OFF);
214 addrspace_activate (&thread_current ()->addrspace);
217 if (prev != NULL && prev->status == THREAD_DYING)
218 thread_destroy (prev);
222 thread_schedule (void)
224 struct thread *cur, *next, *prev;
226 ASSERT (intr_get_level () == IF_OFF);
228 cur = thread_current ();
229 ASSERT (cur->status != THREAD_RUNNING);
231 next = find_next_to_run ();
235 next->status = THREAD_RUNNING;
238 prev = switch_threads (cur, next);
240 /* Prevent GCC from reordering anything around the thread
242 asm volatile ("" : : : "memory");
244 schedule_tail (prev);
251 enum if_level old_level;
253 ASSERT (!intr_context ());
255 old_level = intr_disable ();
256 thread_ready (thread_current ());
258 intr_set_level (old_level);
264 ASSERT (!intr_context ());
267 thread_current ()->status = THREAD_DYING;
275 ASSERT (!intr_context ());
276 ASSERT (intr_get_level () == IF_OFF);
278 thread_current ()->status = THREAD_BLOCKED;