1 Index: src/threads/synch.c
2 ===================================================================
3 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/threads/synch.c,v
4 retrieving revision 1.15
5 diff -u -p -u -r1.15 synch.c
6 --- src/threads/synch.c 31 Dec 2004 21:13:38 -0000 1.15
7 +++ src/threads/synch.c 31 Dec 2004 22:31:03 -0000
8 @@ -77,6 +77,17 @@ sema_down (struct semaphore *sema)
9 intr_set_level (old_level);
12 +/* Returns true if thread A has lower priority than thread B. */
14 +donated_lower_priority (const list_elem *a_, const list_elem *b_,
17 + const struct thread *a = list_entry (a_, struct thread, donor_elem);
18 + const struct thread *b = list_entry (b_, struct thread, donor_elem);
20 + return a->priority < b->priority;
23 /* Up or "V" operation on a semaphore. Increments SEMA's value
24 and wakes up one thread of those waiting for SEMA, if any.
26 @@ -89,10 +100,28 @@ sema_up (struct semaphore *sema)
27 ASSERT (sema != NULL);
29 old_level = intr_disable ();
30 - if (!list_empty (&sema->waiters))
31 - thread_unblock (list_entry (list_pop_front (&sema->waiters),
32 - struct thread, elem));
34 + if (!list_empty (&sema->waiters))
36 + /* Find highest-priority waiting thread. */
37 + struct thread *max = list_entry (list_max (&sema->waiters,
38 + donated_lower_priority, NULL),
39 + struct thread, elem);
41 + /* Remove `max' from wait list and unblock. */
42 + list_remove (&max->elem);
43 + thread_unblock (max);
45 + /* Yield to a higher-priority thread, if we're running in a
46 + context where it makes sense to do so.
48 + Kind of a funny interaction with donation here.
49 + We only support donation for locks, and locks turn off
50 + interrupts before calling us, so we automatically don't
51 + do the yield here, delegating to lock_release(). */
52 + if (!intr_context () && old_level == INTR_ON)
53 + thread_yield_to_higher_priority ();
55 intr_set_level (old_level);
58 @@ -184,6 +213,23 @@ lock_acquire (struct lock *lock)
59 ASSERT (!lock_held_by_current_thread (lock));
61 old_level = intr_disable ();
63 + if (lock->holder != NULL)
65 + /* Donate our priority to the thread holding the lock.
66 + First, update the data structures. */
67 + struct thread *donor = thread_current ();
68 + donor->want_lock = lock;
69 + donor->donee = lock->holder;
70 + list_push_back (&lock->holder->donors, &donor->donor_elem);
72 + /* Now implement the priority donation itself
73 + by recomputing the donee's priority
74 + and cascading the donation as far as necessary. */
75 + while (donor->donee != NULL && thread_recompute_priority (donor->donee))
76 + donor = donor->donee;
79 sema_down (&lock->semaphore);
80 lock->holder = thread_current ();
81 intr_set_level (old_level);
82 @@ -198,13 +244,38 @@ void
83 lock_release (struct lock *lock)
85 enum intr_level old_level;
86 + struct thread *t = thread_current ();
89 ASSERT (lock != NULL);
90 ASSERT (lock_held_by_current_thread (lock));
92 old_level = intr_disable ();
94 + /* Remove donations from threads that want the lock we're
96 + for (e = list_begin (&t->donors); e != list_end (&t->donors); )
98 + struct thread *donor = list_entry (e, struct thread, donor_elem);
99 + if (donor->want_lock == lock)
101 + donor->donee = NULL;
102 + e = list_remove (e);
108 + /* Release lock. */
110 sema_up (&lock->semaphore);
112 + /* Recompute our priority based on our remaining donations,
113 + then yield to a higher-priority ready thread if one now
115 + thread_recompute_priority (t);
116 + thread_yield_to_higher_priority ();
118 intr_set_level (old_level);
121 Index: src/threads/thread.c
122 ===================================================================
123 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/threads/thread.c,v
124 retrieving revision 1.48
125 diff -u -p -u -r1.48 thread.c
126 --- src/threads/thread.c 9 Oct 2004 18:01:37 -0000 1.48
127 +++ src/threads/thread.c 31 Dec 2004 22:31:03 -0000
128 @@ -166,6 +166,8 @@ thread_create (const char *name, int pri
130 /* Add to run queue. */
132 + if (priority > thread_get_priority ())
137 @@ -186,6 +188,18 @@ thread_block (void)
141 +/* Returns true if A has higher priority than B,
142 + within a list of threads. */
144 +thread_higher_priority (const list_elem *a_, const list_elem *b_,
147 + const struct thread *a = list_entry (a_, struct thread, elem);
148 + const struct thread *b = list_entry (b_, struct thread, elem);
150 + return a->priority > b->priority;
153 /* Transitions a blocked thread T to the ready-to-run state.
154 This is an error if T is not blocked. (Use thread_yield() to
155 make the running thread ready.) */
156 @@ -198,7 +212,7 @@ thread_unblock (struct thread *t)
158 old_level = intr_disable ();
159 ASSERT (t->status == THREAD_BLOCKED);
160 - list_push_back (&ready_list, &t->elem);
161 + list_insert_ordered (&ready_list, &t->elem, thread_higher_priority, NULL);
162 t->status = THREAD_READY;
163 intr_set_level (old_level);
165 @@ -266,11 +280,79 @@ thread_yield (void)
166 ASSERT (!intr_context ());
168 old_level = intr_disable ();
169 - list_push_back (&ready_list, &cur->elem);
170 + list_insert_ordered (&ready_list, &cur->elem, thread_higher_priority, NULL);
171 cur->status = THREAD_READY;
173 intr_set_level (old_level);
176 +/* Sets the current thread's priority to PRIORITY. */
178 +thread_set_priority (int priority)
180 + struct thread *t = thread_current ();
181 + enum intr_level old_level;
183 + t->normal_priority = priority;
185 + old_level = intr_disable ();
186 + while (thread_recompute_priority (t) && t->donee != NULL)
188 + thread_yield_to_higher_priority ();
189 + intr_set_level (old_level);
192 +/* Returns the current thread's priority. */
194 +thread_get_priority (void)
196 + return thread_current ()->priority;
199 +/* Returns true if thread A has lower priority than thread B,
200 + within a list of donors. */
202 +donated_lower_priority (const list_elem *a_, const list_elem *b_,
205 + const struct thread *a = list_entry (a_, struct thread, donor_elem);
206 + const struct thread *b = list_entry (b_, struct thread, donor_elem);
208 + return a->priority < b->priority;
211 +/* Recomputes T's priority in terms of its normal priority and
212 + its donors' priorities, if any.
213 + Returns true if T's new priority is higher than its old
214 + priority, false otherwise. */
216 +thread_recompute_priority (struct thread *t)
218 + int old = t->priority;
220 + if (!list_empty (&t->donors))
221 + donation = list_entry (list_max (&t->donors, donated_lower_priority, NULL),
222 + struct thread, donor_elem)->priority;
223 + t->priority = donation > t->normal_priority ? donation : t->normal_priority;
224 + return t->priority > old;
227 +/* If the ready list contains a thread with a higher priority,
230 +thread_yield_to_higher_priority (void)
232 + enum intr_level old_level = intr_disable ();
233 + if (!list_empty (&ready_list))
235 + struct thread *cur = thread_current ();
236 + struct thread *max = list_entry (list_front (&ready_list),
237 + struct thread, elem);
238 + if (max->priority > cur->priority)
241 + intr_set_level (old_level);
244 /* Idle thread. Executes when no other thread is ready to run. */
246 @@ -335,8 +417,9 @@ init_thread (struct thread *t, const cha
247 t->status = THREAD_BLOCKED;
248 strlcpy (t->name, name, sizeof t->name);
249 t->stack = (uint8_t *) t + PGSIZE;
250 - t->priority = priority;
251 + t->priority = t->normal_priority = priority;
252 t->magic = THREAD_MAGIC;
253 + list_init (&t->donors);
256 /* Allocates a SIZE-byte frame at the top of thread T's stack and
257 Index: src/threads/thread.h
258 ===================================================================
259 RCS file: /afs/ir.stanford.edu/users/b/l/blp/private/cvs/pintos/src/threads/thread.h,v
260 retrieving revision 1.28
261 diff -u -p -u -r1.28 thread.h
262 --- src/threads/thread.h 29 Sep 2004 01:04:20 -0000 1.28
263 +++ src/threads/thread.h 31 Dec 2004 22:31:03 -0000
264 @@ -87,7 +87,14 @@ struct thread
265 enum thread_status status; /* Thread state. */
266 char name[16]; /* Name (for debugging purposes). */
267 uint8_t *stack; /* Saved stack pointer. */
268 - int priority; /* Priority. */
270 + /* Thread priority. */
271 + int priority; /* Priority, including donations. */
272 + int normal_priority; /* Priority, without donations. */
273 + struct list donors; /* Threads donating priority to us. */
274 + list_elem donor_elem; /* Element in donors list. */
275 + struct thread *donee; /* Thread we're donating to. */
276 + struct lock *want_lock; /* Lock we're waiting to acquire. */
278 /* Shared between thread.c and synch.c. */
279 list_elem elem; /* List element. */
280 @@ -118,6 +125,8 @@ const char *thread_name (void);
282 void thread_exit (void) NO_RETURN;
283 void thread_yield (void);
284 +void thread_yield_to_higher_priority (void);
285 +bool thread_recompute_priority (struct thread *);
287 /* This function will be implemented in problem 1-2. */
288 void thread_join (tid_t);