summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-05-13 16:20:02 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-05-13 16:20:02 +0800
commit08e4b900baa9819717e35d276d2a02518ef34ecf (patch)
tree4338d2d227a312a877e9880e8bafd73ed63ce187
parent0d572ed18a504ccf7e0046d948f78451edc22df5 (diff)
downloadlume-08e4b900baa9819717e35d276d2a02518ef34ecf.tar.gz
lume-08e4b900baa9819717e35d276d2a02518ef34ecf.tar.xz
feat: add scheduler and scheduler class interfaces with core functions
-rw-r--r--na/include/lume/sched.h73
-rw-r--r--na/include/lume/sched_class.h35
2 files changed, 108 insertions, 0 deletions
diff --git a/na/include/lume/sched.h b/na/include/lume/sched.h
new file mode 100644
index 0000000..0f25e58
--- /dev/null
+++ b/na/include/lume/sched.h
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Copyright (C) 2026 Richard Qin */
+
+#pragma once
+
+/*
+ * Scheduler Public Interface
+ *
+ * Linux-style sched_class core with per-CPU runqueues.
+ * Phase 2: O(1) bitmap class is the default scheduling class.
+ *
+ * Reference: docs/specs/scheduler.md
+ */
+
+#include <lume/types.h>
+#include <lume/config.h>
+
+struct TaskControlBlock;
+
+/* ========================================================================
+ * Scheduler Constants — driven by CONFIG_xxx (see scripts/Kconfig.mk)
+ * ======================================================================== */
+
+inline constexpr int kNumPriorities = CONFIG_NR_PRIORITIES;
+inline constexpr int kDefaultPriority = CONFIG_DEFAULT_PRIORITY;
+inline constexpr int kDefaultTimeSlice = CONFIG_DEFAULT_TIMESLICE;
+inline constexpr int kIdlePriority = CONFIG_IDLE_PRIORITY;
+
+/* ========================================================================
+ * Initialization
+ * ======================================================================== */
+
+/* BSP: initialize all RunQueues, create BSP idle thread and boot TCB. */
+void sched_init();
+
+/* AP: create per-CPU idle thread and boot TCB. */
+void sched_init_ap();
+
+/* ========================================================================
+ * Core Scheduling
+ * ======================================================================== */
+
+/* Main scheduling entry point. Picks the next task and context-switches.
+ * Called voluntarily (yield, sleep, exit) or from trap return path. */
+void schedule();
+
+/* Timer tick handler. Delegates to current task's sched_class.
+ * Called from MI trap layer in interrupt context. */
+void sched_tick();
+
+/* Check if rescheduling is needed (called before returning to user mode). */
+void sched_check_preempt();
+
+/* Voluntarily yield the current time slice. */
+void sched_yield();
+
+/* ========================================================================
+ * Task Queue Management (used by task.cc, waitqueue.cc)
+ * ======================================================================== */
+
+/* Make a task runnable: add it to its CPU's RunQueue. */
+void sched_enqueue(TaskControlBlock* task);
+
+/* Mark a task as sleeping: remove it from the RunQueue. */
+void sched_dequeue(TaskControlBlock* task);
+
+/* ========================================================================
+ * Time Slice Policy
+ * ======================================================================== */
+
+/* Convert priority level to time slice length.
+ * Higher priority (lower number) → longer time slice. */
+int32 priority_to_timeslice(uint8 priority);
diff --git a/na/include/lume/sched_class.h b/na/include/lume/sched_class.h
new file mode 100644
index 0000000..08686c1
--- /dev/null
+++ b/na/include/lume/sched_class.h
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Copyright (C) 2026 Richard Qin */
+
+#pragma once
+
+/*
+ * Scheduler Class Interface (Linux-style)
+ *
+ * Each scheduling algorithm implements a sched_class and is chained
+ * in priority order. The scheduler core walks the chain to select
+ * the next runnable task.
+ */
+
+#include <lume/types.h>
+
+struct RunQueue;
+struct TaskControlBlock;
+
+struct SchedClass {
+ const char* name;
+ const SchedClass* next; // Linked list order: higher priority first
+
+ void (*init_rq)(RunQueue* rq);
+
+ void (*enqueue_task)(RunQueue* rq, TaskControlBlock* task);
+ void (*dequeue_task)(RunQueue* rq, TaskControlBlock* task);
+ TaskControlBlock* (*pick_next_task)(RunQueue* rq);
+ void (*put_prev_task)(RunQueue* rq, TaskControlBlock* task);
+
+ void (*task_tick)(RunQueue* rq, TaskControlBlock* curr);
+ void (*yield_task)(RunQueue* rq, TaskControlBlock* curr);
+};
+
+extern const SchedClass g_sched_class_o1;
+extern const SchedClass g_sched_class_idle;