From 4580871c15e960621a888faff86f472df406e93f Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 15 Nov 2024 15:02:18 +0100 Subject: [PATCH 1/4] replace tick() usages with run_for_ns --- src/loop.zig | 2 +- src/shell.zig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/loop.zig b/src/loop.zig index 1934043..08a8fd2 100644 --- a/src/loop.zig +++ b/src/loop.zig @@ -66,7 +66,7 @@ pub const SingleThreaded = struct { // on the go when they are executed (ie. nested I/O events). pub fn run(self: *Self) !void { while (self.eventsNb() > 0) { - try self.io.tick(); + try self.io.run_for_ns(10 * std.time.ns_per_ms); // at each iteration we might have new events registred by previous callbacks } // TODO: return instead immediatly on the first JS callback error diff --git a/src/shell.zig b/src/shell.zig index c25b0fe..5e1e31f 100644 --- a/src/shell.zig +++ b/src/shell.zig @@ -224,7 +224,7 @@ pub fn shellExec( // - user input command from repl // - JS callbacks events from scripts while (true) { - try loop.io.tick(); + try loop.io.run_for_ns(10 * std.time.ns_per_ms); if (loop.cbk_error) { if (try try_catch.exception(alloc, js_env.*)) |msg| { defer alloc.free(msg); From af5a58a07699faa5f30e55cfb135013a49cd75a9 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 15 Nov 2024 15:02:33 +0100 Subject: [PATCH 2/4] loop: remove Yield impl --- src/loop.zig | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/src/loop.zig b/src/loop.zig index 08a8fd2..4a12e95 100644 --- a/src/loop.zig +++ b/src/loop.zig @@ -207,44 +207,6 @@ pub const SingleThreaded = struct { } } - // Yield - pub fn Yield(comptime Ctx: type) type { - // TODO check ctx interface funcs: - // - onYield(ctx: *Ctx, ?anyerror) void - return struct { - const YieldImpl = @This(); - const Loop = Self; - - loop: *Loop, - ctx: *Ctx, - completion: IO.Completion, - - pub fn init(loop: *Loop) YieldImpl { - return .{ - .loop = loop, - .completion = undefined, - .ctx = undefined, - }; - } - - pub fn tick(self: *YieldImpl) !void { - return try self.loop.io.tick(); - } - - pub fn yield(self: *YieldImpl, ctx: *Ctx) void { - self.ctx = ctx; - _ = self.loop.addEvent(); - self.loop.io.timeout(*YieldImpl, self, YieldImpl.yieldCbk, &self.completion, 0); - } - - fn yieldCbk(self: *YieldImpl, _: *IO.Completion, result: IO.TimeoutError!void) void { - _ = self.loop.removeEvent(); - _ = result catch |err| return self.ctx.onYield(err); - return self.ctx.onYield(null); - } - }; - } - // Network pub fn Network(comptime Ctx: type) type { From 2c66f4149b713cbb96b73d0c260413d88a2fe1f3 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 15 Nov 2024 15:02:49 +0100 Subject: [PATCH 3/4] loop: remove tick from Network impl --- src/loop.zig | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/loop.zig b/src/loop.zig index 4a12e95..2751919 100644 --- a/src/loop.zig +++ b/src/loop.zig @@ -231,10 +231,6 @@ pub const SingleThreaded = struct { }; } - pub fn tick(self: *NetworkImpl) !void { - return try self.loop.io.tick(); - } - pub fn connect(self: *NetworkImpl, ctx: *Ctx, socket: std.posix.socket_t, address: std.net.Address) void { self.ctx = ctx; _ = self.loop.addEvent(); From d0e006becd9ddac8a4e0ac9890c7b4087e237bd7 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Thu, 21 Nov 2024 12:17:55 +0100 Subject: [PATCH 4/4] loop: Add IO methods Signed-off-by: Francis Bouvier --- src/loop.zig | 127 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 74 insertions(+), 53 deletions(-) diff --git a/src/loop.zig b/src/loop.zig index 2751919..87733d2 100644 --- a/src/loop.zig +++ b/src/loop.zig @@ -44,6 +44,11 @@ pub const SingleThreaded = struct { cbk_error: bool = false, const Self = @This(); + pub const Completion = IO.Completion; + + pub const ConnectError = IO.ConnectError; + pub const RecvError = IO.RecvError; + pub const SendError = IO.SendError; pub fn init(alloc: std.mem.Allocator) !Self { const io = try alloc.create(IO); @@ -97,8 +102,8 @@ pub const SingleThreaded = struct { self.alloc.destroy(ctx); } - // Callback-based APIs - // ------------------- + // JS callbacks APIs + // ----------------- // Timeout @@ -207,65 +212,81 @@ pub const SingleThreaded = struct { } } - // Network - pub fn Network(comptime Ctx: type) type { - - // TODO check ctx interface funcs: - // - onConnect(ctx: *Ctx, ?anyerror) void - // - onReceive(ctx: *Ctx, usize, ?anyerror) void - // - onSend(ctx: *Ctx, usize, ?anyerror) void + // IO callbacks APIs + // ----------------- - return struct { - const NetworkImpl = @This(); - const Loop = Self; + // Connect - loop: *Loop, - ctx: *Ctx, - completion: IO.Completion, + pub fn connect( + self: *Self, + comptime Ctx: type, + ctx: *Ctx, + completion: *Completion, + comptime cbk: fn (ctx: *Ctx, _: *Completion, res: ConnectError!void) void, + socket: std.posix.socket_t, + address: std.net.Address, + ) void { + const old_events_nb = self.addEvent(); + self.io.connect(*Ctx, ctx, cbk, completion, socket, address); + if (builtin.is_test) { + report("start connect {d}", .{old_events_nb + 1}); + } + } - pub fn init(loop: *Loop) NetworkImpl { - return .{ - .loop = loop, - .completion = undefined, - .ctx = undefined, - }; - } + pub fn onConnect(self: *Self, _: ConnectError!void) void { + const old_events_nb = self.removeEvent(); + if (builtin.is_test) { + report("connect done, remaining events: {d}", .{old_events_nb - 1}); + } + } - pub fn connect(self: *NetworkImpl, ctx: *Ctx, socket: std.posix.socket_t, address: std.net.Address) void { - self.ctx = ctx; - _ = self.loop.addEvent(); - self.loop.io.connect(*NetworkImpl, self, NetworkImpl.connectCbk, &self.completion, socket, address); - } + // Send - fn connectCbk(self: *NetworkImpl, _: *IO.Completion, result: IO.ConnectError!void) void { - _ = self.loop.removeEvent(); - _ = result catch |err| return self.ctx.onConnect(err); - return self.ctx.onConnect(null); - } + pub fn send( + self: *Self, + comptime Ctx: type, + ctx: *Ctx, + completion: *Completion, + comptime cbk: fn (ctx: *Ctx, completion: *Completion, res: SendError!usize) void, + socket: std.posix.socket_t, + buf: []const u8, + ) void { + const old_events_nb = self.addEvent(); + self.io.send(*Ctx, ctx, cbk, completion, socket, buf); + if (builtin.is_test) { + report("start send {d}", .{old_events_nb + 1}); + } + } - pub fn receive(self: *NetworkImpl, ctx: *Ctx, socket: std.posix.socket_t, buffer: []u8) void { - self.ctx = ctx; - _ = self.loop.addEvent(); - self.loop.io.recv(*NetworkImpl, self, NetworkImpl.receiveCbk, &self.completion, socket, buffer); - } + pub fn onSend(self: *Self, _: SendError!usize) void { + const old_events_nb = self.removeEvent(); + if (builtin.is_test) { + report("send done, remaining events: {d}", .{old_events_nb - 1}); + } + } - fn receiveCbk(self: *NetworkImpl, _: *IO.Completion, result: IO.RecvError!usize) void { - _ = self.loop.removeEvent(); - const ln = result catch |err| return self.ctx.onReceive(0, err); - return self.ctx.onReceive(ln, null); - } + // Recv - pub fn send(self: *NetworkImpl, ctx: *Ctx, socket: std.posix.socket_t, buffer: []const u8) void { - self.ctx = ctx; - _ = self.loop.addEvent(); - self.loop.io.send(*NetworkImpl, self, NetworkImpl.sendCbk, &self.completion, socket, buffer); - } + pub fn recv( + self: *Self, + comptime Ctx: type, + ctx: *Ctx, + completion: *Completion, + comptime cbk: fn (ctx: *Ctx, completion: *Completion, res: RecvError!usize) void, + socket: std.posix.socket_t, + buf: []u8, + ) void { + const old_events_nb = self.addEvent(); + self.io.recv(*Ctx, ctx, cbk, completion, socket, buf); + if (builtin.is_test) { + report("start recv {d}", .{old_events_nb + 1}); + } + } - fn sendCbk(self: *NetworkImpl, _: *IO.Completion, result: IO.SendError!usize) void { - _ = self.loop.removeEvent(); - const ln = result catch |err| return self.ctx.onSend(0, err); - return self.ctx.onSend(ln, null); - } - }; + pub fn onRecv(self: *Self, _: RecvError!usize) void { + const old_events_nb = self.removeEvent(); + if (builtin.is_test) { + report("recv done, remaining events: {d}", .{old_events_nb - 1}); + } } };