diff --git a/service/ptos/head/ke/sched.h b/service/ptos/head/ke/sched.h index 8c45664..a9f54f3 100644 --- a/service/ptos/head/ke/sched.h +++ b/service/ptos/head/ke/sched.h @@ -10,6 +10,7 @@ #define _KE_SCHED_H_ 1 #include +#include #include #include @@ -19,15 +20,33 @@ * * @ThreadQueue: Represents the actual queue that holds threads * @QueueEntries: Represents the number of entries in the queue + * @Lock: Lock protecting this queue */ typedef struct { TAILQ_HEAD(, _THREAD) ThreadQueue; USIZE QueueEntries; + SPINLOCK Lock; } SCHED_QUEUE; /* Macro used to initialize the scheduler queue */ #define SCHED_QUEUE_INIT(SQ_P) \ TAILQ_INIT(&(SQ_P)->ThreadQueue); \ - (SQ_P)->QueueEntries = 0; + (SQ_P)->QueueEntries = 0; \ + KeSpinLockInit(&(SQ_P)->Lock, "sched"); + +/* + * Enqueue a thread to a scheduler queue + * + * @Queue: Scheduler queue to add to + * @Thread: Thread to enqueue + */ +VOID KeSchedEnqueue(SCHED_QUEUE *Queue, THREAD *Thread); + +/* + * Dequeue a thread from a scheduler queue + * + * @Queue: Scheduler queue to dequeue from + */ +THREAD *KeSchedDequeue(SCHED_QUEUE *Queue); #endif /* !_KE_SCHED_H_ */ diff --git a/service/ptos/ke/sched.c b/service/ptos/ke/sched.c new file mode 100644 index 0000000..7d7a247 --- /dev/null +++ b/service/ptos/ke/sched.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: Scheduler management + * Author: Chloe M. + */ + +#include + +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; +}