92d1aa899efe1d8bf84c6c7056ae7d45215c687b
[pintos-anon] / src / threads / thread.c
1 #include "threads/thread.h"
2 #include <debug.h>
3 #include <stddef.h>
4 #include <random.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include "threads/flags.h"
8 #include "threads/interrupt.h"
9 #include "threads/intr-stubs.h"
10 #include "threads/palloc.h"
11 #include "threads/switch.h"
12 #include "threads/synch.h"
13 #include "threads/vaddr.h"
14 #ifdef USERPROG
15 #include "userprog/process.h"
16 #endif
17
18 /* Random value for struct thread's `magic' member.
19    Used to detect stack overflow.  See the big comment at the top
20    of thread.h for details. */
21 #define THREAD_MAGIC 0xcd6abf4b
22
23 /* List of processes in THREAD_READY state, that is, processes
24    that are ready to run but not actually running. */
25 static struct list ready_list;
26
27 /* Idle thread. */
28 static struct thread *idle_thread;
29
30 /* Initial thread, the thread running init.c:main(). */
31 static struct thread *initial_thread;
32
33 /* Lock used by allocate_tid(). */
34 static struct lock tid_lock;
35
36 /* Stack frame for kernel_thread(). */
37 struct kernel_thread_frame 
38   {
39     void *eip;                  /* Return address. */
40     thread_func *function;      /* Function to call. */
41     void *aux;                  /* Auxiliary data for function. */
42   };
43
44 /* Statistics. */
45 static long long idle_ticks;    /* # of timer ticks spent idle. */
46 static long long kernel_ticks;  /* # of timer ticks in kernel threads. */
47 static long long user_ticks;    /* # of timer ticks in user programs. */
48
49 /* Scheduling. */
50 #define TIME_SLICE 4            /* # of timer ticks to give each thread. */
51 static unsigned thread_ticks;   /* # of timer ticks since last yield. */
52
53 /* If false (default), use round-robin scheduler.
54    If true, use multi-level feedback queue scheduler.
55    Controlled by kernel command-line option "-o mlfqs". */
56 bool thread_mlfqs;
57
58 static void kernel_thread (thread_func *, void *aux);
59
60 static void idle (void *aux UNUSED);
61 static struct thread *running_thread (void);
62 static struct thread *next_thread_to_run (void);
63 static void init_thread (struct thread *, const char *name, int priority);
64 static bool is_thread (struct thread *) UNUSED;
65 static void *alloc_frame (struct thread *, size_t size);
66 static void schedule (void);
67 void schedule_tail (struct thread *prev);
68 static tid_t allocate_tid (void);
69
70 /* Initializes the threading system by transforming the code
71    that's currently running into a thread.  This can't work in
72    general and it is possible in this case only because loader.S
73    was careful to put the bottom of the stack at a page boundary.
74
75    Also initializes the run queue and the tid lock.
76
77    After calling this function, be sure to initialize the page
78    allocator before trying to create any threads with
79    thread_create().
80
81    It is not safe to call thread_current() until this function
82    finishes. */
83 void
84 thread_init (void) 
85 {
86   ASSERT (intr_get_level () == INTR_OFF);
87
88   lock_init (&tid_lock);
89   list_init (&ready_list);
90
91   /* Set up a thread structure for the running thread. */
92   initial_thread = running_thread ();
93   init_thread (initial_thread, "main", PRI_DEFAULT);
94   initial_thread->status = THREAD_RUNNING;
95   initial_thread->tid = allocate_tid ();
96 }
97
98 /* Starts preemptive thread scheduling by enabling interrupts.
99    Also creates the idle thread. */
100 void
101 thread_start (void) 
102 {
103   /* Create the idle thread. */
104   struct semaphore idle_started;
105   sema_init (&idle_started, 0);
106   thread_create ("idle", PRI_MIN, idle, &idle_started);
107
108   /* Start preemptive thread scheduling. */
109   intr_enable ();
110
111   /* Wait for the idle thread to initialize idle_thread. */
112   sema_down (&idle_started);
113 }
114
115 /* Called by the timer interrupt handler at each timer tick.
116    Thus, this function runs in an external interrupt context. */
117 void
118 thread_tick (void) 
119 {
120   struct thread *t = thread_current ();
121
122   /* Update statistics. */
123   if (t == idle_thread)
124     idle_ticks++;
125 #ifdef USERPROG
126   else if (t->pagedir != NULL)
127     user_ticks++;
128 #endif
129   else
130     kernel_ticks++;
131
132   /* Enforce preemption. */
133   if (++thread_ticks >= TIME_SLICE)
134     intr_yield_on_return ();
135 }
136
137 /* Prints thread statistics. */
138 void
139 thread_print_stats (void) 
140 {
141   printf ("Thread: %lld idle ticks, %lld kernel ticks, %lld user ticks\n",
142           idle_ticks, kernel_ticks, user_ticks);
143 }
144
145 /* Creates a new kernel thread named NAME with the given initial
146    PRIORITY, which executes FUNCTION passing AUX as the argument,
147    and adds it to the ready queue.  Returns the thread identifier
148    for the new thread, or TID_ERROR if creation fails.
149
150    If thread_start() has been called, then the new thread may be
151    scheduled before thread_create() returns.  It could even exit
152    before thread_create() returns.  Contrariwise, the original
153    thread may run for any amount of time before the new thread is
154    scheduled.  Use a semaphore or some other form of
155    synchronization if you need to ensure ordering.
156
157    The code provided sets the new thread's `priority' member to
158    PRIORITY, but no actual priority scheduling is implemented.
159    Priority scheduling is the goal of Problem 1-3. */
160 tid_t
161 thread_create (const char *name, int priority,
162                thread_func *function, void *aux) 
163 {
164   struct thread *t;
165   struct kernel_thread_frame *kf;
166   struct switch_entry_frame *ef;
167   struct switch_threads_frame *sf;
168   tid_t tid;
169
170   ASSERT (function != NULL);
171
172   /* Allocate thread. */
173   t = palloc_get_page (PAL_ZERO);
174   if (t == NULL)
175     return TID_ERROR;
176
177   /* Initialize thread. */
178   init_thread (t, name, priority);
179   tid = t->tid = allocate_tid ();
180
181   /* Stack frame for kernel_thread(). */
182   kf = alloc_frame (t, sizeof *kf);
183   kf->eip = NULL;
184   kf->function = function;
185   kf->aux = aux;
186
187   /* Stack frame for switch_entry(). */
188   ef = alloc_frame (t, sizeof *ef);
189   ef->eip = (void (*) (void)) kernel_thread;
190
191   /* Stack frame for switch_threads(). */
192   sf = alloc_frame (t, sizeof *sf);
193   sf->eip = switch_entry;
194
195   /* Add to run queue. */
196   thread_unblock (t);
197
198   return tid;
199 }
200
201 /* Puts the current thread to sleep.  It will not be scheduled
202    again until awoken by thread_unblock().
203
204    This function must be called with interrupts turned off.  It
205    is usually a better idea to use one of the synchronization
206    primitives in synch.h. */
207 void
208 thread_block (void) 
209 {
210   ASSERT (!intr_context ());
211   ASSERT (intr_get_level () == INTR_OFF);
212
213   thread_current ()->status = THREAD_BLOCKED;
214   schedule ();
215 }
216
217 /* Transitions a blocked thread T to the ready-to-run state.
218    This is an error if T is not blocked.  (Use thread_yield() to
219    make the running thread ready.)
220
221    This function does not preempt the running thread.  This can
222    be important: if the caller had disabled interrupts itself,
223    it may expect that it can atomically unblock a thread and
224    update other data. */
225 void
226 thread_unblock (struct thread *t) 
227 {
228   enum intr_level old_level;
229
230   ASSERT (is_thread (t));
231
232   old_level = intr_disable ();
233   ASSERT (t->status == THREAD_BLOCKED);
234   list_push_back (&ready_list, &t->elem);
235   t->status = THREAD_READY;
236   intr_set_level (old_level);
237 }
238
239 /* Returns the name of the running thread. */
240 const char *
241 thread_name (void) 
242 {
243   return thread_current ()->name;
244 }
245
246 /* Returns the running thread.
247    This is running_thread() plus a couple of sanity checks.
248    See the big comment at the top of thread.h for details. */
249 struct thread *
250 thread_current (void) 
251 {
252   struct thread *t = running_thread ();
253   
254   /* Make sure T is really a thread.
255      If either of these assertions fire, then your thread may
256      have overflowed its stack.  Each thread has less than 4 kB
257      of stack, so a few big automatic arrays or moderate
258      recursion can cause stack overflow. */
259   ASSERT (is_thread (t));
260   ASSERT (t->status == THREAD_RUNNING);
261
262   return t;
263 }
264
265 /* Returns the running thread's tid. */
266 tid_t
267 thread_tid (void) 
268 {
269   return thread_current ()->tid;
270 }
271
272 /* Deschedules the current thread and destroys it.  Never
273    returns to the caller. */
274 void
275 thread_exit (void) 
276 {
277   ASSERT (!intr_context ());
278
279 #ifdef USERPROG
280   process_exit ();
281 #endif
282
283   /* Just set our status to dying and schedule another process.
284      We will be destroyed during the call to schedule_tail(). */
285   intr_disable ();
286   thread_current ()->status = THREAD_DYING;
287   schedule ();
288   NOT_REACHED ();
289 }
290
291 /* Yields the CPU.  The current thread is not put to sleep and
292    may be scheduled again immediately at the scheduler's whim. */
293 void
294 thread_yield (void) 
295 {
296   struct thread *cur = thread_current ();
297   enum intr_level old_level;
298   
299   ASSERT (!intr_context ());
300
301   old_level = intr_disable ();
302   if (cur != idle_thread) 
303     list_push_back (&ready_list, &cur->elem);
304   cur->status = THREAD_READY;
305   schedule ();
306   intr_set_level (old_level);
307 }
308
309 /* Sets the current thread's priority to NEW_PRIORITY. */
310 void
311 thread_set_priority (int new_priority) 
312 {
313   thread_current ()->priority = new_priority;
314 }
315
316 /* Returns the current thread's priority. */
317 int
318 thread_get_priority (void) 
319 {
320   return thread_current ()->priority;
321 }
322
323 /* Sets the current thread's nice value to NICE. */
324 void
325 thread_set_nice (int nice UNUSED) 
326 {
327   /* Not yet implemented. */
328 }
329
330 /* Returns the current thread's nice value. */
331 int
332 thread_get_nice (void) 
333 {
334   /* Not yet implemented. */
335   return 0;
336 }
337
338 /* Returns 100 times the system load average. */
339 int
340 thread_get_load_avg (void) 
341 {
342   /* Not yet implemented. */
343   return 0;
344 }
345
346 /* Returns 100 times the current thread's recent_cpu value. */
347 int
348 thread_get_recent_cpu (void) 
349 {
350   /* Not yet implemented. */
351   return 0;
352 }
353 \f
354 /* Idle thread.  Executes when no other thread is ready to run.
355
356    The idle thread is initially put on the ready list by
357    thread_start().  It will be scheduled once initially, at which
358    point it initializes idle_thread, "up"s the semaphore passed
359    to it to enable thread_start() to continue, and immediately
360    blocks.  After that, the idle thread never appears in the
361    ready list.  It is returned by next_thread_to_run() as a
362    special case when the ready list is empty. */
363 static void
364 idle (void *idle_started_ UNUSED) 
365 {
366   struct semaphore *idle_started = idle_started_;
367   idle_thread = thread_current ();
368   sema_up (idle_started);
369
370   for (;;) 
371     {
372       /* Let someone else run. */
373       intr_disable ();
374       thread_block ();
375
376       /* Re-enable interrupts and wait for the next one.
377
378          The `sti' instruction disables interrupts until the
379          completion of the next instruction, so these two
380          instructions are executed atomically.  This atomicity is
381          important; otherwise, an interrupt could be handled
382          between re-enabling interrupts and waiting for the next
383          one to occur, wasting as much as one clock tick worth of
384          time.
385
386          See [IA32-v2a] "HLT", [IA32-v2b] "STI", and [IA32-v3a]
387          7.11.1 "HLT Instruction". */
388       asm volatile ("sti; hlt" : : : "memory");
389     }
390 }
391
392 /* Function used as the basis for a kernel thread. */
393 static void
394 kernel_thread (thread_func *function, void *aux) 
395 {
396   ASSERT (function != NULL);
397
398   intr_enable ();       /* The scheduler runs with interrupts off. */
399   function (aux);       /* Execute the thread function. */
400   thread_exit ();       /* If function() returns, kill the thread. */
401 }
402 \f
403 /* Returns the running thread. */
404 struct thread *
405 running_thread (void) 
406 {
407   uint32_t *esp;
408
409   /* Copy the CPU's stack pointer into `esp', and then round that
410      down to the start of a page.  Because `struct thread' is
411      always at the beginning of a page and the stack pointer is
412      somewhere in the middle, this locates the curent thread. */
413   asm ("mov %%esp, %0" : "=g" (esp));
414   return pg_round_down (esp);
415 }
416
417 /* Returns true if T appears to point to a valid thread. */
418 static bool
419 is_thread (struct thread *t)
420 {
421   return t != NULL && t->magic == THREAD_MAGIC;
422 }
423
424 /* Does basic initialization of T as a blocked thread named
425    NAME. */
426 static void
427 init_thread (struct thread *t, const char *name, int priority)
428 {
429   ASSERT (t != NULL);
430   ASSERT (PRI_MIN <= priority && priority <= PRI_MAX);
431   ASSERT (name != NULL);
432
433   memset (t, 0, sizeof *t);
434   t->status = THREAD_BLOCKED;
435   strlcpy (t->name, name, sizeof t->name);
436   t->stack = (uint8_t *) t + PGSIZE;
437   t->priority = priority;
438   t->magic = THREAD_MAGIC;
439 }
440
441 /* Allocates a SIZE-byte frame at the top of thread T's stack and
442    returns a pointer to the frame's base. */
443 static void *
444 alloc_frame (struct thread *t, size_t size) 
445 {
446   /* Stack data is always allocated in word-size units. */
447   ASSERT (is_thread (t));
448   ASSERT (size % sizeof (uint32_t) == 0);
449
450   t->stack -= size;
451   return t->stack;
452 }
453
454 /* Chooses and returns the next thread to be scheduled.  Should
455    return a thread from the run queue, unless the run queue is
456    empty.  (If the running thread can continue running, then it
457    will be in the run queue.)  If the run queue is empty, return
458    idle_thread. */
459 static struct thread *
460 next_thread_to_run (void) 
461 {
462   if (list_empty (&ready_list))
463     return idle_thread;
464   else
465     return list_entry (list_pop_front (&ready_list), struct thread, elem);
466 }
467
468 /* Completes a thread switch by activating the new thread's page
469    tables, and, if the previous thread is dying, destroying it.
470
471    At this function's invocation, we just switched from thread
472    PREV, the new thread is already running, and interrupts are
473    still disabled.  This function is normally invoked by
474    thread_schedule() as its final action before returning, but
475    the first time a thread is scheduled it is called by
476    switch_entry() (see switch.S).
477
478    It's not safe to call printf() until the thread switch is
479    complete.  In practice that means that printf()s should be
480    added at the end of the function.
481
482    After this function and its caller returns, the thread switch
483    is complete. */
484 void
485 schedule_tail (struct thread *prev) 
486 {
487   struct thread *cur = running_thread ();
488   
489   ASSERT (intr_get_level () == INTR_OFF);
490
491   /* Mark us as running. */
492   cur->status = THREAD_RUNNING;
493
494   /* Start new time slice. */
495   thread_ticks = 0;
496
497 #ifdef USERPROG
498   /* Activate the new address space. */
499   process_activate ();
500 #endif
501
502   /* If the thread we switched from is dying, destroy its struct
503      thread.  This must happen late so that thread_exit() doesn't
504      pull out the rug under itself.  (We don't free
505      initial_thread because its memory was not obtained via
506      palloc().) */
507   if (prev != NULL && prev->status == THREAD_DYING && prev != initial_thread) 
508     {
509       ASSERT (prev != cur);
510       palloc_free_page (prev);
511     }
512 }
513
514 /* Schedules a new process.  At entry, interrupts must be off and
515    the running process's state must have been changed from
516    running to some other state.  This function finds another
517    thread to run and switches to it.
518
519    It's not safe to call printf() until schedule_tail() has
520    completed. */
521 static void
522 schedule (void) 
523 {
524   struct thread *cur = running_thread ();
525   struct thread *next = next_thread_to_run ();
526   struct thread *prev = NULL;
527
528   ASSERT (intr_get_level () == INTR_OFF);
529   ASSERT (cur->status != THREAD_RUNNING);
530   ASSERT (is_thread (next));
531
532   if (cur != next)
533     prev = switch_threads (cur, next);
534   schedule_tail (prev); 
535 }
536
537 /* Returns a tid to use for a new thread. */
538 static tid_t
539 allocate_tid (void) 
540 {
541   static tid_t next_tid = 1;
542   tid_t tid;
543
544   lock_acquire (&tid_lock);
545   tid = next_tid++;
546   lock_release (&tid_lock);
547
548   return tid;
549 }
550 \f
551 /* Offset of `stack' member within `struct thread'.
552    Used by switch.S, which can't figure it out on its own. */
553 uint32_t thread_stack_ofs = offsetof (struct thread, stack);