2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("../std.zig");
|
2018-07-09 19:22:44 -07:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const assert = std.debug.assert;
|
2019-02-08 15:18:47 -08:00
|
|
|
const testing = std.testing;
|
2018-07-09 19:22:44 -07:00
|
|
|
const Loop = std.event.Loop;
|
|
|
|
|
2019-10-29 19:59:30 -07:00
|
|
|
/// Many producer, many consumer, thread-safe, runtime configurable buffer size.
|
|
|
|
/// When buffer is empty, consumers suspend and are resumed by producers.
|
|
|
|
/// When buffer is full, producers suspend and are resumed by consumers.
|
2018-07-09 19:22:44 -07:00
|
|
|
pub fn Channel(comptime T: type) type {
|
2018-11-13 05:08:37 -08:00
|
|
|
return struct {
|
2018-07-11 16:38:01 -07:00
|
|
|
getters: std.atomic.Queue(GetNode),
|
2018-08-02 14:04:17 -07:00
|
|
|
or_null_queue: std.atomic.Queue(*std.atomic.Queue(GetNode).Node),
|
2018-07-11 16:38:01 -07:00
|
|
|
putters: std.atomic.Queue(PutNode),
|
2018-07-09 19:22:44 -07:00
|
|
|
get_count: usize,
|
|
|
|
put_count: usize,
|
2020-03-10 13:46:19 -07:00
|
|
|
dispatch_lock: bool,
|
|
|
|
need_dispatch: bool,
|
2018-07-09 19:22:44 -07:00
|
|
|
|
|
|
|
// simple fixed size ring buffer
|
|
|
|
buffer_nodes: []T,
|
|
|
|
buffer_index: usize,
|
|
|
|
buffer_len: usize,
|
|
|
|
|
2018-09-13 13:34:33 -07:00
|
|
|
const SelfChannel = @This();
|
2018-11-13 05:08:37 -08:00
|
|
|
const GetNode = struct {
|
2018-07-09 19:22:44 -07:00
|
|
|
tick_node: *Loop.NextTickNode,
|
2018-08-02 14:04:17 -07:00
|
|
|
data: Data,
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const Data = union(enum) {
|
2018-08-02 14:04:17 -07:00
|
|
|
Normal: Normal,
|
|
|
|
OrNull: OrNull,
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const Normal = struct {
|
2018-08-02 14:04:17 -07:00
|
|
|
ptr: *T,
|
|
|
|
};
|
|
|
|
|
2018-11-13 05:08:37 -08:00
|
|
|
const OrNull = struct {
|
2018-08-02 14:04:17 -07:00
|
|
|
ptr: *?T,
|
|
|
|
or_null: *std.atomic.Queue(*std.atomic.Queue(GetNode).Node).Node,
|
|
|
|
};
|
2018-07-09 19:22:44 -07:00
|
|
|
};
|
2018-11-13 05:08:37 -08:00
|
|
|
const PutNode = struct {
|
2018-07-09 19:22:44 -07:00
|
|
|
data: T,
|
|
|
|
tick_node: *Loop.NextTickNode,
|
|
|
|
};
|
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
const global_event_loop = Loop.instance orelse
|
|
|
|
@compileError("std.event.Channel currently only works with event-based I/O");
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
/// Call `deinit` to free resources when done.
|
|
|
|
/// `buffer` must live until `deinit` is called.
|
|
|
|
/// For a zero length buffer, use `[0]T{}`.
|
|
|
|
/// TODO https://github.com/ziglang/zig/issues/2765
|
|
|
|
pub fn init(self: *SelfChannel, buffer: []T) void {
|
2019-11-27 18:19:08 -08:00
|
|
|
// The ring buffer implementation only works with power of 2 buffer sizes
|
|
|
|
// because of relying on subtracting across zero. For example (0 -% 1) % 10 == 5
|
|
|
|
assert(buffer.len == 0 or @popCount(usize, buffer.len) == 1);
|
|
|
|
|
2019-02-03 13:13:28 -08:00
|
|
|
self.* = SelfChannel{
|
2018-07-09 19:22:44 -07:00
|
|
|
.buffer_len = 0,
|
2019-10-31 08:41:39 -07:00
|
|
|
.buffer_nodes = buffer,
|
2018-07-09 19:22:44 -07:00
|
|
|
.buffer_index = 0,
|
2020-03-10 13:46:19 -07:00
|
|
|
.dispatch_lock = false,
|
|
|
|
.need_dispatch = false,
|
2018-07-11 16:38:01 -07:00
|
|
|
.getters = std.atomic.Queue(GetNode).init(),
|
|
|
|
.putters = std.atomic.Queue(PutNode).init(),
|
2018-08-02 14:04:17 -07:00
|
|
|
.or_null_queue = std.atomic.Queue(*std.atomic.Queue(GetNode).Node).init(),
|
2018-07-09 19:22:44 -07:00
|
|
|
.get_count = 0,
|
|
|
|
.put_count = 0,
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
/// Must be called when all calls to put and get have suspended and no more calls occur.
|
|
|
|
/// This can be omitted if caller can guarantee that the suspended putters and getters
|
|
|
|
/// do not need to be run to completion. Note that this may leave awaiters hanging.
|
|
|
|
pub fn deinit(self: *SelfChannel) void {
|
2018-07-09 19:22:44 -07:00
|
|
|
while (self.getters.get()) |get_node| {
|
2019-08-11 16:53:10 -07:00
|
|
|
resume get_node.data.tick_node.data;
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
while (self.putters.get()) |put_node| {
|
2019-08-11 16:53:10 -07:00
|
|
|
resume put_node.data.tick_node.data;
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
2019-10-31 08:41:39 -07:00
|
|
|
self.* = undefined;
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
|
2019-08-11 16:53:10 -07:00
|
|
|
/// puts a data item in the channel. The function returns when the value has been added to the
|
2018-07-09 19:22:44 -07:00
|
|
|
/// buffer, or in the case of a zero size buffer, when the item has been retrieved by a getter.
|
2019-08-11 16:53:10 -07:00
|
|
|
/// Or when the channel is destroyed.
|
|
|
|
pub fn put(self: *SelfChannel, data: T) void {
|
2019-08-08 13:41:38 -07:00
|
|
|
var my_tick_node = Loop.NextTickNode.init(@frame());
|
2018-11-13 05:08:37 -08:00
|
|
|
var queue_node = std.atomic.Queue(PutNode).Node.init(PutNode{
|
2018-08-02 14:04:17 -07:00
|
|
|
.tick_node = &my_tick_node,
|
|
|
|
.data = data,
|
|
|
|
});
|
|
|
|
|
2018-07-29 01:12:33 -07:00
|
|
|
suspend {
|
2018-07-09 19:22:44 -07:00
|
|
|
self.putters.put(&queue_node);
|
2019-08-11 19:35:12 -07:00
|
|
|
_ = @atomicRmw(usize, &self.put_count, .Add, 1, .SeqCst);
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2018-07-24 18:28:54 -07:00
|
|
|
self.dispatch();
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 16:53:10 -07:00
|
|
|
/// await this function to get an item from the channel. If the buffer is empty, the frame will
|
2018-07-09 19:22:44 -07:00
|
|
|
/// complete when the next item is put in the channel.
|
|
|
|
pub async fn get(self: *SelfChannel) T {
|
2019-08-11 19:35:12 -07:00
|
|
|
// TODO https://github.com/ziglang/zig/issues/2765
|
2018-07-09 19:22:44 -07:00
|
|
|
var result: T = undefined;
|
2019-08-08 13:41:38 -07:00
|
|
|
var my_tick_node = Loop.NextTickNode.init(@frame());
|
2018-11-13 05:08:37 -08:00
|
|
|
var queue_node = std.atomic.Queue(GetNode).Node.init(GetNode{
|
2018-08-02 14:04:17 -07:00
|
|
|
.tick_node = &my_tick_node,
|
2018-11-13 05:08:37 -08:00
|
|
|
.data = GetNode.Data{
|
|
|
|
.Normal = GetNode.Normal{ .ptr = &result },
|
2018-08-02 14:04:17 -07:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-08-02 14:29:31 -07:00
|
|
|
suspend {
|
2018-07-09 19:22:44 -07:00
|
|
|
self.getters.put(&queue_node);
|
2019-08-11 19:35:12 -07:00
|
|
|
_ = @atomicRmw(usize, &self.get_count, .Add, 1, .SeqCst);
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2018-07-24 18:28:54 -07:00
|
|
|
self.dispatch();
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
//pub async fn select(comptime EnumUnion: type, channels: ...) EnumUnion {
|
|
|
|
// assert(@memberCount(EnumUnion) == channels.len); // enum union and channels mismatch
|
|
|
|
// assert(channels.len != 0); // enum unions cannot have 0 fields
|
|
|
|
// if (channels.len == 1) {
|
|
|
|
// const result = await (async channels[0].get() catch unreachable);
|
|
|
|
// return @unionInit(EnumUnion, @memberName(EnumUnion, 0), result);
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
/// Get an item from the channel. If the buffer is empty and there are no
|
|
|
|
/// puts waiting, this returns `null`.
|
|
|
|
pub fn getOrNull(self: *SelfChannel) ?T {
|
2018-08-02 14:04:17 -07:00
|
|
|
// TODO integrate this function with named return values
|
|
|
|
// so we can get rid of this extra result copy
|
|
|
|
var result: ?T = null;
|
2019-08-08 13:41:38 -07:00
|
|
|
var my_tick_node = Loop.NextTickNode.init(@frame());
|
2018-08-02 14:04:17 -07:00
|
|
|
var or_null_node = std.atomic.Queue(*std.atomic.Queue(GetNode).Node).Node.init(undefined);
|
2018-11-13 05:08:37 -08:00
|
|
|
var queue_node = std.atomic.Queue(GetNode).Node.init(GetNode{
|
2018-08-02 14:04:17 -07:00
|
|
|
.tick_node = &my_tick_node,
|
2018-11-13 05:08:37 -08:00
|
|
|
.data = GetNode.Data{
|
|
|
|
.OrNull = GetNode.OrNull{
|
2018-08-02 14:04:17 -07:00
|
|
|
.ptr = &result,
|
|
|
|
.or_null = &or_null_node,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
or_null_node.data = &queue_node;
|
|
|
|
|
2018-08-02 14:29:31 -07:00
|
|
|
suspend {
|
2018-08-02 14:04:17 -07:00
|
|
|
self.getters.put(&queue_node);
|
2019-08-11 19:35:12 -07:00
|
|
|
_ = @atomicRmw(usize, &self.get_count, .Add, 1, .SeqCst);
|
2018-08-02 14:04:17 -07:00
|
|
|
self.or_null_queue.put(&or_null_node);
|
|
|
|
|
|
|
|
self.dispatch();
|
|
|
|
}
|
|
|
|
return result;
|
2018-08-01 13:26:37 -07:00
|
|
|
}
|
|
|
|
|
2018-07-24 18:28:54 -07:00
|
|
|
fn dispatch(self: *SelfChannel) void {
|
2018-07-09 19:22:44 -07:00
|
|
|
// set the "need dispatch" flag
|
2020-03-10 13:46:19 -07:00
|
|
|
@atomicStore(bool, &self.need_dispatch, true, .SeqCst);
|
2018-07-09 19:22:44 -07:00
|
|
|
|
|
|
|
lock: while (true) {
|
|
|
|
// set the lock flag
|
2020-03-12 13:42:01 -07:00
|
|
|
if (@atomicRmw(bool, &self.dispatch_lock, .Xchg, true, .SeqCst)) return;
|
2018-07-09 19:22:44 -07:00
|
|
|
|
|
|
|
// clear the need_dispatch flag since we're about to do it
|
2020-03-10 13:46:19 -07:00
|
|
|
@atomicStore(bool, &self.need_dispatch, false, .SeqCst);
|
2018-07-09 19:22:44 -07:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
one_dispatch: {
|
|
|
|
// later we correct these extra subtractions
|
2019-08-11 19:35:12 -07:00
|
|
|
var get_count = @atomicRmw(usize, &self.get_count, .Sub, 1, .SeqCst);
|
|
|
|
var put_count = @atomicRmw(usize, &self.put_count, .Sub, 1, .SeqCst);
|
2018-07-09 19:22:44 -07:00
|
|
|
|
|
|
|
// transfer self.buffer to self.getters
|
|
|
|
while (self.buffer_len != 0) {
|
|
|
|
if (get_count == 0) break :one_dispatch;
|
|
|
|
|
|
|
|
const get_node = &self.getters.get().?.data;
|
2018-08-02 14:04:17 -07:00
|
|
|
switch (get_node.data) {
|
|
|
|
GetNode.Data.Normal => |info| {
|
2019-11-27 18:19:08 -08:00
|
|
|
info.ptr.* = self.buffer_nodes[(self.buffer_index -% self.buffer_len) % self.buffer_nodes.len];
|
2018-08-02 14:04:17 -07:00
|
|
|
},
|
|
|
|
GetNode.Data.OrNull => |info| {
|
|
|
|
_ = self.or_null_queue.remove(info.or_null);
|
2019-11-27 18:19:08 -08:00
|
|
|
info.ptr.* = self.buffer_nodes[(self.buffer_index -% self.buffer_len) % self.buffer_nodes.len];
|
2018-08-02 14:04:17 -07:00
|
|
|
},
|
|
|
|
}
|
2019-10-31 08:41:39 -07:00
|
|
|
global_event_loop.onNextTick(get_node.tick_node);
|
2018-07-09 19:22:44 -07:00
|
|
|
self.buffer_len -= 1;
|
|
|
|
|
2019-08-11 19:35:12 -07:00
|
|
|
get_count = @atomicRmw(usize, &self.get_count, .Sub, 1, .SeqCst);
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// direct transfer self.putters to self.getters
|
|
|
|
while (get_count != 0 and put_count != 0) {
|
|
|
|
const get_node = &self.getters.get().?.data;
|
|
|
|
const put_node = &self.putters.get().?.data;
|
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
switch (get_node.data) {
|
|
|
|
GetNode.Data.Normal => |info| {
|
|
|
|
info.ptr.* = put_node.data;
|
|
|
|
},
|
|
|
|
GetNode.Data.OrNull => |info| {
|
|
|
|
_ = self.or_null_queue.remove(info.or_null);
|
|
|
|
info.ptr.* = put_node.data;
|
|
|
|
},
|
|
|
|
}
|
2019-10-31 08:41:39 -07:00
|
|
|
global_event_loop.onNextTick(get_node.tick_node);
|
|
|
|
global_event_loop.onNextTick(put_node.tick_node);
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2019-08-11 19:35:12 -07:00
|
|
|
get_count = @atomicRmw(usize, &self.get_count, .Sub, 1, .SeqCst);
|
|
|
|
put_count = @atomicRmw(usize, &self.put_count, .Sub, 1, .SeqCst);
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// transfer self.putters to self.buffer
|
|
|
|
while (self.buffer_len != self.buffer_nodes.len and put_count != 0) {
|
|
|
|
const put_node = &self.putters.get().?.data;
|
|
|
|
|
2019-11-27 18:19:08 -08:00
|
|
|
self.buffer_nodes[self.buffer_index % self.buffer_nodes.len] = put_node.data;
|
2019-10-31 08:41:39 -07:00
|
|
|
global_event_loop.onNextTick(put_node.tick_node);
|
2018-07-09 19:22:44 -07:00
|
|
|
self.buffer_index +%= 1;
|
|
|
|
self.buffer_len += 1;
|
|
|
|
|
2019-08-11 19:35:12 -07:00
|
|
|
put_count = @atomicRmw(usize, &self.put_count, .Sub, 1, .SeqCst);
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// undo the extra subtractions
|
2019-08-11 19:35:12 -07:00
|
|
|
_ = @atomicRmw(usize, &self.get_count, .Add, 1, .SeqCst);
|
|
|
|
_ = @atomicRmw(usize, &self.put_count, .Add, 1, .SeqCst);
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
// All the "get or null" functions should resume now.
|
|
|
|
var remove_count: usize = 0;
|
|
|
|
while (self.or_null_queue.get()) |or_null_node| {
|
|
|
|
remove_count += @boolToInt(self.getters.remove(or_null_node.data));
|
2019-10-31 08:41:39 -07:00
|
|
|
global_event_loop.onNextTick(or_null_node.data.data.tick_node);
|
2018-08-02 14:04:17 -07:00
|
|
|
}
|
|
|
|
if (remove_count != 0) {
|
2019-08-11 19:35:12 -07:00
|
|
|
_ = @atomicRmw(usize, &self.get_count, .Sub, remove_count, .SeqCst);
|
2018-08-02 14:04:17 -07:00
|
|
|
}
|
|
|
|
|
2018-07-09 19:22:44 -07:00
|
|
|
// clear need-dispatch flag
|
2020-03-12 13:42:01 -07:00
|
|
|
if (@atomicRmw(bool, &self.need_dispatch, .Xchg, false, .SeqCst)) continue;
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2020-03-12 13:42:01 -07:00
|
|
|
assert(@atomicRmw(bool, &self.dispatch_lock, .Xchg, false, .SeqCst));
|
2018-07-09 19:22:44 -07:00
|
|
|
|
|
|
|
// we have to check again now that we unlocked
|
2020-03-10 13:46:19 -07:00
|
|
|
if (@atomicLoad(bool, &self.need_dispatch, .SeqCst)) continue :lock;
|
2018-07-09 19:22:44 -07:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.event.Channel" {
|
2020-02-08 13:24:26 -08:00
|
|
|
if (!std.io.is_async) return error.SkipZigTest;
|
|
|
|
|
2019-02-01 14:49:29 -08:00
|
|
|
// https://github.com/ziglang/zig/issues/1908
|
|
|
|
if (builtin.single_threaded) return error.SkipZigTest;
|
2019-10-30 18:29:45 -07:00
|
|
|
|
2019-09-19 10:45:54 -07:00
|
|
|
// https://github.com/ziglang/zig/issues/3251
|
2020-02-24 22:52:27 -08:00
|
|
|
if (builtin.os.tag == .freebsd) return error.SkipZigTest;
|
2019-02-01 14:49:29 -08:00
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
var channel: Channel(i32) = undefined;
|
2020-02-08 13:24:26 -08:00
|
|
|
channel.init(&[0]i32{});
|
2019-10-31 08:41:39 -07:00
|
|
|
defer channel.deinit();
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
var handle = async testChannelGetter(&channel);
|
|
|
|
var putter = async testChannelPutter(&channel);
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
await handle;
|
|
|
|
await putter;
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
|
2019-11-27 18:19:08 -08:00
|
|
|
test "std.event.Channel wraparound" {
|
|
|
|
|
|
|
|
// TODO provide a way to run tests in evented I/O mode
|
|
|
|
if (!std.io.is_async) return error.SkipZigTest;
|
|
|
|
|
|
|
|
const channel_size = 2;
|
|
|
|
|
2019-12-08 19:53:51 -08:00
|
|
|
var buf: [channel_size]i32 = undefined;
|
2019-11-27 18:19:08 -08:00
|
|
|
var channel: Channel(i32) = undefined;
|
|
|
|
channel.init(&buf);
|
|
|
|
defer channel.deinit();
|
|
|
|
|
|
|
|
// add items to channel and pull them out until
|
|
|
|
// the buffer wraps around, make sure it doesn't crash.
|
2019-12-08 19:53:51 -08:00
|
|
|
var result: i32 = undefined;
|
2019-11-27 18:19:08 -08:00
|
|
|
channel.put(5);
|
|
|
|
testing.expectEqual(@as(i32, 5), channel.get());
|
|
|
|
channel.put(6);
|
|
|
|
testing.expectEqual(@as(i32, 6), channel.get());
|
|
|
|
channel.put(7);
|
|
|
|
testing.expectEqual(@as(i32, 7), channel.get());
|
|
|
|
}
|
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
async fn testChannelGetter(channel: *Channel(i32)) void {
|
2019-08-11 19:35:12 -07:00
|
|
|
const value1 = channel.get();
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(value1 == 1234);
|
2018-07-09 19:22:44 -07:00
|
|
|
|
2019-08-11 19:35:12 -07:00
|
|
|
const value2 = channel.get();
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(value2 == 4567);
|
2018-08-02 14:04:17 -07:00
|
|
|
|
2019-08-11 19:35:12 -07:00
|
|
|
const value3 = channel.getOrNull();
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(value3 == null);
|
2018-08-02 14:04:17 -07:00
|
|
|
|
2019-08-16 08:27:29 -07:00
|
|
|
var last_put = async testPut(channel, 4444);
|
2019-08-08 13:41:38 -07:00
|
|
|
const value4 = channel.getOrNull();
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(value4.? == 4444);
|
2018-08-02 14:04:17 -07:00
|
|
|
await last_put;
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn testChannelPutter(channel: *Channel(i32)) void {
|
2019-08-08 13:41:38 -07:00
|
|
|
channel.put(1234);
|
|
|
|
channel.put(4567);
|
2018-07-09 19:22:44 -07:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
async fn testPut(channel: *Channel(i32), value: i32) void {
|
2019-08-08 13:41:38 -07:00
|
|
|
channel.put(value);
|
2018-08-02 14:04:17 -07:00
|
|
|
}
|