1 diff -u src/devices/timer.c~ src/devices/timer.c
2 --- src/devices/timer.c~ 2005-05-24 15:52:43.000000000 -0700
3 +++ src/devices/timer.c 2005-05-26 15:19:20.000000000 -0700
4 @@ -23,6 +23,9 @@ static volatile int64_t ticks;
5 Initialized by timer_calibrate(). */
6 static unsigned loops_per_tick;
8 +/* Threads waiting in timer_sleep(). */
9 +static struct list wait_list;
11 static intr_handler_func timer_interrupt;
12 static bool too_many_loops (unsigned loops);
13 static void busy_wait (int64_t loops);
14 @@ -43,6 +46,8 @@ timer_init (void)
15 outb (0x40, count >> 8);
17 intr_register_ext (0x20, timer_interrupt, "8254 Timer");
19 + list_init (&wait_list);
22 /* Calibrates loops_per_tick, used to implement brief delays. */
23 @@ -87,15 +92,36 @@ timer_elapsed (int64_t then)
24 return timer_ticks () - then;
27 +/* Compares two threads based on their wake-up times. */
29 +compare_threads_by_wakeup_time (const struct list_elem *a_,
30 + const struct list_elem *b_,
33 + const struct thread *a = list_entry (a_, struct thread, timer_elem);
34 + const struct thread *b = list_entry (b_, struct thread, timer_elem);
36 + return a->wakeup_time < b->wakeup_time;
39 /* Suspends execution for approximately TICKS timer ticks. */
41 timer_sleep (int64_t ticks)
43 - int64_t start = timer_ticks ();
44 + struct thread *t = thread_current ();
46 + /* Schedule our wake-up time. */
47 + t->wakeup_time = timer_ticks () + ticks;
49 + /* Atomically insert the current thread into the wait list. */
50 ASSERT (intr_get_level () == INTR_ON);
51 - while (timer_elapsed (start) < ticks)
54 + list_insert_ordered (&wait_list, &t->timer_elem,
55 + compare_threads_by_wakeup_time, NULL);
59 + sema_down (&t->timer_sema);
62 /* Suspends execution for approximately MS milliseconds. */
63 @@ -132,6 +158,16 @@ timer_interrupt (struct intr_frame *args
68 + while (!list_empty (&wait_list))
70 + struct thread *t = list_entry (list_front (&wait_list),
71 + struct thread, timer_elem);
72 + if (ticks < t->wakeup_time)
74 + sema_up (&t->timer_sema);
75 + list_pop_front (&wait_list);
79 /* Returns true if LOOPS iterations waits for more than one timer
80 diff -u src/threads/fixed-point.h~ src/threads/fixed-point.h
81 --- src/threads/fixed-point.h~ 1969-12-31 16:00:00.000000000 -0800
82 +++ src/threads/fixed-point.h 2005-06-02 14:11:21.000000000 -0700
84 +#ifndef THREADS_FIXED_POINT_H
85 +#define THREADS_FIXED_POINT_H
90 +#define FIX_BITS 32 /* Total bits per fixed-point number. */
91 +#define FIX_P 16 /* Number of integer bits. */
92 +#define FIX_Q 16 /* Number of fractional bits. */
93 +#define FIX_F (1 << FIX_Q) /* pow(2, FIX_Q). */
95 +#define FIX_MIN_INT (-FIX_MAX_INT) /* Smallest representable integer. */
96 +#define FIX_MAX_INT ((1 << FIX_P) - 1) /* Largest representable integer. */
98 +/* A fixed-point number. */
105 +/* Returns a fixed-point number with F as its internal value. */
106 +static inline fixed_point_t
114 +/* Returns fixed-point number corresponding to integer N. */
115 +static inline fixed_point_t
118 + ASSERT (n >= FIX_MIN_INT && n <= FIX_MAX_INT);
119 + return __mk_fix (n * FIX_F);
122 +/* Returns fixed-point number corresponding to N divided by D. */
123 +static inline fixed_point_t
124 +fix_frac (int n, int d)
127 + ASSERT (n / d >= FIX_MIN_INT && n / d <= FIX_MAX_INT);
128 + return __mk_fix ((long long) n * FIX_F / d);
131 +/* Returns X rounded to the nearest integer. */
133 +fix_round (fixed_point_t x)
135 + return (x.f + FIX_F / 2) / FIX_F;
138 +/* Returns X truncated down to the nearest integer. */
140 +fix_trunc (fixed_point_t x)
142 + return x.f / FIX_F;
145 +/* Returns X + Y. */
146 +static inline fixed_point_t
147 +fix_add (fixed_point_t x, fixed_point_t y)
149 + return __mk_fix (x.f + y.f);
152 +/* Returns X - Y. */
153 +static inline fixed_point_t
154 +fix_sub (fixed_point_t x, fixed_point_t y)
156 + return __mk_fix (x.f - y.f);
159 +/* Returns X * Y. */
160 +static inline fixed_point_t
161 +fix_mul (fixed_point_t x, fixed_point_t y)
163 + return __mk_fix ((long long) x.f * y.f / FIX_F);
166 +/* Returns X * N. */
167 +static inline fixed_point_t
168 +fix_scale (fixed_point_t x, int n)
171 + return __mk_fix (x.f * n);
174 +/* Returns X / Y. */
175 +static inline fixed_point_t
176 +fix_div (fixed_point_t x, fixed_point_t y)
178 + return __mk_fix ((long long) x.f * FIX_F / y.f);
181 +/* Returns X / N. */
182 +static inline fixed_point_t
183 +fix_unscale (fixed_point_t x, int n)
186 + return __mk_fix (x.f / n);
189 +/* Returns 1 / X. */
190 +static inline fixed_point_t
191 +fix_inv (fixed_point_t x)
193 + return fix_div (fix_int (1), x);
196 +/* Returns -1 if X < Y, 0 if X == Y, 1 if X > Y. */
198 +fix_compare (fixed_point_t x, fixed_point_t y)
200 + return x.f < y.f ? -1 : x.f > y.f;
203 +#endif /* threads/fixed-point.h */
204 diff -u src/threads/synch.c~ src/threads/synch.c
205 --- src/threads/synch.c~ 2005-05-24 20:47:28.000000000 -0700
206 +++ src/threads/synch.c 2005-06-02 14:20:15.000000000 -0700
207 @@ -114,10 +114,28 @@ sema_up (struct semaphore *sema)
208 ASSERT (sema != NULL);
210 old_level = intr_disable ();
211 - if (!list_empty (&sema->waiters))
212 - thread_unblock (list_entry (list_pop_front (&sema->waiters),
213 - struct thread, elem));
215 + if (!list_empty (&sema->waiters))
217 + /* Find highest-priority waiting thread. */
218 + struct thread *max = list_entry (list_max (&sema->waiters,
219 + thread_lower_priority, NULL),
220 + struct thread, elem);
222 + /* Remove `max' from wait list and unblock. */
223 + list_remove (&max->elem);
224 + thread_unblock (max);
226 + /* Yield to a higher-priority thread, if we're running in a
227 + context where it makes sense to do so.
229 + Kind of a funny interaction with donation here.
230 + We only support donation for locks, and locks turn off
231 + interrupts before calling us, so we automatically don't
232 + do the yield here, delegating to lock_release(). */
233 + if (!intr_context () && old_level == INTR_ON)
234 + thread_yield_to_higher_priority ();
236 intr_set_level (old_level);
239 @@ -200,8 +218,29 @@ lock_acquire (struct lock *lock)
240 lock_acquire (struct lock *lock)
242 + enum intr_level old_level;
244 ASSERT (lock != NULL);
245 ASSERT (!intr_context ());
246 ASSERT (!lock_held_by_current_thread (lock));
248 + old_level = intr_disable ();
250 + if (lock->holder != NULL)
252 + /* Donate our priority to the thread holding the lock.
253 + First, update the data structures. */
254 + struct thread *donor = thread_current ();
255 + donor->want_lock = lock;
256 + donor->donee = lock->holder;
257 + list_push_back (&lock->holder->donors, &donor->donor_elem);
259 + /* Now implement the priority donation itself
260 + by recomputing the donee's priority
261 + and cascading the donation as far as necessary. */
262 + if (donor->donee != NULL)
263 + thread_recompute_priority (donor->donee);
266 sema_down (&lock->semaphore);
267 lock->holder = thread_current ();
268 + intr_set_level (old_level);
269 @@ -238,9 +273,37 @@ void
270 lock_release (struct lock *lock)
272 + enum intr_level old_level;
273 + struct thread *t = thread_current ();
274 + struct list_elem *e;
276 ASSERT (lock != NULL);
277 ASSERT (lock_held_by_current_thread (lock));
279 + old_level = intr_disable ();
281 + /* Return donations to threads that want this lock. */
282 + for (e = list_begin (&t->donors); e != list_end (&t->donors); )
284 + struct thread *donor = list_entry (e, struct thread, donor_elem);
285 + if (donor->want_lock == lock)
287 + donor->donee = NULL;
288 + e = list_remove (e);
294 + /* Release lock. */
296 sema_up (&lock->semaphore);
298 + /* Recompute our priority based on our remaining donations,
299 + then yield to a higher-priority ready thread if one now
301 + thread_recompute_priority (t);
302 + thread_yield_to_higher_priority ();
304 + intr_set_level (old_level);
307 @@ -264,6 +323,7 @@ struct semaphore_elem
309 struct list_elem elem; /* List element. */
310 struct semaphore semaphore; /* This semaphore. */
311 + struct thread *thread; /* Thread. */
314 /* Initializes condition variable COND. A condition variable
315 @@ -308,12 +368,26 @@ cond_wait (struct condition *cond, struc
316 ASSERT (lock_held_by_current_thread (lock));
318 sema_init (&waiter.semaphore, 0);
319 + waiter.thread = thread_current ();
320 list_push_back (&cond->waiters, &waiter.elem);
322 sema_down (&waiter.semaphore);
327 +semaphore_elem_lower_priority (const struct list_elem *a_,
328 + const struct list_elem *b_,
331 + const struct semaphore_elem *a
332 + = list_entry (a_, struct semaphore_elem, elem);
333 + const struct semaphore_elem *b
334 + = list_entry (b_, struct semaphore_elem, elem);
336 + return a->thread->priority < b->thread->priority;
339 /* If any threads are waiting on COND (protected by LOCK), then
340 this function signals one of them to wake up from its wait.
341 LOCK must be held before calling this function.
342 @@ -330,8 +404,12 @@ cond_signal (struct condition *cond, str
343 ASSERT (lock_held_by_current_thread (lock));
345 if (!list_empty (&cond->waiters))
346 - sema_up (&list_entry (list_pop_front (&cond->waiters),
347 - struct semaphore_elem, elem)->semaphore);
349 + struct list_elem *max
350 + = list_max (&cond->waiters, semaphore_elem_lower_priority, NULL);
352 + sema_up (&list_entry (max, struct semaphore_elem, elem)->semaphore);
356 /* Wakes up all threads, if any, waiting on COND (protected by
357 diff -u src/threads/thread.c~ src/threads/thread.c
358 --- src/threads/thread.c~ 2005-06-02 14:35:12.000000000 -0700
359 +++ src/threads/thread.c 2005-06-02 14:55:56.000000000 -0700
363 #include "threads/flags.h"
364 +#include "threads/init.h"
365 #include "threads/interrupt.h"
366 #include "threads/intr-stubs.h"
367 #include "threads/palloc.h"
368 #include "threads/switch.h"
369 #include "threads/synch.h"
370 +#include "devices/timer.h"
371 #include "threads/vaddr.h"
373 #include "userprog/process.h"
376 that are ready to run but not actually running. */
377 static struct list ready_list;
379 +/* List of all threads. */
380 +static struct list all_threads_list;
383 static struct thread *idle_thread;
385 @@ -49,6 +54,7 @@ static long long user_ticks; /* # of
387 #define TIME_SLICE 4 /* # of timer ticks to give each thread. */
388 static unsigned thread_ticks; /* # of timer ticks since last yield. */
389 +static fixed_point_t load_avg; /* Load average. */
391 static void kernel_thread (thread_func *, void *aux);
393 @@ -79,12 +85,15 @@ thread_init (void)
395 lock_init (&tid_lock);
396 list_init (&ready_list);
397 + list_init (&all_threads_list);
398 + load_avg = fix_int (0);
400 /* Set up a thread structure for the running thread. */
401 initial_thread = running_thread ();
402 init_thread (initial_thread, "main", PRI_DEFAULT);
403 initial_thread->status = THREAD_RUNNING;
404 initial_thread->tid = allocate_tid ();
405 + list_push_front (&all_threads_list, &initial_thread->all_elem);
408 /* Starts preemptive thread scheduling by enabling interrupts.
409 @@ -101,9 +110,48 @@ void
413 - /* Enforce preemption. */
414 - if (++thread_ticks >= TIME_SLICE)
415 - intr_yield_on_return ();
418 + /* Update load average. */
419 + if (timer_ticks () % TIMER_FREQ == 0)
421 + size_t ready_threads = list_size (&ready_list);
422 + if (t != idle_thread)
425 + load_avg = fix_add (fix_mul (fix_frac (59, 60), load_avg),
426 + fix_mul (fix_frac (1, 60), fix_int (ready_threads)));
429 + /* Increment running process's recent_cpu. */
430 + if (t != idle_thread)
431 + t->recent_cpu = fix_add (t->recent_cpu, fix_int (1));
433 + /* Update recent_cpu and thread priorities once per second. */
434 + if (timer_ticks () % TIMER_FREQ == 0)
436 + struct list_elem *e;
437 + fixed_point_t twice_load = fix_scale (load_avg, 2);
438 + fixed_point_t twice_load_plus_1 = fix_add (twice_load, fix_int (1));
439 + fixed_point_t load_factor = fix_div (twice_load, twice_load_plus_1);
440 + for (e = list_begin (&all_threads_list);
441 + e != list_end (&all_threads_list); e = list_next (e))
443 + struct thread *t = list_entry (e, struct thread, all_elem);
444 + t->recent_cpu = fix_add (fix_mul (load_factor, t->recent_cpu),
445 + fix_int (t->nice));
446 + thread_recompute_priority (t);
451 + /* Switch threads if time slice has expired. */
452 + if (++thread_ticks >= TIME_SLICE)
455 + thread_recompute_priority (thread_current ());
456 + intr_yield_on_return ();
460 /* Prints thread statistics. */
461 @@ -143,10 +191,12 @@ tid_t
462 thread_create (const char *name, int priority,
463 thread_func *function, void *aux)
465 + struct thread *cur = thread_current ();
467 struct kernel_thread_frame *kf;
468 struct switch_entry_frame *ef;
469 struct switch_threads_frame *sf;
470 + enum intr_level old_level;
473 ASSERT (function != NULL);
474 @@ -157,8 +207,10 @@ thread_create (const char *name, int pri
477 /* Initialize thread. */
478 - init_thread (t, name, priority);
479 + init_thread (t, name, enable_mlfqs ? cur->priority : priority);
480 tid = t->tid = allocate_tid ();
481 + t->nice = cur->nice;
482 + t->recent_cpu = cur->recent_cpu;
484 /* Stack frame for kernel_thread(). */
485 kf = alloc_frame (t, sizeof *kf);
486 @@ -174,8 +226,15 @@ thread_create (const char *name, int pri
487 sf = alloc_frame (t, sizeof *sf);
488 sf->eip = switch_entry;
490 + /* Add to list of all threads. */
491 + old_level = intr_disable ();
492 + list_push_front (&all_threads_list, &t->all_elem);
493 + intr_set_level (old_level);
495 /* Add to run queue. */
497 + if (priority > thread_get_priority ())
502 @@ -196,6 +255,19 @@ thread_block (void)
506 +/* Returns true if A has lower priority than B,
507 + within a list of threads. */
509 +thread_lower_priority (const struct list_elem *a_,
510 + const struct list_elem *b_,
513 + const struct thread *a = list_entry (a_, struct thread, elem);
514 + const struct thread *b = list_entry (b_, struct thread, elem);
516 + return a->priority < b->priority;
519 /* Transitions a blocked thread T to the ready-to-run state.
520 This is an error if T is not blocked. (Use thread_yield() to
521 make the running thread ready.) */
522 @@ -260,6 +332,7 @@ thread_exit (void)
523 /* Just set our status to dying and schedule another process.
524 We will be destroyed during the call to schedule_tail(). */
526 + list_remove (&thread_current ()->all_elem);
527 thread_current ()->status = THREAD_DYING;
530 @@ -282,11 +355,26 @@ thread_yield (void)
531 intr_set_level (old_level);
534 -/* Sets the current thread's priority to NEW_PRIORITY. */
536 +recompute_priority_chain (void)
538 + enum intr_level old_level = intr_disable ();
539 + thread_recompute_priority (thread_current ());
540 + thread_yield_to_higher_priority ();
541 + intr_set_level (old_level);
544 +/* Sets the current thread's priority to PRIORITY. */
546 -thread_set_priority (int new_priority)
547 +thread_set_priority (int priority)
549 - thread_current ()->priority = new_priority;
552 + struct thread *t = thread_current ();
554 + t->normal_priority = priority;
555 + recompute_priority_chain ();
559 /* Returns the current thread's priority. */
560 @@ -298,33 +386,93 @@ thread_get_priority (void)
562 /* Sets the current thread's nice value to NICE. */
564 -thread_set_nice (int nice UNUSED)
565 +thread_set_nice (int nice)
567 - /* Not yet implemented. */
568 + thread_current ()->nice = nice;
569 + recompute_priority_chain ();
572 /* Returns the current thread's nice value. */
574 thread_get_nice (void)
576 - /* Not yet implemented. */
578 + return thread_current ()->nice;
581 -/* Returns 100 times the system load average. */
583 thread_get_load_avg (void)
585 - /* Not yet implemented. */
588 + enum intr_level level = intr_disable ();
589 + load_avg_int = fix_round (fix_scale (load_avg, 100));
590 + intr_set_level (level);
591 + return load_avg_int;
594 -/* Returns 100 times the current thread's recent_cpu value. */
596 thread_get_recent_cpu (void)
598 - /* Not yet implemented. */
600 + int recent_cpu_int;
601 + enum intr_level level = intr_disable ();
602 + recent_cpu_int = fix_round (fix_scale (thread_current ()->recent_cpu, 100));
603 + intr_set_level (level);
604 + return recent_cpu_int;
607 +/* Returns true if thread A has lower priority than thread B,
608 + within a list of donors. */
610 +donated_lower_priority (const struct list_elem *a_,
611 + const struct list_elem *b_,
614 + const struct thread *a = list_entry (a_, struct thread, donor_elem);
615 + const struct thread *b = list_entry (b_, struct thread, donor_elem);
617 + return a->priority < b->priority;
620 +/* Recomputes T's priority in terms of its normal priority and
621 + its donors' priorities, if any,
622 + and cascades the donation as necessary. */
624 +thread_recompute_priority (struct thread *t)
626 + int old_priority = t->priority;
627 + int default_priority = t->normal_priority;
628 + int donation = PRI_MIN;
631 + default_priority = PRI_MAX - fix_round (t->recent_cpu) / 4 - t->nice * 2;
632 + if (default_priority < PRI_MIN)
633 + default_priority = PRI_MIN;
634 + else if (default_priority > PRI_MAX)
635 + default_priority = PRI_MAX;
637 + if (!list_empty (&t->donors))
638 + donation = list_entry (list_max (&t->donors, donated_lower_priority, NULL),
639 + struct thread, donor_elem)->priority;
640 + t->priority = donation > default_priority ? donation : default_priority;
641 + if (t->priority > old_priority && t->donee != NULL)
642 + thread_recompute_priority (t->donee);
645 +/* If the ready list contains a thread with a higher priority,
648 +thread_yield_to_higher_priority (void)
650 + enum intr_level old_level = intr_disable ();
651 + if (!list_empty (&ready_list))
653 + struct thread *cur = thread_current ();
654 + struct thread *max = list_entry (list_max (&ready_list,
655 + thread_lower_priority, NULL),
656 + struct thread, elem);
657 + if (max->priority > cur->priority)
660 + intr_set_level (old_level);
663 /* Idle thread. Executes when no other thread is ready to run. */
664 @@ -399,8 +547,10 @@ init_thread (struct thread *t, const cha
665 t->status = THREAD_BLOCKED;
666 strlcpy (t->name, name, sizeof t->name);
667 t->stack = (uint8_t *) t + PGSIZE;
668 - t->priority = priority;
669 + t->priority = t->normal_priority = priority;
670 t->magic = THREAD_MAGIC;
671 + sema_init (&t->timer_sema, 0);
672 + list_init (&t->donors);
675 /* Allocates a SIZE-byte frame at the top of thread T's stack and
676 @@ -426,8 +576,14 @@ next_thread_to_run (void)
678 if (list_empty (&ready_list))
681 - return list_entry (list_pop_front (&ready_list), struct thread, elem);
685 + = list_entry (list_max (&ready_list, thread_lower_priority, NULL),
686 + struct thread, elem);
687 + list_remove (&max->elem);
692 /* Completes a thread switch by activating the new thread's page
693 diff -u src/threads/thread.h~ src/threads/thread.h
694 --- src/threads/thread.h~ 2005-06-02 14:32:36.000000000 -0700
695 +++ src/threads/thread.h 2005-06-02 14:38:46.000000000 -0700
700 +#include "threads/synch.h"
701 +#include "threads/fixed-point.h"
703 /* States in a thread's life cycle. */
705 @@ -87,11 +89,26 @@ struct thread
706 enum thread_status status; /* Thread state. */
707 char name[16]; /* Name (for debugging purposes). */
708 uint8_t *stack; /* Saved stack pointer. */
709 - int priority; /* Priority. */
711 + /* Scheduler data. */
712 + int priority; /* Priority, including donations. */
713 + int normal_priority; /* Priority, without donations. */
714 + struct list donors; /* Threads donating priority to us. */
715 + struct list_elem donor_elem; /* Element in donors list. */
716 + struct thread *donee; /* Thread we're donating to. */
717 + struct lock *want_lock; /* Lock we're waiting to acquire. */
718 + int nice; /* Niceness. */
719 + fixed_point_t recent_cpu; /* Recent amount of CPU time. */
720 + struct list_elem all_elem; /* all_threads list element. */
722 /* Shared between thread.c and synch.c. */
723 struct list_elem elem; /* List element. */
726 + int64_t wakeup_time; /* Time to wake this thread up. */
727 + struct list_elem timer_elem; /* Element in timer_wait_list. */
728 + struct semaphore timer_sema; /* Semaphore. */
731 /* Owned by userprog/process.c. */
732 uint32_t *pagedir; /* Page directory. */
733 @@ -118,6 +135,10 @@ const char *thread_name (void);
735 void thread_exit (void) NO_RETURN;
736 void thread_yield (void);
737 +void thread_yield_to_higher_priority (void);
738 +void thread_recompute_priority (struct thread *);
739 +bool thread_lower_priority (const struct list_elem *, const struct list_elem *,
742 int thread_get_priority (void);
743 void thread_set_priority (int);