authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-03 17:13:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-03 17:13:22-07:00
log70f37679035e64bacfc4807709da37bc02fbb346
tree1d8aff79960d20679c576a5911f470fe7d0ab955
parentf3397fad6859c30c1c1d080bbf5b99f262eaa1d8

revert adding std.event.Loop.runDetached

I'd like to discuss this before adding it. I think this is the wrong direction to go with this API.

1 files changed, 0 insertions(+), 53 deletions(-)

lib/std/event/loop.zig-53
...@@ -647,29 +647,6 @@ pub const Loop = struct {...@@ -647,29 +647,6 @@ pub const Loop = struct {
647 }647 }
648 }648 }
649649
650 /// Runs the provided function asynchonously, similarly to Go's "go" operator.
651 /// `func` must return void and it can be an async function.
652 pub fn runDetached(self: *Loop, alloc: *mem.Allocator, comptime func: anytype, args: anytype) error{OutOfMemory}!void {
653 if (!std.io.is_async) @compileError("Can't use runDetached in non-async mode!");
654 if (@TypeOf(@call(.{}, func, args)) != void) {
655 @compileError("`func` must not have a return value");
656 }
657
658 const Wrapper = struct {
659 const Args = @TypeOf(args);
660 fn run(func_args: Args, loop: *Loop, allocator: *mem.Allocator) void {
661 loop.yield();
662 const result = @call(.{}, func, func_args);
663 suspend {
664 allocator.destroy(@frame());
665 }
666 }
667 };
668
669 var run_frame = try alloc.create(@Frame(Wrapper.run));
670 run_frame.* = async Wrapper.run(args, self, alloc);
671 }
672
673 /// Yielding lets the event loop run, starting any unstarted async operations.650 /// Yielding lets the event loop run, starting any unstarted async operations.
674 /// Note that async operations automatically start when a function yields for any other reason,651 /// Note that async operations automatically start when a function yields for any other reason,
675 /// for example, when async I/O is performed. This function is intended to be used only when652 /// for example, when async I/O is performed. This function is intended to be used only when
...@@ -1516,33 +1493,3 @@ fn testEventLoop2(h: anyframe->i32, did_it: *bool) void {...@@ -1516,33 +1493,3 @@ fn testEventLoop2(h: anyframe->i32, did_it: *bool) void {
1516 testing.expect(value == 1234);1493 testing.expect(value == 1234);
1517 did_it.* = true;1494 did_it.* = true;
1518}1495}
1519
1520var testRunDetachedData: usize = 0;
1521test "std.event.Loop - runDetached" {
1522 // https://github.com/ziglang/zig/issues/1908
1523 if (builtin.single_threaded) return error.SkipZigTest;
1524 if (!std.io.is_async) return error.SkipZigTest;
1525 if (true) {
1526 // https://github.com/ziglang/zig/issues/4922
1527 return error.SkipZigTest;
1528 }
1529
1530 var loop: Loop = undefined;
1531 try loop.initMultiThreaded();
1532 defer loop.deinit();
1533
1534 // Schedule the execution, won't actually start until we start the
1535 // event loop.
1536 try loop.runDetached(std.testing.allocator, testRunDetached, .{});
1537
1538 // Now we can start the event loop. The function will return only
1539 // after all tasks have been completed, allowing us to synchonize
1540 // with the previous runDetached.
1541 loop.run();
1542
1543 testing.expect(testRunDetachedData == 1);
1544}
1545
1546fn testRunDetached() void {
1547 testRunDetachedData += 1;
1548}