-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal.zig
More file actions
175 lines (135 loc) · 5.04 KB
/
signal.zig
File metadata and controls
175 lines (135 loc) · 5.04 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
const std = @import("std");
const assert = std.debug.assert;
const testing = std.testing;
const SignalError = error{
Timeout,
};
pub fn Signal(comptime T: type) type {
return struct {
const Self = @This();
cond: std.Io.Condition,
mutex: std.Io.Mutex,
ready: bool,
value: ?T,
io: std.Io,
pub fn new(io: std.Io) Self {
return .{
.cond = .init,
.mutex = .init,
.ready = false,
.value = null,
.io = io,
};
}
pub fn receive(self: *Self) T {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
while (!self.ready) {
self.cond.waitUncancelable(self.io, &self.mutex);
}
const value = self.value orelse unreachable;
return value;
}
pub fn send(self: *Self, value: T) void {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
assert(!self.ready);
assert(self.value == null);
self.value = value;
self.ready = true;
self.cond.signal(self.io);
}
pub fn tryReceive(self: *Self, timeout: std.Io.Duration) SignalError!T {
const poll_ns = 100_000; // 100us
var now_ts = std.Io.Clock.now(.awake, self.io);
const deadline = now_ts.nanoseconds + timeout.nanoseconds;
while (true) {
self.mutex.lockUncancelable(self.io);
if (self.ready) {
const value = self.value orelse unreachable;
self.mutex.unlock(self.io);
return value;
}
self.mutex.unlock(self.io);
now_ts = std.Io.Clock.now(.awake, self.io);
if (now_ts.nanoseconds >= deadline) {
return SignalError.Timeout;
}
const remaining = deadline - now_ts.nanoseconds;
self.io.sleep(.fromNanoseconds(@min(poll_ns, remaining)), .awake) catch {
return SignalError.Timeout;
};
}
}
pub fn trySend(self: *Self, value: T, timeout: std.Io.Duration) SignalError!void {
_ = timeout;
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
assert(!self.ready);
assert(self.value == null);
self.value = value;
self.ready = true;
self.cond.signal(self.io);
}
};
}
test "basic operation" {
const io = testing.io;
const want: usize = 123;
var signal = Signal(usize).new(io);
signal.send(want);
const got = signal.receive();
try testing.expectEqual(want, got);
}
test "multithreaded support" {
const io = testing.io;
const want: usize = 123;
var signal = Signal(usize).new(io);
const Sender = struct {
fn send(sig: *Signal(usize), value: usize) void {
sig.send(value);
}
fn trySend(sig: *Signal(usize), value: usize, timeout: std.Io.Duration) void {
sig.trySend(value, timeout) catch unreachable;
}
};
const Receiver = struct {
fn receive(sig: *Signal(usize), res: *usize) void {
res.* = sig.receive();
}
fn tryReceive(sig: *Signal(usize), res: *usize, delay: std.Io.Duration) void {
res.* = sig.tryReceive(delay) catch unreachable;
}
};
var result: usize = 0;
try testing.expect(want != result);
const receive_thread = try std.Thread.spawn(.{}, Receiver.receive, .{ &signal, &result });
defer receive_thread.join();
const send_thread = try std.Thread.spawn(.{}, Sender.send, .{ &signal, want });
defer send_thread.join();
const timeout = std.Io.Duration.fromSeconds(1);
var now_ts = std.Io.Clock.now(.awake, io);
const deadline = now_ts.nanoseconds + timeout.nanoseconds;
while (true) {
if (result == want) break;
now_ts = std.Io.Clock.now(.awake, io);
if (now_ts.nanoseconds >= deadline) return error.TestExceededDeadline;
io.sleep(.fromNanoseconds(100_000), .awake) catch {};
}
try testing.expectEqual(want, result);
result = 0;
var timed_signal = Signal(usize).new(io);
const try_receive_thread = try std.Thread.spawn(.{}, Receiver.tryReceive, .{ &timed_signal, &result, timeout });
defer try_receive_thread.join();
const try_send_thread = try std.Thread.spawn(.{}, Sender.trySend, .{ &timed_signal, want, timeout });
defer try_send_thread.join();
now_ts = std.Io.Clock.now(.awake, io);
const deadline2 = now_ts.nanoseconds + timeout.nanoseconds;
while (true) {
if (result == want) break;
now_ts = std.Io.Clock.now(.awake, io);
if (now_ts.nanoseconds >= deadline2) return error.TestExceededDeadline;
io.sleep(.fromNanoseconds(100_000), .awake) catch {};
}
try testing.expectEqual(want, result);
}