/*
 * Copyright (c) 2026, Chloe M.
 * Provided under the BSD-3 clause.
 *
 * Description: Scheduler management
 * Author:      Chloe M.
 */

#include <ke/sched.h>

VOID
KeSchedEnqueue(SCHED_QUEUE *Queue, THREAD *Thread)
{
    if (Queue == NULL || Thread == NULL) {
        return;
    }

    KeSpinLockAcquire(&Queue->Lock, true);
    TAILQ_INSERT_TAIL(&Queue->ThreadQueue, Thread, ThreadLink);
    KeSpinLockRelease(&Queue->Lock);
}

THREAD *
KeSchedDequeue(SCHED_QUEUE *Queue)
{
    THREAD *Thread;

    KeSpinLockAcquire(&Queue->Lock, true);
    if (TAILQ_EMPTY(&Queue->ThreadQueue)) {
        KeSpinLockRelease(&Queue->Lock);
        return NULL;
    }

    Thread = TAILQ_FIRST(&Queue->ThreadQueue);
    TAILQ_REMOVE(&Queue->ThreadQueue, Thread, ThreadLink);
    KeSpinLockRelease(&Queue->Lock);
    return Thread;
}
