-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscheduling.zig
More file actions
50 lines (39 loc) · 1.55 KB
/
scheduling.zig
File metadata and controls
50 lines (39 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const build_options = @import("build_options");
const sbi = @import("sbi");
const std = @import("std");
const thread = @import("thread");
pub fn schedule(current_stack: usize) usize {
const maybe_current_thread = thread.getCurrentThread();
if (maybe_current_thread) |current_thread| {
current_thread.sp_save = current_stack;
if (comptime build_options.enable_debug_logs) {
_ = sbi.debug_print("[I] Enqueueing the current thread\n");
}
thread.enqueueReady(current_thread);
} else {
if (comptime build_options.enable_debug_logs) {
_ = sbi.debug_print("[W] NO CURRENT THREAD AVAILABLE!\n");
}
}
const maybe_new_thread = thread.dequeueReady();
if (maybe_new_thread) |new_thread| {
// TODO: software interrupt to yield to the user thread
if (comptime build_options.enable_debug_logs) {
_ = sbi.debug_print("Yielding to the new thread\n");
}
thread.setCurrentThread(new_thread);
if (comptime build_options.enable_debug_logs) {
var buffer: [256]u8 = undefined;
const content = std.fmt.bufPrint(&buffer, "New thread ID: {d}, stack top: {x}\n", .{ new_thread.id, new_thread.sp_save }) catch {
return 0; // Return bogus stack, should be more robust in reality
};
_ = sbi.debug_print(content);
}
return new_thread.sp_save;
}
_ = sbi.debug_print("NO NEW THREAD AVAILABLE!\n");
while (true) {
asm volatile ("wfi");
}
unreachable;
}