Repository navigation
Expand file tree
/
Copy pathsignal.zig
More file actions
206 lines (166 loc) · 6.02 KB
/
Copy pathsignal.zig
File metadata and controls
206 lines (166 loc) · 6.02 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const std = @import("std");
const assert = std.debug.assert;
const log = std.log.scoped(.Signal);
const Signal = @import("stdx").Signal;
const UnbufferedChannel = @import("stdx").UnbufferedChannel;
const RingBuffer = @import("stdx").RingBuffer;
const Reply = struct {
result: usize,
};
const Request = struct {
left: usize,
right: usize,
op: Operation,
signal: *Signal(Reply),
};
const Operation = enum {
add,
subtract,
multiply,
divide,
};
const CalculatorState = enum {
running,
closing,
closed,
};
const ITERATIONS = 100_000;
const AsyncCalculator = struct {
const Self = @This();
close_channel: UnbufferedChannel(bool),
done_channel: UnbufferedChannel(bool),
mutex: std.Io.Mutex,
allocator: std.mem.Allocator,
requests: *RingBuffer(Request),
state: CalculatorState,
io: std.Io,
pub fn init(allocator: std.mem.Allocator, io: std.Io) !Self {
const requests = try allocator.create(RingBuffer(Request));
errdefer allocator.destroy(requests);
requests.* = try RingBuffer(Request).initCapacity(allocator, ITERATIONS);
errdefer requests.deinit(allocator);
return Self{
.close_channel = UnbufferedChannel(bool).new(io),
.done_channel = UnbufferedChannel(bool).new(io),
.mutex = .init,
.requests = requests,
.state = .closed,
.allocator = allocator,
.io = io,
};
}
pub fn deinit(self: *Self) void {
self.requests.deinit(self.allocator);
self.allocator.destroy(self.requests);
}
pub fn handleRequest(req: Request) void {
const left = req.left;
const right = req.right;
const rep = switch (req.op) {
.add => Reply{ .result = left + right },
.subtract => Reply{ .result = left - right },
.multiply => Reply{ .result = left * right },
.divide => Reply{ .result = left / right },
};
req.signal.send(rep);
}
pub fn run(self: *Self, ready_channel: *UnbufferedChannel(bool)) void {
self.state = .running;
ready_channel.send(true);
while (true) {
// check if the close channel has received a close command
const close_channel_received = self.close_channel.tryReceive(.fromMilliseconds(0)) catch false;
if (close_channel_received) {
self.state = .closing;
}
switch (self.state) {
.running => {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
while (self.requests.dequeue()) |req| {
AsyncCalculator.handleRequest(req);
}
// This is the tick rate and completely arbitrary
self.io.sleep(.fromMilliseconds(1), .awake) catch unreachable;
},
.closing => {
self.state = .closed;
self.done_channel.send(true);
return;
},
.closed => return,
}
}
}
pub fn close(self: *Self) void {
switch (self.state) {
.closed, .closing => return,
else => {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
self.close_channel.send(true);
},
}
_ = self.done_channel.receive();
}
};
pub fn main(init: std.process.Init) !void {
const io = init.io;
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// spawn the async calculator
var ready_channel = UnbufferedChannel(bool).new(io);
var calculator = try AsyncCalculator.init(allocator, io);
defer calculator.deinit();
const calculator_thread = try std.Thread.spawn(.{}, AsyncCalculator.run, .{ &calculator, &ready_channel });
defer calculator.close();
calculator_thread.detach();
_ = ready_channel.tryReceive(.fromMilliseconds(100)) catch |err| {
calculator.close();
return err;
};
// at this point we know that the calculator is running
var signals = std.array_list.Managed(*Signal(Reply)).init(allocator);
defer signals.deinit();
// at this point we know that the calculator is running
var requests = std.array_list.Managed(Request).init(allocator);
defer requests.deinit();
// everything is setup now
// var timestamp = try std.Io.Timestamp.now(io, .awake);
const enqueue_start = std.Io.Timestamp.now(io, .awake);
// enqueue all the requests at once
{
calculator.mutex.lockUncancelable(io);
defer calculator.mutex.unlock(io);
for (0..ITERATIONS) |_| {
const signal = try allocator.create(Signal(Reply));
errdefer allocator.destroy(signal);
signal.* = Signal(Reply).new(io);
try signals.append(signal);
const req = Request{
.left = 10,
.right = 5,
.op = .add,
.signal = signal,
};
try requests.append(req);
try calculator.requests.enqueue(allocator, req);
}
}
const enqueue_end = std.Io.Timestamp.now(io, .awake);
const await_start = std.Io.Timestamp.now(io, .awake);
// This is the main thread handling each request it received
for (requests.items) |req| {
const rep = req.signal.receive();
assert(rep.result == 15);
allocator.destroy(req.signal);
}
const await_end = std.Io.Timestamp.now(io, .awake);
log.info("total_time: {}ms, enqueue_time: {}ms, await_time {}ms, total iters {}", .{
@divTrunc(await_end.nanoseconds - enqueue_start.nanoseconds, std.time.ns_per_ms),
@divTrunc(enqueue_end.nanoseconds - enqueue_start.nanoseconds, std.time.ns_per_ms),
@divTrunc(await_end.nanoseconds - await_start.nanoseconds, std.time.ns_per_ms),
ITERATIONS,
});
}