Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest Zig master #18

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down Expand Up @@ -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.
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.
2 changes: 1 addition & 1 deletion event_epoll.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion example_tcp_client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 7 additions & 5 deletions example_tcp_server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion os/posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion os/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion os/windows/ws2_32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 34 additions & 18 deletions signal_posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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, .{});
},
Expand All @@ -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));

Expand All @@ -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,
Expand All @@ -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, .{});
Expand Down
2 changes: 1 addition & 1 deletion socket_posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion socket_windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions waker.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down