Skip to content

Commit

Permalink
aio: single-threaded backends and TimerQueue, resurrect wasi support
Browse files Browse the repository at this point in the history
  • Loading branch information
Cloudef committed Feb 5, 2025
1 parent 4dd4f66 commit 1d8a1c6
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 600 deletions.
33 changes: 3 additions & 30 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ const builtin = @import("builtin");
const std = @import("std");

pub fn build(b: *std.Build) void {
var target = b.standardTargetOptions(.{});
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

switch (target.query.os_tag orelse builtin.os.tag) {
.wasi => {
target.query.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.atomics));
target.query.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory));
},
else => {},
}

const sanitize = b.option(bool, "sanitize", "use sanitizers when running examples or tests") orelse false;

var aio_opts = b.addOptions();
Expand All @@ -35,22 +27,11 @@ pub fn build(b: *std.Build) void {
coro_opts.addOption(bool, "debug", debug);
}

var minilib_opts = b.addOptions();
{
const force_foreign_timer_queue = b.option(bool, "minilib:force_foreign_timer_queue", "force the use of foreign timer queue backend") orelse false;
minilib_opts.addOption(bool, "force_foreign_timer_queue", force_foreign_timer_queue);
}

const minilib = b.addModule("minilib", .{
.root_source_file = b.path("src/minilib.zig"),
.target = target,
.optimize = optimize,
.single_threaded = switch (target.query.os_tag orelse builtin.os.tag) {
.linux => null, // io_uring backend can be used without threads
else => false,
},
});
minilib.addImport("build_options", minilib_opts.createModule());

const aio = b.addModule("aio", .{
.root_source_file = b.path("src/aio.zig"),
Expand All @@ -61,7 +42,6 @@ pub fn build(b: *std.Build) void {
.freebsd, .openbsd, .dragonfly, .netbsd => true,
else => false,
},
.single_threaded = minilib.single_threaded,
});
aio.addImport("minilib", minilib);
aio.addImport("build_options", aio_opts.createModule());
Expand Down Expand Up @@ -94,7 +74,6 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
.sanitize_thread = sanitize,
.single_threaded = minilib.single_threaded,
.strip = false,
});
exe.root_module.addImport("aio", aio);
Expand All @@ -114,7 +93,6 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
.filters = &.{test_filter},
.link_libc = aio.link_libc,
.single_threaded = minilib.single_threaded,
.sanitize_thread = sanitize,
.strip = false,
});
Expand All @@ -140,7 +118,6 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
.sanitize_thread = sanitize,
.single_threaded = minilib.single_threaded,
.strip = false,
});
exe.root_module.addImport("aio", aio);
Expand All @@ -163,7 +140,7 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = .ReleaseFast,
.sanitize_thread = sanitize,
.single_threaded = minilib.single_threaded,
.single_threaded = if (sanitize) false else bench != .ticker,
.strip = false,
});
exe.root_module.addImport("aio", aio);
Expand All @@ -187,12 +164,8 @@ const RunStepOptions = struct {
fn runArtifactForStep(b: *std.Build, target: std.Build.ResolvedTarget, step: *std.Build.Step.Compile, opts: RunStepOptions) *std.Build.Step.Run {
return switch (target.query.os_tag orelse builtin.os.tag) {
.wasi => blk: {
step.shared_memory = true;
step.max_memory = std.mem.alignForward(usize, opts.wasm_max_memory, 65536);
step.import_memory = true;
step.export_memory = true;
step.root_module.export_symbol_names = &.{"wasi_thread_start"};
const wasmtime = b.addSystemCommand(&.{ "wasmtime", "-W", "trap-on-grow-failure=y", "-S", "threads=y", "--dir", "." });
const wasmtime = b.addSystemCommand(&.{ "wasmtime", "-W", "trap-on-grow-failure=y", "--dir", "." });
wasmtime.addArtifactArg(step);
break :blk wasmtime;
},
Expand Down
6 changes: 2 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
zig build test bug -Daio:posix=disable -Dsanitize=true
echo "zig build test bug -Daio:posix=force -Dsanitize=true"
zig build test bug -Daio:posix=force -Dsanitize=true
echo "zig build test bug -Daio:posix=force -Dminilib:force_foreign_timer_queue=true -Dsanitize=true"
zig build test bug -Daio:posix=force -Dminilib:force_foreign_timer_queue=true -Dsanitize=true
# echo "zig build test:aio test:minilib -Dtarget=wasm32-wasi-none"
# zig build -Doptimize=ReleaseSafe test:aio test:minilib -Dtarget=wasm32-wasi-none
echo "zig build test:aio test:minilib -Dtarget=wasm32-wasi-none"
zig build -Doptimize=ReleaseSafe test:aio test:minilib -Dtarget=wasm32-wasi-none
elif [[ "$(uname)" == "Darwin" ]]; then
echo "zig build test bug"
zig build test bug
Expand Down
9 changes: 2 additions & 7 deletions src/aio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub const Options = struct {
debug: bool = build_options.debug,
/// Max thread count for a thread pool if a backend requires one.
/// By default use the cpu core count.
/// Use count 1 to disable threading in multi-threaded builds.
/// In single-threaded builds this option is ignored.
max_threads: ?u32 = null,
/// Operations that the main backend must support.
/// If the operations are not supported by a main backend then a posix backend will be used instead.
Expand All @@ -23,13 +25,6 @@ pub const Options = struct {
/// Choose a posix fallback mode.
/// Posix backend is never used on windows
posix: enum { auto, force, disable } = @enumFromInt(@intFromEnum(build_options.posix)),
/// Max kludge threads for the posix backend.
/// Kludge threads are used when operation cannot be polled for readiness.
/// One example is macos's /dev/tty which can only be queried for readiness using select/pselect.
/// <https://lists.apple.com/archives/Darwin-dev/2006/Apr/msg00066.html>
/// <https://nathancraddock.com/blog/macos-dev-tty-polling/>
/// Only used on platforms that need this hack (darwin)
posix_max_kludge_threads: usize = 16,
/// Wasi support
wasi: enum { wasi, wasix } = @enumFromInt(@intFromEnum(build_options.wasi)),
};
Expand Down
Loading

0 comments on commit 1d8a1c6

Please sign in to comment.