diff --git a/README.md b/README.md index 505cede..30ebd5d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ +update to 0.9.0 release (2021-12-20) + +updated files: +example_tcp_server.zig +signal_posix.zig + + # pike A minimal cross-platform high-performance async I/O library written in [Zig](https://ziglang.org). @@ -44,4 +51,4 @@ A `Handle`'s implementation is specific to a `Notifier` implementation, though o Subject to the `Notifier` implementation a `Handle`'s implementation falls under, state required to drive asynchronous I/O syscalls through a `Handle` is kept inside a `Handle`. -An example would be an intrusive linked list of suspended asynchronous function frames that are to be resumed upon the recipient of a notification that a file descriptor/handle is ready to be written to/read from. \ No newline at end of file +An example would be an intrusive linked list of suspended asynchronous function frames that are to be resumed upon the recipient of a notification that a file descriptor/handle is ready to be written to/read from. diff --git a/event_epoll.zig b/event_epoll.zig index aa7e354..38795bc 100644 --- a/event_epoll.zig +++ b/event_epoll.zig @@ -42,7 +42,7 @@ pub const Event = struct { } } - fn ErrorUnionOf(comptime func: anytype) std.builtin.TypeInfo.ErrorUnion { + fn ErrorUnionOf(comptime func: anytype) std.builtin.Type.ErrorUnion { return @typeInfo(@typeInfo(@TypeOf(func)).Fn.return_type.?).ErrorUnion; } diff --git a/example_tcp_client.zig b/example_tcp_client.zig index d967af1..576c818 100644 --- a/example_tcp_client.zig +++ b/example_tcp_client.zig @@ -26,7 +26,8 @@ pub fn main() !void { fn run(notifier: *const pike.Notifier, stopped: *bool) !void { defer stopped.* = true; - const address = try net.Address.parseIp("127.0.0.1", 44123); + //const address = try net.Address.parseIp("127.0.0.1", 44123); + const address = try net.Address.parseIp("127.0.0.1", 37701); var socket = try pike.Socket.init(os.AF.INET, os.SOCK.STREAM, os.IPPROTO.TCP, 0); defer socket.deinit(); diff --git a/example_tcp_server.zig b/example_tcp_server.zig index 6043d29..7bd7149 100644 --- a/example_tcp_server.zig +++ b/example_tcp_server.zig @@ -52,10 +52,10 @@ pub const Server = struct { socket: pike.Socket, clients: ClientQueue, - allocator: *mem.Allocator, + allocator: mem.Allocator, frame: @Frame(Server.run), - pub fn init(allocator: *mem.Allocator) !Server { + pub fn init(allocator: mem.Allocator) !Server { var socket = try pike.Socket.init(os.AF.INET, os.SOCK.STREAM, os.IPPROTO.TCP, 0); errdefer socket.deinit(); @@ -123,8 +123,9 @@ pub const Server = struct { pub fn run(notifier: *const pike.Notifier, stopped: *bool) !void { // Setup allocator. - var gpa: heap.GeneralPurposeAllocator(.{}) = .{}; - defer _ = gpa.deinit(); + var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; + var gpa = general_purpose_allocator.allocator(); + defer _=general_purpose_allocator.deinit(); // Setup signal handler. @@ -143,7 +144,8 @@ pub fn run(notifier: *const pike.Notifier, stopped: *bool) !void { // Setup TCP server. - var server = try Server.init(&gpa.allocator); + //var server = try Server.init(gpa.allocator); + var server = try Server.init(gpa); defer server.deinit(); // Start the server, and await for an interrupt signal to gracefully shutdown diff --git a/os/posix.zig b/os/posix.zig index d4c91ce..f33bceb 100644 --- a/os/posix.zig +++ b/os/posix.zig @@ -233,7 +233,7 @@ pub fn getsockopt(comptime T: type, handle: os.socket_t, level: u32, opt: u32) ! var val_len: u32 = @sizeOf(T); const rc = os.system.getsockopt(handle, level, opt, @ptrCast([*]u8, &val), &val_len); - return switch (std.os.linux.getErrno(rc)) { + return switch (std.os.linux.getErrno(@intCast(usize, rc))) { .SUCCESS => val, .BADF => error.BadFileDescriptor, // The argument sockfd is not a valid file descriptor. .FAULT => error.InvalidParameter, // The address pointed to by optval or optlen is not in a valid part of the process address space. diff --git a/os/windows.zig b/os/windows.zig index c20f0a4..96cb168 100644 --- a/os/windows.zig +++ b/os/windows.zig @@ -32,7 +32,7 @@ pub fn loadWinsockExtensionFunction(comptime T: type, sock: ws2_32.SOCKET, guid: const rc = ws2_32.WSAIoctl( sock, @import("windows/ws2_32.zig").SIO_GET_EXTENSION_FUNCTION_POINTER, - @ptrCast(*const c_void, &guid), + @ptrCast(*const anyopaque, &guid), @sizeOf(windows.GUID), &function, @sizeOf(T), diff --git a/os/windows/ws2_32.zig b/os/windows/ws2_32.zig index 0654bdf..dbd01c1 100644 --- a/os/windows/ws2_32.zig +++ b/os/windows/ws2_32.zig @@ -79,7 +79,7 @@ pub const ConnectEx = fn ( s: ws2_32.SOCKET, name: *const ws2_32.sockaddr, namelen: c_int, - lpSendBuffer: ?*c_void, + lpSendBuffer: ?*anyopaque, dwSendDataLength: windows.DWORD, lpdwBytesSent: ?*windows.DWORD, lpOverlapped: *windows.OVERLAPPED, diff --git a/signal_posix.zig b/signal_posix.zig index d9a7780..db043a3 100644 --- a/signal_posix.zig +++ b/signal_posix.zig @@ -46,44 +46,54 @@ pub const Signal = struct { previous: [@bitSizeOf(SignalType)]os.Sigaction, fn handler(signal: c_int) callconv(.C) void { - const current_held = lock.acquire(); + const current_held = lock.lock(); + _=current_held; const current_mask = mask; - current_held.release(); + lock.unlock(); + //current_held.release(); switch (signal) { os.SIG.TERM => { if (!current_mask.terminate) return; - const held = lock.acquire(); + const held = lock.lock(); + _=held; const next_node = waker.wake(.{ .terminate = true }); - held.release(); + lock.unlock(); + //held.release(); if (next_node) |node| pike.dispatch(&node.data, .{}); }, os.SIG.INT => { if (!current_mask.interrupt) return; - const held = lock.acquire(); + const held = lock.lock(); + _=held; const next_node = waker.wake(.{ .interrupt = true }); - held.release(); + lock.unlock(); + //held.release(); if (next_node) |node| pike.dispatch(&node.data, .{}); }, os.SIG.QUIT => { if (!current_mask.quit) return; - const held = lock.acquire(); + const held = lock.lock(); + _=held; const next_node = waker.wake(.{ .quit = true }); - held.release(); + lock.unlock(); + //held.release(); if (next_node) |node| pike.dispatch(&node.data, .{}); }, os.SIG.HUP => { if (!current_mask.hup) return; - const held = lock.acquire(); + const held = lock.lock(); + _=held; const next_node = waker.wake(.{ .hup = true }); - held.release(); + lock.unlock(); + //held.release(); if (next_node) |node| pike.dispatch(&node.data, .{}); }, @@ -92,8 +102,9 @@ pub const Signal = struct { } pub fn init(current: SignalType) !Self { - const held = lock.acquire(); - defer held.release(); + const held = lock.lock(); + _=held; + defer lock.unlock(); const new_mask = @bitCast(SignalType, @bitCast(MaskInt, current) | @bitCast(MaskInt, mask)); @@ -119,7 +130,7 @@ pub const Signal = struct { } pub fn deinit(self: *Self) void { - for (self.previous) |sigaction, i| { + for (self.previous, 0..) |sigaction, i| { os.sigaction( switch (i) { 0 => os.SIG.TERM, @@ -135,19 +146,24 @@ pub const Signal = struct { } pub fn wait(self: *Self) callconv(.Async) !void { - const held = lock.acquire(); + const held = lock.lock(); + _=held; if (waker.wait(self.current)) { - held.release(); + lock.unlock(); + //held.release(); } else { suspend { var node = @TypeOf(waker).FrameNode{ .data = pike.Task.init(@frame()) }; @TypeOf(waker).FrameList.append(&waker.heads, self.current, &node); - held.release(); + lock.unlock(); + //held.release(); } - const next_held = lock.acquire(); + const next_held = lock.lock(); + _=next_held; const next_node = waker.next(self.current); - next_held.release(); + lock.unlock(); + //next_held.release(); if (next_node) |node| { pike.dispatch(&node.data, .{}); diff --git a/socket_posix.zig b/socket_posix.zig index 359b1b3..c1c81bd 100644 --- a/socket_posix.zig +++ b/socket_posix.zig @@ -106,7 +106,7 @@ pub const Socket = struct { } } - fn ErrorUnionOf(comptime func: anytype) std.builtin.TypeInfo.ErrorUnion { + fn ErrorUnionOf(comptime func: anytype) std.builtin.Type.ErrorUnion { return @typeInfo(@typeInfo(@TypeOf(func)).Fn.return_type.?).ErrorUnion; } diff --git a/socket_windows.zig b/socket_windows.zig index cc3b225..eea457e 100644 --- a/socket_windows.zig +++ b/socket_windows.zig @@ -107,7 +107,7 @@ pub const Socket = struct { try notifier.register(&self.handle, .{ .read = true, .write = true }); } - fn ErrorUnionOf(comptime func: anytype) std.builtin.TypeInfo.ErrorUnion { + fn ErrorUnionOf(comptime func: anytype) std.builtin.Type.ErrorUnion { return @typeInfo(@typeInfo(@TypeOf(func)).Fn.return_type.?).ErrorUnion; } diff --git a/waker.zig b/waker.zig index d169156..1eb93c7 100644 --- a/waker.zig +++ b/waker.zig @@ -114,7 +114,7 @@ pub fn PackedWaker(comptime Frame: type, comptime Set: type) type { pub fn wait(self: *Self, set: Set) bool { var any_ready = false; - inline for (set_fields) |field, field_index| { + inline for (set_fields, 0..) |field, field_index| { if (@field(set, field.name) and self.ready[field_index]) { if (self.ready[field_index]) { self.ready[field_index] = false; @@ -128,7 +128,7 @@ pub fn PackedWaker(comptime Frame: type, comptime Set: type) type { pub fn wake(self: *Self, set: Set) ?*FrameList.Node { return FrameList.pop(&self.heads, set) orelse blk: { - inline for (set_fields) |field, field_index| { + inline for (set_fields, 0..) |field, field_index| { if (@field(set, field.name) and self.heads[field_index] == null) { self.ready[field_index] = true; } @@ -139,7 +139,7 @@ pub fn PackedWaker(comptime Frame: type, comptime Set: type) type { } pub fn next(self: *Self, set: Set) ?*FrameList.Node { - inline for (set_fields) |field, field_index| { + inline for (set_fields, 0..) |field, field_index| { if (@field(set, field.name) and self.heads[field_index] == null) { return null; } @@ -228,7 +228,7 @@ fn PackedList(comptime T: type, comptime U: type) type { assert(mem.allEqual(?*Self.Node, &node.prev, null)); assert(mem.allEqual(?*Self.Node, &node.next, null)); - inline for (set_fields) |field, i| { + inline for (set_fields, 0..) |field, i| { if (@field(set, field.name)) { if (heads[i]) |head| { const tail = head.prev[i] orelse unreachable; @@ -249,7 +249,7 @@ fn PackedList(comptime T: type, comptime U: type) type { assert(mem.allEqual(?*Self.Node, &node.prev, null)); assert(mem.allEqual(?*Self.Node, &node.next, null)); - inline for (set_fields) |field, i| { + inline for (set_fields, 0..) |field, i| { if (@field(set, field.name)) { if (heads[i]) |head| { node.prev[i] = head; @@ -266,7 +266,7 @@ fn PackedList(comptime T: type, comptime U: type) type { } pub fn pop(heads: *[set_count]?*Self.Node, set: U) ?*Self.Node { - inline for (set_fields) |field, field_index| { + inline for (set_fields, 0..) |field, field_index| { if (@field(set, field.name) and heads[field_index] != null) { const head = heads[field_index] orelse unreachable;