쓸만한 함수들

struct thread *curr = running_thread();
struct thread *next = next_thread_to_run();
static struct thread *
next_thread_to_run(void)
{
	if (list_empty(&ready_list))
		return idle_thread;
	else
		return list_entry(list_pop_front(&ready_list), struct thread, elem);
}

작업한 코드

thread_yield

/* CPU를 양보합니다. 현재 스레드는 휴면 상태가 되지 않으며,
	스케줄러의 판단에 따라 즉시 다시 스케줄될 수 있습니다. */
void thread_yield(void)
{
	struct thread *curr = thread_current();
	enum intr_level old_level;

	ASSERT(!intr_context());

	old_level = intr_disable();
	if (curr != idle_thread)
		// list_push_back(&ready_list, &curr->elem);
		list_insert_ordered(&ready_list, &curr->elem, compare_priority, NULL);
	do_schedule(THREAD_READY);
	intr_set_level(old_level);
}
/* Yields the CPU.  The current thread is not put to sleep and
   may be scheduled again immediately at the scheduler's whim. */
void
thread_yield (void) {
	struct thread *curr = thread_current ();
	enum intr_level old_level;

	ASSERT (!intr_context ());

	old_level = intr_disable ();
	if (curr != idle_thread)
		list_push_back (&ready_list, &curr->elem);
	do_schedule (THREAD_READY);
	intr_set_level (old_level);
}

compare_priority

static bool compare_priority(const struct list_elem *a, const struct list_elem *b, void *aux)
{
	struct thread *t1 = list_entry(a, struct thread, elem);
	struct thread *t2 = list_entry(b, struct thread, elem);

	return t1->priority > t2->priority;
}

thread_unblock

/* 차단된 스레드 T를 실행 준비 상태로 전환합니다.
	T가 차단되지 않은 경우 이는 오류입니다. (실행 중인 스레드를 준비 상태로 만들려면 thread_yield()를 사용하세요.)

	이 함수는 실행 중인 스레드를 선점하지 않습니다. 이는 중요할 수 있습니다: 호출자가 인터럽트를 직접 비활성화한 경우,
	스레드를 원자적으로 차단 해제하고 다른 데이터를 업데이트할 수 있다고 기대할 수 있습니다. */
void thread_unblock(struct thread *t)
{
	enum intr_level old_level;
	struct thread *cur = thread_current(); // 현재 실행중인 쓰레드

	ASSERT(is_thread(t));

	old_level = intr_disable(); // 인터럽트 끄기 -> 레이스 컨디션 방지
	ASSERT(t->status == THREAD_BLOCKED);
	list_insert_ordered(&ready_list, &t->elem, compare_priority, NULL); // 우선순위 순으로 레디 큐에 저장

	if (thread_get_priority() < t->priority) // 레디 큐에 넣을 쓰레드의 우선순위가 더 높은 경우
	{
		thread_yield(); // 현재 쓰레드는 CPU 양보
	}

	// list_push_back(&ready_list, &t->elem);
	t->status = THREAD_READY;
	intr_set_level(old_level); // 인터럽트 다시 켜기
}
void
thread_unblock (struct thread *t) {
	enum intr_level old_level;

	ASSERT (is_thread (t));

	old_level = intr_disable ();
	ASSERT (t->status == THREAD_BLOCKED);
	list_push_back (&ready_list, &t->elem);
	t->status = THREAD_READY;
	intr_set_level (old_level);
}