1 diff --git a/src/devices/timer.c b/src/devices/timer.c
2 index befaaae..1aebae7 100644
3 --- a/src/devices/timer.c
4 +++ b/src/devices/timer.c
5 @@ -24,6 +24,9 @@ static int64_t ticks;
6 Initialized by timer_calibrate(). */
7 static unsigned loops_per_tick;
9 +/* Threads waiting in timer_sleep(). */
10 +static struct list wait_list;
12 static intr_handler_func timer_interrupt;
13 static bool too_many_loops (unsigned loops);
14 static void busy_wait (int64_t loops);
15 @@ -37,6 +40,8 @@ timer_init (void)
17 pit_configure_channel (0, 2, TIMER_FREQ);
18 intr_register_ext (0x20, timer_interrupt, "8254 Timer");
20 + list_init (&wait_list);
23 /* Calibrates loops_per_tick, used to implement brief delays. */
24 @@ -84,16 +89,37 @@ timer_elapsed (int64_t then)
25 return timer_ticks () - then;
28 +/* Compares two threads based on their wake-up times. */
30 +compare_threads_by_wakeup_time (const struct list_elem *a_,
31 + const struct list_elem *b_,
34 + const struct thread *a = list_entry (a_, struct thread, timer_elem);
35 + const struct thread *b = list_entry (b_, struct thread, timer_elem);
37 + return a->wakeup_time < b->wakeup_time;
40 /* Sleeps for approximately TICKS timer ticks. Interrupts must
43 timer_sleep (int64_t ticks)
45 - int64_t start = timer_ticks ();
46 + struct thread *t = thread_current ();
48 + /* Schedule our wake-up time. */
49 + t->wakeup_time = timer_ticks () + ticks;
51 + /* Atomically insert the current thread into the wait list. */
52 ASSERT (intr_get_level () == INTR_ON);
53 - while (timer_elapsed (start) < ticks)
56 + list_insert_ordered (&wait_list, &t->timer_elem,
57 + compare_threads_by_wakeup_time, NULL);
61 + sema_down (&t->timer_sema);
64 /* Sleeps for approximately MS milliseconds. Interrupts must be
65 @@ -172,6 +198,17 @@ timer_interrupt (struct intr_frame *args UNUSED)
70 + while (!list_empty (&wait_list))
72 + struct thread *t = list_entry (list_front (&wait_list),
73 + struct thread, timer_elem);
74 + if (ticks < t->wakeup_time)
76 + sema_up (&t->timer_sema);
77 + thread_yield_to_higher_priority ();
78 + list_pop_front (&wait_list);
82 /* Returns true if LOOPS iterations waits for more than one timer
83 diff --git a/src/threads/fixed-point.h b/src/threads/fixed-point.h
85 index 0000000..ca88f97
87 +++ b/src/threads/fixed-point.h
89 +#ifndef THREADS_FIXED_POINT_H
90 +#define THREADS_FIXED_POINT_H
95 +#define FIX_BITS 32 /* Total bits per fixed-point number. */
96 +#define FIX_P 16 /* Number of integer bits. */
97 +#define FIX_Q 16 /* Number of fractional bits. */
98 +#define FIX_F (1 << FIX_Q) /* pow(2, FIX_Q). */
100 +#define FIX_MIN_INT (-FIX_MAX_INT) /* Smallest representable integer. */
101 +#define FIX_MAX_INT ((1 << FIX_P) - 1) /* Largest representable integer. */
103 +/* A fixed-point number. */
110 +/* Returns a fixed-point number with F as its internal value. */
111 +static inline fixed_point_t
119 +/* Returns fixed-point number corresponding to integer N. */
120 +static inline fixed_point_t
123 + ASSERT (n >= FIX_MIN_INT && n <= FIX_MAX_INT);
124 + return __mk_fix (n * FIX_F);
127 +/* Returns fixed-point number corresponding to N divided by D. */
128 +static inline fixed_point_t
129 +fix_frac (int n, int d)
132 + ASSERT (n / d >= FIX_MIN_INT && n / d <= FIX_MAX_INT);
133 + return __mk_fix ((long long) n * FIX_F / d);
136 +/* Returns X rounded to the nearest integer. */
138 +fix_round (fixed_point_t x)
140 + return (x.f + FIX_F / 2) / FIX_F;
143 +/* Returns X truncated down to the nearest integer. */
145 +fix_trunc (fixed_point_t x)
147 + return x.f / FIX_F;
150 +/* Returns X + Y. */
151 +static inline fixed_point_t
152 +fix_add (fixed_point_t x, fixed_point_t y)
154 + return __mk_fix (x.f + y.f);
157 +/* Returns X - Y. */
158 +static inline fixed_point_t
159 +fix_sub (fixed_point_t x, fixed_point_t y)
161 + return __mk_fix (x.f - y.f);
164 +/* Returns X * Y. */
165 +static inline fixed_point_t
166 +fix_mul (fixed_point_t x, fixed_point_t y)
168 + return __mk_fix ((long long) x.f * y.f / FIX_F);
171 +/* Returns X * N. */
172 +static inline fixed_point_t
173 +fix_scale (fixed_point_t x, int n)
176 + return __mk_fix (x.f * n);
179 +/* Returns X / Y. */
180 +static inline fixed_point_t
181 +fix_div (fixed_point_t x, fixed_point_t y)
183 + return __mk_fix ((long long) x.f * FIX_F / y.f);
186 +/* Returns X / N. */
187 +static inline fixed_point_t
188 +fix_unscale (fixed_point_t x, int n)
191 + return __mk_fix (x.f / n);
194 +/* Returns 1 / X. */
195 +static inline fixed_point_t
196 +fix_inv (fixed_point_t x)
198 + return fix_div (fix_int (1), x);
201 +/* Returns -1 if X < Y, 0 if X == Y, 1 if X > Y. */
203 +fix_compare (fixed_point_t x, fixed_point_t y)
205 + return x.f < y.f ? -1 : x.f > y.f;
208 +#endif /* threads/fixed-point.h */
209 diff --git a/src/threads/synch.c b/src/threads/synch.c
210 index 317c68a..53197bb 100644
211 --- a/src/threads/synch.c
212 +++ b/src/threads/synch.c
213 @@ -113,10 +113,28 @@ sema_up (struct semaphore *sema)
214 ASSERT (sema != NULL);
216 old_level = intr_disable ();
217 - if (!list_empty (&sema->waiters))
218 - thread_unblock (list_entry (list_pop_front (&sema->waiters),
219 - struct thread, elem));
221 + if (!list_empty (&sema->waiters))
223 + /* Find highest-priority waiting thread. */
224 + struct thread *max = list_entry (list_max (&sema->waiters,
225 + thread_lower_priority, NULL),
226 + struct thread, elem);
228 + /* Remove `max' from wait list and unblock. */
229 + list_remove (&max->elem);
230 + thread_unblock (max);
232 + /* Yield to a higher-priority thread, if we're running in a
233 + context where it makes sense to do so.
235 + Kind of a funny interaction with donation here.
236 + We only support donation for locks, and locks turn off
237 + interrupts before calling us, so we automatically don't
238 + do the yield here, delegating to lock_release(). */
239 + if (!intr_context () && old_level == INTR_ON)
240 + thread_yield_to_higher_priority ();
242 intr_set_level (old_level);
245 @@ -192,12 +210,33 @@ lock_init (struct lock *lock)
247 lock_acquire (struct lock *lock)
249 + enum intr_level old_level;
251 ASSERT (lock != NULL);
252 ASSERT (!intr_context ());
253 ASSERT (!lock_held_by_current_thread (lock));
255 + old_level = intr_disable ();
257 + if (lock->holder != NULL)
259 + /* Donate our priority to the thread holding the lock.
260 + First, update the data structures. */
261 + struct thread *donor = thread_current ();
262 + donor->want_lock = lock;
263 + donor->donee = lock->holder;
264 + list_push_back (&lock->holder->donors, &donor->donor_elem);
266 + /* Now implement the priority donation itself
267 + by recomputing the donee's priority
268 + and cascading the donation as far as necessary. */
269 + if (donor->donee != NULL)
270 + thread_recompute_priority (donor->donee);
273 sema_down (&lock->semaphore);
274 lock->holder = thread_current ();
275 + intr_set_level (old_level);
278 /* Tries to acquires LOCK and returns true if successful or false
279 @@ -228,11 +267,39 @@ lock_try_acquire (struct lock *lock)
281 lock_release (struct lock *lock)
283 + enum intr_level old_level;
284 + struct thread *t = thread_current ();
285 + struct list_elem *e;
287 ASSERT (lock != NULL);
288 ASSERT (lock_held_by_current_thread (lock));
290 + old_level = intr_disable ();
292 + /* Return donations to threads that want this lock. */
293 + for (e = list_begin (&t->donors); e != list_end (&t->donors); )
295 + struct thread *donor = list_entry (e, struct thread, donor_elem);
296 + if (donor->want_lock == lock)
298 + donor->donee = NULL;
299 + e = list_remove (e);
305 + /* Release lock. */
307 sema_up (&lock->semaphore);
309 + /* Recompute our priority based on our remaining donations,
310 + then yield to a higher-priority ready thread if one now
312 + thread_recompute_priority (t);
313 + thread_yield_to_higher_priority ();
315 + intr_set_level (old_level);
318 /* Returns true if the current thread holds LOCK, false
319 @@ -251,6 +318,7 @@ struct semaphore_elem
321 struct list_elem elem; /* List element. */
322 struct semaphore semaphore; /* This semaphore. */
323 + struct thread *thread; /* Thread. */
326 /* Initializes condition variable COND. A condition variable
327 @@ -295,12 +363,26 @@ cond_wait (struct condition *cond, struct lock *lock)
328 ASSERT (lock_held_by_current_thread (lock));
330 sema_init (&waiter.semaphore, 0);
331 + waiter.thread = thread_current ();
332 list_push_back (&cond->waiters, &waiter.elem);
334 sema_down (&waiter.semaphore);
339 +semaphore_elem_lower_priority (const struct list_elem *a_,
340 + const struct list_elem *b_,
343 + const struct semaphore_elem *a
344 + = list_entry (a_, struct semaphore_elem, elem);
345 + const struct semaphore_elem *b
346 + = list_entry (b_, struct semaphore_elem, elem);
348 + return a->thread->priority < b->thread->priority;
351 /* If any threads are waiting on COND (protected by LOCK), then
352 this function signals one of them to wake up from its wait.
353 LOCK must be held before calling this function.
354 @@ -317,8 +399,12 @@ cond_signal (struct condition *cond, struct lock *lock UNUSED)
355 ASSERT (lock_held_by_current_thread (lock));
357 if (!list_empty (&cond->waiters))
358 - sema_up (&list_entry (list_pop_front (&cond->waiters),
359 - struct semaphore_elem, elem)->semaphore);
361 + struct list_elem *max
362 + = list_max (&cond->waiters, semaphore_elem_lower_priority, NULL);
364 + sema_up (&list_entry (max, struct semaphore_elem, elem)->semaphore);
368 /* Wakes up all threads, if any, waiting on COND (protected by
369 diff --git a/src/threads/thread.c b/src/threads/thread.c
370 index 87f22b8..86614f5 100644
371 --- a/src/threads/thread.c
372 +++ b/src/threads/thread.c
376 #include "threads/flags.h"
377 +#include "threads/init.h"
378 #include "threads/interrupt.h"
379 #include "threads/intr-stubs.h"
380 #include "threads/palloc.h"
381 #include "threads/switch.h"
382 #include "threads/synch.h"
383 +#include "devices/timer.h"
384 #include "threads/vaddr.h"
386 #include "userprog/process.h"
387 @@ -53,6 +55,7 @@ static long long user_ticks; /* # of timer ticks in user programs. */
389 #define TIME_SLICE 4 /* # of timer ticks to give each thread. */
390 static unsigned thread_ticks; /* # of timer ticks since last yield. */
391 +static fixed_point_t load_avg; /* Load average. */
393 /* If false (default), use round-robin scheduler.
394 If true, use multi-level feedback queue scheduler.
395 @@ -92,6 +95,7 @@ thread_init (void)
396 lock_init (&tid_lock);
397 list_init (&ready_list);
398 list_init (&all_list);
399 + load_avg = fix_int (0);
401 /* Set up a thread structure for the running thread. */
402 initial_thread = running_thread ();
403 @@ -117,6 +121,18 @@ thread_start (void)
404 sema_down (&idle_started);
407 +/* Adjust recent CPU of a thread based on load factor
408 + and recompute its priority. */
410 +adjust_recent_cpu (struct thread *t, void *aux)
412 + fixed_point_t load_factor = *(fixed_point_t *)aux;
414 + t->recent_cpu = fix_add (fix_mul (load_factor, t->recent_cpu),
415 + fix_int (t->nice));
416 + thread_recompute_priority (t);
419 /* Called by the timer interrupt handler at each timer tick.
420 Thus, this function runs in an external interrupt context. */
422 @@ -134,9 +150,41 @@ thread_tick (void)
426 - /* Enforce preemption. */
427 - if (++thread_ticks >= TIME_SLICE)
428 - intr_yield_on_return ();
431 + /* Update load average. */
432 + if (timer_ticks () % TIMER_FREQ == 0)
434 + size_t ready_threads = list_size (&ready_list);
435 + if (t != idle_thread)
438 + load_avg = fix_add (fix_mul (fix_frac (59, 60), load_avg),
439 + fix_mul (fix_frac (1, 60), fix_int (ready_threads)));
442 + /* Increment running process's recent_cpu. */
443 + if (t != idle_thread)
444 + t->recent_cpu = fix_add (t->recent_cpu, fix_int (1));
446 + /* Update recent_cpu and thread priorities once per second. */
447 + if (timer_ticks () % TIMER_FREQ == 0)
449 + fixed_point_t twice_load = fix_scale (load_avg, 2);
450 + fixed_point_t twice_load_plus_1 = fix_add (twice_load, fix_int (1));
451 + fixed_point_t load_factor = fix_div (twice_load, twice_load_plus_1);
453 + thread_foreach (adjust_recent_cpu, &load_factor);
457 + /* Switch threads if time slice has expired. */
458 + if (++thread_ticks >= TIME_SLICE)
461 + thread_recompute_priority (thread_current ());
462 + intr_yield_on_return ();
466 /* Prints thread statistics. */
467 @@ -166,6 +214,7 @@ tid_t
468 thread_create (const char *name, int priority,
469 thread_func *function, void *aux)
471 + struct thread *cur = thread_current ();
473 struct kernel_thread_frame *kf;
474 struct switch_entry_frame *ef;
475 @@ -180,8 +229,10 @@ thread_create (const char *name, int priority,
478 /* Initialize thread. */
479 - init_thread (t, name, priority);
480 + init_thread (t, name, thread_mlfqs ? cur->priority : priority);
481 tid = t->tid = allocate_tid ();
482 + t->nice = cur->nice;
483 + t->recent_cpu = cur->recent_cpu;
485 /* Stack frame for kernel_thread(). */
486 kf = alloc_frame (t, sizeof *kf);
487 @@ -200,6 +251,8 @@ thread_create (const char *name, int priority,
489 /* Add to run queue. */
491 + if (priority > thread_get_priority ())
496 @@ -220,6 +273,19 @@ thread_block (void)
500 +/* Returns true if A has lower priority than B,
501 + within a list of threads. */
503 +thread_lower_priority (const struct list_elem *a_,
504 + const struct list_elem *b_,
507 + const struct thread *a = list_entry (a_, struct thread, elem);
508 + const struct thread *b = list_entry (b_, struct thread, elem);
510 + return a->priority < b->priority;
513 /* Transitions a blocked thread T to the ready-to-run state.
514 This is an error if T is not blocked. (Use thread_yield() to
515 make the running thread ready.)
516 @@ -331,11 +397,26 @@ thread_foreach (thread_action_func *func, void *aux)
520 -/* Sets the current thread's priority to NEW_PRIORITY. */
522 +recompute_priority_chain (void)
524 + enum intr_level old_level = intr_disable ();
525 + thread_recompute_priority (thread_current ());
526 + thread_yield_to_higher_priority ();
527 + intr_set_level (old_level);
530 +/* Sets the current thread's priority to PRIORITY. */
532 -thread_set_priority (int new_priority)
533 +thread_set_priority (int priority)
535 - thread_current ()->priority = new_priority;
538 + struct thread *t = thread_current ();
540 + t->normal_priority = priority;
541 + recompute_priority_chain ();
545 /* Returns the current thread's priority. */
546 @@ -347,33 +428,98 @@ thread_get_priority (void)
548 /* Sets the current thread's nice value to NICE. */
550 -thread_set_nice (int nice UNUSED)
551 +thread_set_nice (int nice)
553 - /* Not yet implemented. */
554 + thread_current ()->nice = nice;
555 + recompute_priority_chain ();
558 /* Returns the current thread's nice value. */
560 thread_get_nice (void)
562 - /* Not yet implemented. */
564 + return thread_current ()->nice;
567 -/* Returns 100 times the system load average. */
569 thread_get_load_avg (void)
571 - /* Not yet implemented. */
574 + enum intr_level level = intr_disable ();
575 + load_avg_int = fix_round (fix_scale (load_avg, 100));
576 + intr_set_level (level);
577 + return load_avg_int;
580 -/* Returns 100 times the current thread's recent_cpu value. */
582 thread_get_recent_cpu (void)
584 - /* Not yet implemented. */
586 + int recent_cpu_int;
587 + enum intr_level level = intr_disable ();
588 + recent_cpu_int = fix_round (fix_scale (thread_current ()->recent_cpu, 100));
589 + intr_set_level (level);
590 + return recent_cpu_int;
593 +/* Returns true if thread A has lower priority than thread B,
594 + within a list of donors. */
596 +donated_lower_priority (const struct list_elem *a_,
597 + const struct list_elem *b_,
600 + const struct thread *a = list_entry (a_, struct thread, donor_elem);
601 + const struct thread *b = list_entry (b_, struct thread, donor_elem);
603 + return a->priority < b->priority;
606 +/* Recomputes T's priority in terms of its normal priority and
607 + its donors' priorities, if any,
608 + and cascades the donation as necessary. */
610 +thread_recompute_priority (struct thread *t)
612 + int old_priority = t->priority;
613 + int default_priority = t->normal_priority;
614 + int donation = PRI_MIN;
617 + default_priority = PRI_MAX - fix_round (t->recent_cpu) / 4 - t->nice * 2;
618 + if (default_priority < PRI_MIN)
619 + default_priority = PRI_MIN;
620 + else if (default_priority > PRI_MAX)
621 + default_priority = PRI_MAX;
623 + if (!list_empty (&t->donors))
624 + donation = list_entry (list_max (&t->donors, donated_lower_priority, NULL),
625 + struct thread, donor_elem)->priority;
626 + t->priority = donation > default_priority ? donation : default_priority;
627 + if (t->priority > old_priority && t->donee != NULL)
628 + thread_recompute_priority (t->donee);
631 +/* If the ready list contains a thread with a higher priority,
634 +thread_yield_to_higher_priority (void)
636 + enum intr_level old_level = intr_disable ();
637 + if (!list_empty (&ready_list))
639 + struct thread *cur = thread_current ();
640 + struct thread *max = list_entry (list_max (&ready_list,
641 + thread_lower_priority, NULL),
642 + struct thread, elem);
643 + if (max->priority > cur->priority)
645 + if (intr_context ())
646 + intr_yield_on_return ();
651 + intr_set_level (old_level);
654 /* Idle thread. Executes when no other thread is ready to run.
655 @@ -461,9 +607,10 @@ init_thread (struct thread *t, const char *name, int priority)
656 t->status = THREAD_BLOCKED;
657 strlcpy (t->name, name, sizeof t->name);
658 t->stack = (uint8_t *) t + PGSIZE;
659 - t->priority = priority;
660 + t->priority = t->normal_priority = priority;
661 t->magic = THREAD_MAGIC;
663 + sema_init (&t->timer_sema, 0);
664 + list_init (&t->donors);
665 old_level = intr_disable ();
666 list_push_back (&all_list, &t->allelem);
667 intr_set_level (old_level);
668 @@ -492,8 +639,14 @@ next_thread_to_run (void)
670 if (list_empty (&ready_list))
673 - return list_entry (list_pop_front (&ready_list), struct thread, elem);
677 + = list_entry (list_max (&ready_list, thread_lower_priority, NULL),
678 + struct thread, elem);
679 + list_remove (&max->elem);
684 /* Completes a thread switch by activating the new thread's page
685 diff --git a/src/threads/thread.h b/src/threads/thread.h
686 index 7965c06..6601963 100644
687 --- a/src/threads/thread.h
688 +++ b/src/threads/thread.h
693 +#include "threads/synch.h"
694 +#include "threads/fixed-point.h"
696 /* States in a thread's life cycle. */
698 @@ -87,12 +89,26 @@ struct thread
699 enum thread_status status; /* Thread state. */
700 char name[16]; /* Name (for debugging purposes). */
701 uint8_t *stack; /* Saved stack pointer. */
702 - int priority; /* Priority. */
704 + /* Scheduler data. */
705 + int priority; /* Priority, including donations. */
706 + int normal_priority; /* Priority, without donations. */
707 + struct list donors; /* Threads donating priority to us. */
708 + struct list_elem donor_elem; /* Element in donors list. */
709 + struct thread *donee; /* Thread we're donating to. */
710 + struct lock *want_lock; /* Lock we're waiting to acquire. */
711 + int nice; /* Niceness. */
712 + fixed_point_t recent_cpu; /* Recent amount of CPU time. */
713 struct list_elem allelem; /* List element for all threads list. */
715 /* Shared between thread.c and synch.c. */
716 struct list_elem elem; /* List element. */
719 + int64_t wakeup_time; /* Time to wake this thread up. */
720 + struct list_elem timer_elem; /* Element in wait_list. */
721 + struct semaphore timer_sema; /* Semaphore. */
724 /* Owned by userprog/process.c. */
725 uint32_t *pagedir; /* Page directory. */
726 @@ -125,6 +141,10 @@ const char *thread_name (void);
728 void thread_exit (void) NO_RETURN;
729 void thread_yield (void);
730 +void thread_yield_to_higher_priority (void);
731 +void thread_recompute_priority (struct thread *);
732 +bool thread_lower_priority (const struct list_elem *, const struct list_elem *,
735 /* Performs some operation on thread t, given auxiliary data AUX. */
736 typedef void thread_action_func (struct thread *t, void *aux);