-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2pc.zig
More file actions
77 lines (59 loc) · 2.34 KB
/
Copy path2pc.zig
File metadata and controls
77 lines (59 loc) · 2.34 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const std = @import("std");
const polyrole = @import("polyrole");
const Data = polyrole.Data;
const mk2pc = @import("./protocols/two_phase_commit.zig").mk2pc;
const Role = enum { alice, bob, charlie };
const AliceContext = struct {
xoshiro256: std.Random.Xoshiro256 = undefined,
};
const BobContext = struct {
xoshiro256: std.Random.Xoshiro256 = undefined,
};
const CharlieContext = struct {
counter: u32 = 0,
retry_times: u32 = 0,
};
const Context = struct {
alice: type = AliceContext,
bob: type = BobContext,
charlie: type = CharlieContext,
};
pub const EnterFsmState = mk2pc(Role, .charlie, .alice, .bob, Context{}, polyrole.Exit, polyrole.Exit).Begin;
pub const Runner = polyrole.Runner(EnterFsmState);
pub const curr_id = Runner.idFromState(EnterFsmState);
const channel = @import("channel.zig");
const MvarChannelMap = channel.MvarChannelMap(Role);
pub fn main(init: std.process.Init) !void {
const io = init.io;
const gpa = init.gpa;
var counter: std.atomic.Value(usize) = .init(0);
var mvar_channel_map: MvarChannelMap = .init(io, true, &counter);
defer mvar_channel_map.deinit(gpa);
try mvar_channel_map.generate_all_MvarChannel(gpa, 10);
const alice = struct {
fn run(io_: std.Io, mcm: *MvarChannelMap) !void {
var alice_context: AliceContext = undefined;
io_.random(@ptrCast(&alice_context.xoshiro256.s));
try Runner.runProtocol(.alice, null, false, mcm, curr_id, &alice_context);
}
};
const bob = struct {
fn run(io_: std.Io, mcm: *MvarChannelMap) !void {
var bob_context: BobContext = undefined;
io_.random(@ptrCast(&bob_context.xoshiro256.s));
try Runner.runProtocol(.bob, null, false, mcm, curr_id, &bob_context);
}
};
const charlie = struct {
fn run(mcm: *MvarChannelMap) !void {
var charlie_context: CharlieContext = .{};
try Runner.runProtocol(.charlie, null, false, mcm, curr_id, &charlie_context);
}
};
var alice_future = try io.concurrent(alice.run, .{ io, &mvar_channel_map });
var bob_future = try io.concurrent(bob.run, .{ io, &mvar_channel_map });
var charlie_future = try io.concurrent(charlie.run, .{&mvar_channel_map});
try alice_future.await(io);
try bob_future.await(io);
try charlie_future.await(io);
}