ptos: sched: Add thread enqueue/dequeue helpers

Signed-off-by: Chloe M <chloe@mensia.org>
This commit is contained in:
2026-07-14 03:07:37 -04:00
parent bc08e8232e
commit e6ac3d0b30
2 changed files with 58 additions and 1 deletions
+20 -1
View File
@@ -10,6 +10,7 @@
#define _KE_SCHED_H_ 1
#include <ps/thread.h>
#include <ke/spinlock.h>
#include <ptdef.h>
#include <queue.h>
@@ -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_ */
+38
View File
@@ -0,0 +1,38 @@
/*
* 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;
}