-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexec.zig
45 lines (41 loc) · 1.26 KB
/
exec.zig
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
const report = @import("root").report;
const std = @import("std");
const Task = struct { frame: anyframe, time: i128 };
var task_list = [1]?Task{null} ** 10;
var task_mutex = std.Thread.Mutex{};
pub fn runLoop(endless: bool) void {
report("looping events y'all", .{});
while (true) {
const lock = task_mutex.acquire();
const now = std.time.nanoTimestamp();
var any = false;
var frame = for (task_list) |*task| {
if (task.* != null) {
any = true;
if (task.*.?.time <= now) {
report("sleep over y'all", .{});
var frame = task.*.?.frame;
task.* = null;
break frame;
}
}
} else null;
lock.release();
if (frame != null) resume frame.?;
if (!(endless or any)) break;
}
}
pub fn sleep(ns: u64) void {
suspend {
const lock = task_mutex.acquire();
defer lock.release();
const slot = findSlot();
const time = std.time.nanoTimestamp() + ns;
slot.* = Task{ .frame = @frame(), .time = time };
}
}
fn findSlot() *?Task {
return for (task_list) |*task| {
if (task.* == null) break task;
} else unreachable;
}