<aside> 💡 이 의문점을 해결하기 위해 list에 push를 할때 우선순위를 기준으로 삽입하는 방법과, list에서 pop을 할때 우선순위를 고려하는 방법을 모두 구현 하였습니다.
</aside>
//insert 예시
void
sema_down (struct semaphore *sema) {
enum intr_level old_level;
ASSERT (sema != NULL);
ASSERT (!intr_context ());
old_level = intr_disable ();
while (sema->value == 0) {
// list_push_back (&sema->waiters, &thread_current ()->elem);
list_insert_ordered (&sema->waiters, &thread_current ()->elem, more, 0);
thread_block ();
}
sema->value--;
intr_set_level (old_level);
}
// pop 예시
void
sema_up (struct semaphore *sema) {
enum intr_level old_level;
ASSERT (sema != NULL);
old_level = intr_disable ();
if (!list_empty (&sema->waiters))
{
list_sort (&sema->waiters, more, 0); // 솔트 추가한거
thread_unblock (list_entry (list_pop_front (&sema->waiters),
struct thread, elem));
}
sema->value++;
thread_comp_ready(); //실행 되기전 스캐쥴링을 위해
intr_set_level (old_level);
}
// push 예시
void
sema_down (struct semaphore *sema) {
enum intr_level old_level;
ASSERT (sema != NULL);
ASSERT (!intr_context ());
old_level = intr_disable ();
while (sema->value == 0) {
list_push_back (&sema->waiters, &thread_current ()->elem);
thread_block ();
}
sema->value--;
intr_set_level (old_level);
}
// pop 예시
void
sema_up (struct semaphore *sema) {
enum intr_level old_level;
struct thread *t = NULL;
ASSERT (sema != NULL);
old_level = intr_disable ();
if (!list_empty (&sema->waiters)) {
t = thread_pop_max(&sema->waiters);
thread_unblock(t);
}
sema->value++;
if(t != NULL && thread_get_priority() <= t->priority) {
thread_yield();
}
intr_set_level (old_level);
}