aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / sched.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* sched.h -- run-queue scheduler, port of crates/aienos-kernel/src/scheduler.rs. |
| 18 | * Pure logic (no hardware): per-CPU queues, three priorities, fair ticks |
| 19 | * among equal priorities, a background budget under higher-priority load, |
| 20 | * placement by CPU efficiency class and work stealing. Host-tested in |
| 21 | * tests/test_sched.c; the M3 threads use it with one CPU. */ |
| 22 | #ifndef AIENOS_CK_SCHED_H |
| 23 | #define AIENOS_CK_SCHED_H |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | #define CK_SCHED_MAX_CPUS 8 |
| 28 | #define CK_SCHED_MAX_TASKS 16 |
| 29 | |
| 30 | enum ck_prio { CK_PRIO_BACKGROUND = 0, CK_PRIO_NORMAL = 1, CK_PRIO_LATENCY = 2 }; |
| 31 | |
| 32 | enum ck_sched_err { |
| 33 | CK_SCHED_OK = 0, |
| 34 | CK_SCHED_NO_CPUS = -1, |
| 35 | CK_SCHED_FULL = -2, |
| 36 | CK_SCHED_DUPLICATE = -3, |
| 37 | CK_SCHED_INVALID_CPU = -4, |
| 38 | CK_SCHED_UNKNOWN = -5, |
| 39 | }; |
| 40 | |
| 41 | struct ck_task { |
| 42 | uint32_t id; |
| 43 | enum ck_prio prio; |
| 44 | unsigned cpu; |
| 45 | uint64_t ticks; |
| 46 | }; |
| 47 | |
| 48 | struct ck_sched { |
| 49 | unsigned cpus, ntasks; |
| 50 | uint8_t classes[CK_SCHED_MAX_CPUS]; |
| 51 | uint8_t used[CK_SCHED_MAX_TASKS]; |
| 52 | struct ck_task tasks[CK_SCHED_MAX_TASKS]; |
| 53 | int queues[CK_SCHED_MAX_CPUS][CK_SCHED_MAX_TASKS]; /* task slots, -1 empty */ |
| 54 | unsigned queue_len[CK_SCHED_MAX_CPUS]; |
| 55 | unsigned bg_wait[CK_SCHED_MAX_CPUS]; |
| 56 | unsigned bg_interval; |
| 57 | unsigned cursor; |
| 58 | }; |
| 59 | |
| 60 | /* cpus 0 is allowed (every placement then fails with CK_SCHED_NO_CPUS). |
| 61 | * Returns -1 if cpus or tasks exceed the maximums. Interval 0 means 1. */ |
| 62 | int ck_sched_init(struct ck_sched *s, unsigned cpus, const uint8_t *classes, unsigned tasks, |
| 63 | unsigned bg_interval); |
| 64 | /* CPU for a new task: Latency -> highest class, Background -> lowest class, |
| 65 | * Normal -> shortest queue; ties from the round-robin cursor. */ |
| 66 | int ck_sched_place(const struct ck_sched *s, enum ck_prio prio); |
| 67 | /* Returns the CPU it was queued on, or an error. */ |
| 68 | int ck_sched_enqueue(struct ck_sched *s, uint32_t id, enum ck_prio prio); |
| 69 | int ck_sched_remove(struct ck_sched *s, uint32_t id); |
| 70 | /* Picks the next task on cpu (stealing if idle): 1 and *id set, 0 if none, |
| 71 | * or an error. The picked task's tick count increases. */ |
| 72 | int ck_sched_tick(struct ck_sched *s, unsigned cpu, uint32_t *id); |
| 73 | /* 1 if a task moved to idle_cpu, 0 if not, or an error. */ |
| 74 | int ck_sched_steal(struct ck_sched *s, unsigned idle_cpu); |
| 75 | const struct ck_task *ck_sched_task(const struct ck_sched *s, uint32_t id); |
| 76 | |
| 77 | #endif |