2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("../std.zig");
|
2018-07-10 12:17:01 -07:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const Lock = std.event.Lock;
|
2019-02-08 15:18:47 -08:00
|
|
|
const testing = std.testing;
|
2019-11-06 11:41:35 -08:00
|
|
|
const Allocator = std.mem.Allocator;
|
2018-07-10 12:17:01 -07:00
|
|
|
|
2018-07-17 21:34:42 -07:00
|
|
|
/// ReturnType must be `void` or `E!void`
|
2020-02-16 10:25:30 -08:00
|
|
|
/// TODO This API was created back with the old design of async/await, when calling any
|
|
|
|
/// async function required an allocator. There is an ongoing experiment to transition
|
|
|
|
/// all uses of this API to the simpler and more resource-aware `std.event.Batch` API.
|
|
|
|
/// If the transition goes well, all usages of `Group` will be gone, and this API
|
|
|
|
/// will be deleted.
|
2018-07-10 12:17:01 -07:00
|
|
|
pub fn Group(comptime ReturnType: type) type {
|
2018-11-13 05:08:37 -08:00
|
|
|
return struct {
|
2019-08-13 11:14:19 -07:00
|
|
|
frame_stack: Stack,
|
2019-11-06 23:06:22 -08:00
|
|
|
alloc_stack: AllocStack,
|
2018-07-10 12:17:01 -07:00
|
|
|
lock: Lock,
|
2019-11-06 11:41:35 -08:00
|
|
|
allocator: *Allocator,
|
2018-07-10 12:17:01 -07:00
|
|
|
|
2018-09-13 13:34:33 -07:00
|
|
|
const Self = @This();
|
2018-07-10 12:17:01 -07:00
|
|
|
|
|
|
|
const Error = switch (@typeInfo(ReturnType)) {
|
2019-08-11 16:53:10 -07:00
|
|
|
.ErrorUnion => |payload| payload.error_set,
|
2018-07-10 12:17:01 -07:00
|
|
|
else => void,
|
|
|
|
};
|
2019-08-11 16:53:10 -07:00
|
|
|
const Stack = std.atomic.Stack(anyframe->ReturnType);
|
2019-11-06 23:06:22 -08:00
|
|
|
const AllocStack = std.atomic.Stack(Node);
|
|
|
|
|
|
|
|
pub const Node = struct {
|
2020-02-08 13:24:26 -08:00
|
|
|
bytes: []const u8 = &[0]u8{},
|
2019-11-06 23:06:22 -08:00
|
|
|
handle: anyframe->ReturnType,
|
|
|
|
};
|
2018-07-10 12:17:01 -07:00
|
|
|
|
2019-11-06 11:41:35 -08:00
|
|
|
pub fn init(allocator: *Allocator) Self {
|
2018-11-13 05:08:37 -08:00
|
|
|
return Self{
|
2019-08-13 11:14:19 -07:00
|
|
|
.frame_stack = Stack.init(),
|
2019-11-06 23:06:22 -08:00
|
|
|
.alloc_stack = AllocStack.init(),
|
2019-11-06 11:41:35 -08:00
|
|
|
.lock = Lock.init(),
|
|
|
|
.allocator = allocator,
|
2018-07-10 12:17:01 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-11 16:53:10 -07:00
|
|
|
/// Add a frame to the group. Thread-safe.
|
|
|
|
pub fn add(self: *Self, handle: anyframe->ReturnType) (error{OutOfMemory}!void) {
|
2019-11-06 23:06:22 -08:00
|
|
|
const node = try self.allocator.create(AllocStack.Node);
|
|
|
|
node.* = AllocStack.Node{
|
2018-07-10 12:17:01 -07:00
|
|
|
.next = undefined,
|
2019-11-06 23:06:22 -08:00
|
|
|
.data = Node{
|
|
|
|
.handle = handle,
|
|
|
|
},
|
2019-02-03 13:13:28 -08:00
|
|
|
};
|
2018-07-10 12:17:01 -07:00
|
|
|
self.alloc_stack.push(node);
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
/// Add a node to the group. Thread-safe. Cannot fail.
|
2019-08-11 16:53:10 -07:00
|
|
|
/// `node.data` should be the frame handle to add to the group.
|
2019-08-13 11:14:19 -07:00
|
|
|
/// The node's memory should be in the function frame of
|
2018-07-18 21:08:47 -07:00
|
|
|
/// the handle that is in the node, or somewhere guaranteed to live
|
|
|
|
/// at least as long.
|
|
|
|
pub fn addNode(self: *Self, node: *Stack.Node) void {
|
2019-08-13 11:14:19 -07:00
|
|
|
self.frame_stack.push(node);
|
2018-07-18 21:08:47 -07:00
|
|
|
}
|
|
|
|
|
2019-11-06 23:06:22 -08:00
|
|
|
/// This is equivalent to adding a frame to the group but the memory of its frame is
|
|
|
|
/// allocated by the group and freed by `wait`.
|
|
|
|
/// `func` must be async and have return type `ReturnType`.
|
|
|
|
/// Thread-safe.
|
2019-12-09 09:25:57 -08:00
|
|
|
pub fn call(self: *Self, comptime func: var, args: var) error{OutOfMemory}!void {
|
2019-12-09 12:56:19 -08:00
|
|
|
var frame = try self.allocator.create(@TypeOf(@call(.{ .modifier = .async_kw }, func, args)));
|
2019-12-09 09:25:57 -08:00
|
|
|
errdefer self.allocator.destroy(frame);
|
2019-11-06 23:06:22 -08:00
|
|
|
const node = try self.allocator.create(AllocStack.Node);
|
2019-12-09 09:25:57 -08:00
|
|
|
errdefer self.allocator.destroy(node);
|
2019-11-06 23:06:22 -08:00
|
|
|
node.* = AllocStack.Node{
|
|
|
|
.next = undefined,
|
|
|
|
.data = Node{
|
2019-12-09 09:25:57 -08:00
|
|
|
.handle = frame,
|
2019-11-23 12:56:05 -08:00
|
|
|
.bytes = std.mem.asBytes(frame),
|
2019-11-06 23:06:22 -08:00
|
|
|
},
|
|
|
|
};
|
2019-12-09 09:25:57 -08:00
|
|
|
frame.* = @call(.{ .modifier = .async_kw }, func, args);
|
2019-11-06 23:06:22 -08:00
|
|
|
self.alloc_stack.push(node);
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:17:01 -07:00
|
|
|
/// Wait for all the calls and promises of the group to complete.
|
|
|
|
/// Thread-safe.
|
2018-07-22 20:27:58 -07:00
|
|
|
/// Safe to call any number of times.
|
2020-05-04 08:49:27 -07:00
|
|
|
pub fn wait(self: *Self) callconv(.Async) ReturnType {
|
2019-08-11 16:53:10 -07:00
|
|
|
const held = self.lock.acquire();
|
2018-07-10 12:17:01 -07:00
|
|
|
defer held.release();
|
|
|
|
|
2019-08-15 11:05:12 -07:00
|
|
|
var result: ReturnType = {};
|
|
|
|
|
2019-08-13 11:14:19 -07:00
|
|
|
while (self.frame_stack.pop()) |node| {
|
2018-07-10 12:17:01 -07:00
|
|
|
if (Error == void) {
|
|
|
|
await node.data;
|
|
|
|
} else {
|
|
|
|
(await node.data) catch |err| {
|
2019-08-15 11:05:12 -07:00
|
|
|
result = err;
|
2018-07-10 12:17:01 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (self.alloc_stack.pop()) |node| {
|
2019-11-06 23:06:22 -08:00
|
|
|
const handle = node.data.handle;
|
2018-07-10 12:17:01 -07:00
|
|
|
if (Error == void) {
|
|
|
|
await handle;
|
|
|
|
} else {
|
|
|
|
(await handle) catch |err| {
|
2019-08-15 11:05:12 -07:00
|
|
|
result = err;
|
2018-07-10 12:17:01 -07:00
|
|
|
};
|
|
|
|
}
|
2019-11-06 23:06:22 -08:00
|
|
|
self.allocator.free(node.data.bytes);
|
|
|
|
self.allocator.destroy(node);
|
2018-07-10 12:17:01 -07:00
|
|
|
}
|
2019-08-15 11:05:12 -07:00
|
|
|
return result;
|
2018-07-10 12:17:01 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.event.Group" {
|
2019-02-01 14:49:29 -08:00
|
|
|
// https://github.com/ziglang/zig/issues/1908
|
|
|
|
if (builtin.single_threaded) return error.SkipZigTest;
|
|
|
|
|
2019-10-31 08:41:39 -07:00
|
|
|
if (!std.io.is_async) return error.SkipZigTest;
|
2018-07-10 12:17:01 -07:00
|
|
|
|
2020-03-10 16:28:05 -07:00
|
|
|
// TODO this file has bit-rotted. repair it
|
|
|
|
if (true) return error.SkipZigTest;
|
|
|
|
|
2019-11-25 14:25:06 -08:00
|
|
|
const handle = async testGroup(std.heap.page_allocator);
|
2018-07-10 12:17:01 -07:00
|
|
|
}
|
2020-05-04 08:49:27 -07:00
|
|
|
fn testGroup(allocator: *Allocator) callconv(.Async) void {
|
2018-07-10 12:17:01 -07:00
|
|
|
var count: usize = 0;
|
2019-11-06 11:41:35 -08:00
|
|
|
var group = Group(void).init(allocator);
|
2019-08-11 16:53:10 -07:00
|
|
|
var sleep_a_little_frame = async sleepALittle(&count);
|
|
|
|
group.add(&sleep_a_little_frame) catch @panic("memory");
|
|
|
|
var increase_by_ten_frame = async increaseByTen(&count);
|
|
|
|
group.add(&increase_by_ten_frame) catch @panic("memory");
|
|
|
|
group.wait();
|
2019-02-08 15:18:47 -08:00
|
|
|
testing.expect(count == 11);
|
2018-07-10 12:17:01 -07:00
|
|
|
|
2019-11-06 11:41:35 -08:00
|
|
|
var another = Group(anyerror!void).init(allocator);
|
2019-08-11 16:53:10 -07:00
|
|
|
var something_else_frame = async somethingElse();
|
|
|
|
another.add(&something_else_frame) catch @panic("memory");
|
|
|
|
var something_that_fails_frame = async doSomethingThatFails();
|
|
|
|
another.add(&something_that_fails_frame) catch @panic("memory");
|
|
|
|
testing.expectError(error.ItBroke, another.wait());
|
2018-07-10 12:17:01 -07:00
|
|
|
}
|
2020-05-04 08:49:27 -07:00
|
|
|
fn sleepALittle(count: *usize) callconv(.Async) void {
|
2020-05-24 17:06:56 -07:00
|
|
|
std.time.sleep(1 * std.time.ns_per_ms);
|
2019-08-11 16:53:10 -07:00
|
|
|
_ = @atomicRmw(usize, count, .Add, 1, .SeqCst);
|
2018-07-10 12:17:01 -07:00
|
|
|
}
|
2020-05-04 08:49:27 -07:00
|
|
|
fn increaseByTen(count: *usize) callconv(.Async) void {
|
2018-07-10 12:17:01 -07:00
|
|
|
var i: usize = 0;
|
|
|
|
while (i < 10) : (i += 1) {
|
2019-08-11 16:53:10 -07:00
|
|
|
_ = @atomicRmw(usize, count, .Add, 1, .SeqCst);
|
2018-07-10 12:17:01 -07:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 08:49:27 -07:00
|
|
|
fn doSomethingThatFails() callconv(.Async) anyerror!void {}
|
|
|
|
fn somethingElse() callconv(.Async) anyerror!void {
|
2018-07-10 12:17:01 -07:00
|
|
|
return error.ItBroke;
|
|
|
|
}
|