diff options
Diffstat (limited to 'threads/synch.c')
| -rw-r--r-- | threads/synch.c | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/threads/synch.c b/threads/synch.c index db204f5..d7e5e39 100644 --- a/threads/synch.c +++ b/threads/synch.c | |||
| @@ -34,8 +34,6 @@ | |||
| 34 | 34 | ||
| 35 | static bool lock_priority_cmp_greater(const struct list_elem *a, const struct list_elem *b, | 35 | static bool lock_priority_cmp_greater(const struct list_elem *a, const struct list_elem *b, |
| 36 | void *AUX UNUSED); | 36 | void *AUX UNUSED); |
| 37 | static bool semaphore_priority_cmp_greater(const struct list_elem *a, const struct list_elem *b, | ||
| 38 | void *aux UNUSED); | ||
| 39 | 37 | ||
| 40 | /* Initializes semaphore SEMA to VALUE. A semaphore is a | 38 | /* Initializes semaphore SEMA to VALUE. A semaphore is a |
| 41 | nonnegative integer along with two atomic operators for | 39 | nonnegative integer along with two atomic operators for |
| @@ -353,16 +351,6 @@ cond_init (struct condition *cond) | |||
| 353 | list_init (&cond->waiters); | 351 | list_init (&cond->waiters); |
| 354 | } | 352 | } |
| 355 | 353 | ||
| 356 | /* comparison function for our descending ordered condition waiters list. */ | ||
| 357 | static bool | ||
| 358 | semaphore_priority_cmp_greater(const struct list_elem *a, const struct list_elem *b, | ||
| 359 | void *aux UNUSED) | ||
| 360 | { | ||
| 361 | const struct semaphore_elem *s1 = list_entry (a, struct semaphore_elem, elem); | ||
| 362 | const struct semaphore_elem *s2 = list_entry (b, struct semaphore_elem, elem); | ||
| 363 | return (s1->thread->priority > s2->thread->priority); | ||
| 364 | } | ||
| 365 | |||
| 366 | /* Atomically releases LOCK and waits for COND to be signaled by | 354 | /* Atomically releases LOCK and waits for COND to be signaled by |
| 367 | some other piece of code. After COND is signaled, LOCK is | 355 | some other piece of code. After COND is signaled, LOCK is |
| 368 | reacquired before returning. LOCK must be held before calling | 356 | reacquired before returning. LOCK must be held before calling |
| @@ -396,9 +384,9 @@ cond_wait (struct condition *cond, struct lock *lock) | |||
| 396 | sema_init (&waiter.semaphore, 0); | 384 | sema_init (&waiter.semaphore, 0); |
| 397 | /* set thread of waiter */ | 385 | /* set thread of waiter */ |
| 398 | waiter.thread = thread_current (); | 386 | waiter.thread = thread_current (); |
| 399 | /* and add condition to our ordered waiters list */ | 387 | /* and add condition to our waiters list. sorting isn't useful here either. |
| 400 | list_insert_ordered (&cond->waiters, &waiter.elem, | 388 | see sema_up()/cond_signal() for more details */ |
| 401 | &semaphore_priority_cmp_greater, NULL); | 389 | list_push_back (&cond->waiters, &waiter->elem); |
| 402 | lock_release (lock); | 390 | lock_release (lock); |
| 403 | sema_down (&waiter.semaphore); | 391 | sema_down (&waiter.semaphore); |
| 404 | lock_acquire (lock); | 392 | lock_acquire (lock); |
| @@ -419,9 +407,15 @@ cond_signal (struct condition *cond, struct lock *lock UNUSED) | |||
| 419 | ASSERT (!intr_context ()); | 407 | ASSERT (!intr_context ()); |
| 420 | ASSERT (lock_held_by_current_thread (lock)); | 408 | ASSERT (lock_held_by_current_thread (lock)); |
| 421 | 409 | ||
| 422 | if (!list_empty (&cond->waiters)) | 410 | /* we can't sort the list during insertion that easily as the priority of |
| 423 | sema_up (&list_entry (list_pop_front (&cond->waiters), | 411 | blocked/waiting threads may change due to priority donation. |
| 424 | struct semaphore_elem, elem)->semaphore); | 412 | this is the same as with sema_down()/sema_up() */ |
| 413 | if (!list_empty (&cond->waiters)) | ||
| 414 | { | ||
| 415 | list_sort (&cond->waiters, &semaphore_priority_cmp_greater, NULL); | ||
| 416 | sema_up (&list_entry (list_pop_front (&cond->waiters), | ||
| 417 | struct semaphore_elem, elem)->semaphore); | ||
| 418 | } | ||
| 425 | } | 419 | } |
| 426 | 420 | ||
| 427 | /* Wakes up all threads, if any, waiting on COND (protected by | 421 | /* Wakes up all threads, if any, waiting on COND (protected by |
