Skip to content

Commit

Permalink
Fix breaking changes in most recent nightly build (#89)
Browse files Browse the repository at this point in the history
Hi there — big fan of libxev! Noticed some recent Zig changes (namely,
this
[commit](ziglang/zig@265f42d)
+ some other recent ones), broke a bunch of stuff in here. Went through
and did updates to get libxev working with the latest nightly build
(`0.12.0-dev.3191+9cf28d1e9`), and tested the changes on both macOS and
Linux.

Don't have a windows machine, so I expect more changes might be needed.
Also, mlugg seems like he's on a `std.os` refactor spree, so we might
need to do even more updates here eventually.
  • Loading branch information
mitchellh committed Mar 9, 2024
2 parents 4e67818 + 3796c6c commit c0a0779
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 75 deletions.
1 change: 1 addition & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.{
.name = "libxev",
.minimum_zig_version = "0.12.0-dev.3191+9cf28d1e9",
.paths = .{""},
.version = "0.0.0",
}
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/backend/epoll.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub const Loop = struct {

pub fn init(options: xev.Options) !Loop {
var res: Loop = .{
.fd = try std.os.epoll_create1(std.os.O.CLOEXEC),
.fd = try std.os.epoll_create1(std.posix.linux.EPOLL.CLOEXEC),
.thread_pool = options.thread_pool,
.thread_pool_completions = undefined,
.cached_now = undefined,
Expand Down Expand Up @@ -1540,9 +1540,9 @@ test "epoll: timerfd" {

// We'll try with a simple timerfd
const Timerfd = @import("../linux/timerfd.zig").Timerfd;
var t = try Timerfd.init(.monotonic, 0);
var t = try Timerfd.init(.monotonic, .{});
defer t.deinit();
try t.set(0, &.{ .value = .{ .nanoseconds = 1 } }, null);
try t.set(.{}, &.{ .value = .{ .nanoseconds = 1 } }, null);

// Add the timer
var called = false;
Expand Down Expand Up @@ -1591,7 +1591,7 @@ test "epoll: socket accept/connect/send/recv/close" {
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const kernel_backlog = 1;
var ln = try os.socket(address.any.family, os.SOCK.STREAM | os.SOCK.CLOEXEC, 0);
errdefer os.closeSocket(ln);
errdefer os.close(ln);
try os.setsockopt(ln, os.SOL.SOCKET, os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try os.bind(ln, &address.any, address.getOsSockLen());
try os.listen(ln, kernel_backlog);
Expand All @@ -1602,7 +1602,7 @@ test "epoll: socket accept/connect/send/recv/close" {
os.SOCK.NONBLOCK | os.SOCK.STREAM | os.SOCK.CLOEXEC,
0,
);
errdefer os.closeSocket(client_conn);
errdefer os.close(client_conn);

// Accept
var server_conn: os.socket_t = 0;
Expand Down
84 changes: 29 additions & 55 deletions src/backend/io_uring.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const queue = @import("../queue.zig");
const xev = @import("../main.zig").IO_Uring;

pub const Loop = struct {
ring: linux.IO_Uring,
ring: linux.IoUring,

/// The number of active completions. This DOES NOT include completions that
/// are queued in the submissions queue.
Expand Down Expand Up @@ -41,7 +41,7 @@ pub const Loop = struct {
// TODO(mitchellh): add an init_advanced function or something
// for people using the io_uring API directly to be able to set
// the flags for this.
.ring = try linux.IO_Uring.init(entries, 0),
.ring = try linux.IoUring.init(entries, 0),
};
result.update_now();

Expand Down Expand Up @@ -363,35 +363,25 @@ pub const Loop = struct {
return;
},

.accept => |*v| linux.io_uring_prep_accept(
sqe,
.accept => |*v| sqe.prep_accept(
v.socket,
&v.addr,
&v.addr_size,
v.flags,
),

.close => |v| linux.io_uring_prep_close(
sqe,
v.fd,
),
.close => |v| sqe.prep_close(v.fd),

.connect => |*v| linux.io_uring_prep_connect(
sqe,
.connect => |*v| sqe.prep_connect(
v.socket,
&v.addr.any,
v.addr.getOsSockLen(),
),

.poll => |v| linux.io_uring_prep_poll_add(
sqe,
v.fd,
v.events,
),
.poll => |v| sqe.prep_poll_add(v.fd, v.events),

.read => |*v| switch (v.buffer) {
.array => |*buf| linux.io_uring_prep_read(
sqe,
.array => |*buf| sqe.prep_read(
v.fd,
buf,

Expand All @@ -400,8 +390,7 @@ pub const Loop = struct {
@bitCast(@as(i64, -1)),
),

.slice => |buf| linux.io_uring_prep_read(
sqe,
.slice => |buf| sqe.prep_read(
v.fd,
buf,

Expand All @@ -412,56 +401,49 @@ pub const Loop = struct {
},

.pread => |*v| switch (v.buffer) {
.array => |*buf| linux.io_uring_prep_read(
sqe,
.array => |*buf| sqe.prep_read(
v.fd,
buf,
v.offset,
),

.slice => |buf| linux.io_uring_prep_read(
sqe,
.slice => |buf| sqe.prep_read(
v.fd,
buf,
v.offset,
),
},

.recv => |*v| switch (v.buffer) {
.array => |*buf| linux.io_uring_prep_recv(
sqe,
.array => |*buf| sqe.prep_recv(
v.fd,
buf,
0,
),

.slice => |buf| linux.io_uring_prep_recv(
sqe,
.slice => |buf| sqe.prep_recv(
v.fd,
buf,
0,
),
},

.recvmsg => |*v| {
linux.io_uring_prep_recvmsg(
sqe,
sqe.prep_recvmsg(
v.fd,
v.msghdr,
0,
);
},

.send => |*v| switch (v.buffer) {
.array => |*buf| linux.io_uring_prep_send(
sqe,
.array => |*buf| sqe.prep_send(
v.fd,
buf.array[0..buf.len],
0,
),

.slice => |buf| linux.io_uring_prep_send(
sqe,
.slice => |buf| sqe.prep_send(
v.fd,
buf,
0,
Expand All @@ -473,16 +455,14 @@ pub const Loop = struct {
@panic("TODO: sendmsg with buffer");
}

linux.io_uring_prep_sendmsg(
sqe,
sqe.prep_sendmsg(
v.fd,
v.msghdr,
0,
);
},

.shutdown => |v| linux.io_uring_prep_shutdown(
sqe,
.shutdown => |v| sqe.prep_shutdown(
v.socket,
switch (v.how) {
.both => linux.SHUT.RDWR,
Expand All @@ -491,22 +471,19 @@ pub const Loop = struct {
},
),

.timer => |*v| linux.io_uring_prep_timeout(
sqe,
.timer => |*v| sqe.prep_timeout(
&v.next,
0,
linux.IORING_TIMEOUT_ABS,
),

.timer_remove => |v| linux.io_uring_prep_timeout_remove(
sqe,
.timer_remove => |v| sqe.prep_timeout_remove(
@intFromPtr(v.timer),
0,
),

.write => |*v| switch (v.buffer) {
.array => |*buf| linux.io_uring_prep_write(
sqe,
.array => |*buf| sqe.prep_write(
v.fd,
buf.array[0..buf.len],

Expand All @@ -515,8 +492,7 @@ pub const Loop = struct {
@bitCast(@as(i64, -1)),
),

.slice => |buf| linux.io_uring_prep_write(
sqe,
.slice => |buf| sqe.prep_write(
v.fd,
buf,

Expand All @@ -527,22 +503,20 @@ pub const Loop = struct {
},

.pwrite => |*v| switch (v.buffer) {
.array => |*buf| linux.io_uring_prep_write(
sqe,
.array => |*buf| sqe.prep_write(
v.fd,
buf.array[0..buf.len],
v.offset,
),

.slice => |buf| linux.io_uring_prep_write(
sqe,
.slice => |buf| sqe.prep_write(
v.fd,
buf,
v.offset,
),
},

.cancel => |v| linux.io_uring_prep_cancel(sqe, @intCast(@intFromPtr(v.c)), 0),
.cancel => |v| sqe.prep_cancel(@intCast(@intFromPtr(v.c)), 0),
}

// Our sqe user data always points back to the completion.
Expand Down Expand Up @@ -1164,9 +1138,9 @@ test "io_uring: timerfd" {

// We'll try with a simple timerfd
const Timerfd = @import("../linux/timerfd.zig").Timerfd;
var t = try Timerfd.init(.monotonic, 0);
var t = try Timerfd.init(.monotonic, .{});
defer t.deinit();
try t.set(0, &.{ .value = .{ .nanoseconds = 1 } }, null);
try t.set(.{}, &.{ .value = .{ .nanoseconds = 1 } }, null);

// Add the timer
var called = false;
Expand Down Expand Up @@ -1394,14 +1368,14 @@ test "io_uring: socket accept/connect/send/recv/close" {
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const kernel_backlog = 1;
var ln = try os.socket(address.any.family, os.SOCK.STREAM | os.SOCK.CLOEXEC, 0);
errdefer os.closeSocket(ln);
errdefer os.close(ln);
try os.setsockopt(ln, os.SOL.SOCKET, os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try os.bind(ln, &address.any, address.getOsSockLen());
try os.listen(ln, kernel_backlog);

// Create a TCP client socket
var client_conn = try os.socket(address.any.family, os.SOCK.STREAM | os.SOCK.CLOEXEC, 0);
errdefer os.closeSocket(client_conn);
errdefer os.close(client_conn);

// Accept
var server_conn: os.socket_t = 0;
Expand Down Expand Up @@ -1717,7 +1691,7 @@ test "io_uring: socket read cancellation" {
// Create a UDP server socket
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const socket = try os.socket(address.any.family, os.SOCK.DGRAM | os.SOCK.CLOEXEC, 0);
errdefer os.closeSocket(socket);
errdefer os.close(socket);
try os.setsockopt(socket, os.SOL.SOCKET, os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try os.bind(socket, &address.any, address.getOsSockLen());

Expand Down
8 changes: 4 additions & 4 deletions src/backend/iocp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1955,15 +1955,15 @@ test "iocp: socket accept/connect/send/recv/close" {
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const kernel_backlog = 1;
const ln = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
errdefer std.os.closeSocket(ln);
errdefer std.os.close(ln);

try std.os.setsockopt(ln, std.os.SOL.SOCKET, std.os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try std.os.bind(ln, &address.any, address.getOsSockLen());
try std.os.listen(ln, kernel_backlog);

// Create a TCP client socket
const client_conn = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
errdefer std.os.closeSocket(client_conn);
errdefer std.os.close(client_conn);

var server_conn_result: Result = undefined;
var c_accept: Completion = .{
Expand Down Expand Up @@ -2221,7 +2221,7 @@ test "iocp: recv cancellation" {
// Create a TCP server socket
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const socket = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.DGRAM, std.os.IPPROTO.UDP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
errdefer std.os.closeSocket(socket);
errdefer std.os.close(socket);

try std.os.setsockopt(socket, std.os.SOL.SOCKET, std.os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try std.os.bind(socket, &address.any, address.getOsSockLen());
Expand Down Expand Up @@ -2294,7 +2294,7 @@ test "iocp: accept cancellation" {
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const kernel_backlog = 1;
const ln = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
errdefer std.os.closeSocket(ln);
errdefer std.os.close(ln);

try std.os.setsockopt(ln, std.os.SOL.SOCKET, std.os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try std.os.bind(ln, &address.any, address.getOsSockLen());
Expand Down
6 changes: 3 additions & 3 deletions src/backend/kqueue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,7 @@ test "kqueue: socket accept/connect/send/recv/close" {
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const kernel_backlog = 1;
var ln = try os.socket(address.any.family, os.SOCK.STREAM | os.SOCK.CLOEXEC, 0);
errdefer os.closeSocket(ln);
errdefer os.close(ln);
try os.setsockopt(ln, os.SOL.SOCKET, os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try os.bind(ln, &address.any, address.getOsSockLen());
try os.listen(ln, kernel_backlog);
Expand All @@ -2144,7 +2144,7 @@ test "kqueue: socket accept/connect/send/recv/close" {
os.SOCK.NONBLOCK | os.SOCK.STREAM | os.SOCK.CLOEXEC,
0,
);
errdefer os.closeSocket(client_conn);
errdefer os.close(client_conn);

// Accept
var server_conn: os.socket_t = 0;
Expand Down Expand Up @@ -2583,7 +2583,7 @@ test "kqueue: socket accept/cancel cancellation should decrease active count" {
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const kernel_backlog = 1;
var ln = try os.socket(address.any.family, os.SOCK.STREAM | os.SOCK.CLOEXEC, 0);
errdefer os.closeSocket(ln);
errdefer os.close(ln);
try os.setsockopt(ln, os.SOL.SOCKET, os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try os.bind(ln, &address.any, address.getOsSockLen());
try os.listen(ln, kernel_backlog);
Expand Down
Loading

0 comments on commit c0a0779

Please sign in to comment.