aboutsummaryrefslogtreecommitdiff
path: root/mirai/crimson/propagate/chain.zig
blob: 5f8f5260f6f9ec9361a463592725320f05f37c62 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Exception Chain (Thread → Kata → Host)

const types = @import("../types/types.zig");
const constants = @import("../constants/constants.zig");
const handlers = @import("../handlers/handlers.zig");
const ports = @import("../ports/ports.zig");
const deliver = @import("deliver.zig");
const wait = @import("wait.zig");

const Exception = types.Exception;
const Port = types.Port;
const Action = constants.Action;
const ExceptionType = constants.ExceptionType;

pub fn propagate_through_chain(exception: *Exception) Action {
    if (try_thread_port(exception)) |action| {
        return action;
    }

    if (try_kata_port(exception)) |action| {
        return action;
    }

    if (try_host_port(exception)) |action| {
        return action;
    }

    return handlers.default_action(exception.exception_type);
}

fn try_thread_port(exception: *Exception) ?Action {
    if (!ports.thread.has_port(exception.thread_id, exception.exception_type)) {
        return null;
    }

    const port = ports.thread.get_port(exception.thread_id, exception.exception_type);
    return deliver_and_wait(exception, port);
}

fn try_kata_port(exception: *Exception) ?Action {
    if (!ports.kata.has_port(exception.kata_id, exception.exception_type)) {
        return null;
    }

    const port = ports.kata.get_port(exception.kata_id, exception.exception_type);
    return deliver_and_wait(exception, port);
}

fn try_host_port(exception: *Exception) ?Action {
    if (!ports.host.has_port(exception.exception_type)) {
        return null;
    }

    const port = ports.host.get_port(exception.exception_type);
    return deliver_and_wait(exception, port);
}

fn deliver_and_wait(exception: *Exception, port: *const Port) Action {
    if (!deliver.send_exception(exception, port)) {
        return handlers.default_action(exception.exception_type);
    }

    return wait.wait_for_reply(exception, port);
}