summaryrefslogtreecommitdiffstats
path: root/threads/thread.c
diff options
context:
space:
mode:
Diffstat (limited to 'threads/thread.c')
-rw-r--r--threads/thread.c24
1 files changed, 22 insertions, 2 deletions
diff --git a/threads/thread.c b/threads/thread.c
index 845f059..3fefbd8 100644
--- a/threads/thread.c
+++ b/threads/thread.c
@@ -71,6 +71,16 @@ static void schedule (void);
71void thread_schedule_tail (struct thread *prev); 71void thread_schedule_tail (struct thread *prev);
72static tid_t allocate_tid (void); 72static tid_t allocate_tid (void);
73 73
74//TODO: should take donated priority into account
75static bool
76ready_list_cmp_less(const struct list_elem *a, const struct list_elem *b,
77 void *AUX UNUSED)
78{
79 struct thread *t1 = list_entry (a, struct thread, elem);
80 struct thread *t2 = list_entry (b, struct thread, elem);
81 return (t1->priority > t2->priority);
82}
83
74/* Initializes the threading system by transforming the code 84/* Initializes the threading system by transforming the code
75 that's currently running into a thread. This can't work in 85 that's currently running into a thread. This can't work in
76 general and it is possible in this case only because loader.S 86 general and it is possible in this case only because loader.S
@@ -212,6 +222,7 @@ thread_create (const char *name, int priority,
212 222
213 /* Add to run queue. */ 223 /* Add to run queue. */
214 thread_unblock (t); 224 thread_unblock (t);
225 thread_yield ();
215 226
216 return tid; 227 return tid;
217} 228}
@@ -249,7 +260,8 @@ thread_unblock (struct thread *t)
249 260
250 old_level = intr_disable (); 261 old_level = intr_disable ();
251 ASSERT (t->status == THREAD_BLOCKED); 262 ASSERT (t->status == THREAD_BLOCKED);
252 list_push_back (&ready_list, &t->elem); 263 /* add thread to our ordered ready_list */
264 list_insert_ordered (&ready_list, &t->elem, &ready_list_cmp_less, NULL);
253 t->status = THREAD_READY; 265 t->status = THREAD_READY;
254 intr_set_level (old_level); 266 intr_set_level (old_level);
255} 267}
@@ -320,7 +332,7 @@ thread_yield (void)
320 332
321 old_level = intr_disable (); 333 old_level = intr_disable ();
322 if (cur != idle_thread) 334 if (cur != idle_thread)
323 list_push_back (&ready_list, &cur->elem); 335 list_insert_ordered (&ready_list, &cur->elem, &ready_list_cmp_less, NULL);
324 cur->status = THREAD_READY; 336 cur->status = THREAD_READY;
325 schedule (); 337 schedule ();
326 intr_set_level (old_level); 338 intr_set_level (old_level);
@@ -348,6 +360,14 @@ void
348thread_set_priority (int new_priority) 360thread_set_priority (int new_priority)
349{ 361{
350 thread_current ()->priority = new_priority; 362 thread_current ()->priority = new_priority;
363
364 /* compare priority with the highest priority in the list */
365 if (!list_empty (&ready_list))
366 {
367 struct thread *t = list_entry (list_front (&ready_list), struct thread, elem);
368 if (t->priority > thread_current ()->priority)
369 thread_yield();
370 }
351} 371}
352 372
353/* Returns the current thread's priority. */ 373/* Returns the current thread's priority. */