aboutsummaryrefslogtreecommitdiff
path: root/system/libraries/kata/kata.zig
blob: 39f45567aaabeb439ac35ee3482db407cbb3cb3c (plain)
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
//! Kata (process) control

const sys = @import("sys");

const ERROR_RESULT: u64 = @bitCast(@as(i64, -1));

pub const Error = error{
    SpawnFailed,
    WaitFailed,
};

pub fn yield() void {
    _ = sys.syscall(.yield, .{});
}

pub fn exit(code: u64) noreturn {
    _ = sys.syscall(.exit, .{code});
    unreachable;
}

pub fn spawn(location: []const u8) Error!u32 {
    const result = sys.syscall(.spawn, .{ @intFromPtr(location.ptr), location.len, @as(u64, 0), @as(u64, 0) });
    if (result == ERROR_RESULT) {
        return Error.SpawnFailed;
    }
    return @truncate(result);
}

pub fn spawnWithParams(location: []const u8, params: [][*:0]const u8) Error!u32 {
    const result = sys.syscall(.spawn, .{ @intFromPtr(location.ptr), location.len, @intFromPtr(params.ptr), params.len });
    if (result == ERROR_RESULT) {
        return Error.SpawnFailed;
    }
    return @truncate(result);
}

pub fn wait(pid: u32) Error!u64 {
    while (true) {
        const result = sys.syscall(.wait, .{pid});
        if (result != ERROR_RESULT) {
            return result;
        }
        yield();
    }
}