Skip to content

Commit e4a8fb8

Browse files
committed
fix for writergate + delete BoudedArray + update to lastest zig
1 parent a5dea2f commit e4a8fb8

File tree

7 files changed

+35
-442
lines changed

7 files changed

+35
-442
lines changed

docs/pages/coro-blocking-code.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ finishes on a worker thread.
1010

1111
```zig
1212
fn blockingCode() u32 {
13-
std.time.sleep(1 * std.time.ns_per_s);
13+
std.Thread.sleep(1 * std.time.ns_per_s);
1414
return 69;
1515
}
1616

examples/coro_wttr.zig

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ pub const std_options: std.Options = .{
1010

1111
fn getWeather(completed: *std.atomic.Value(u32), allocator: std.mem.Allocator, city: []const u8, lang: []const u8) anyerror![]const u8 {
1212
defer _ = completed.fetchAdd(1, .monotonic);
13-
var url: std.BoundedArray(u8, 256) = .{};
13+
var buf: [256]u8 = undefined;
14+
var url: std.ArrayListUnmanaged(u8) = .initBuffer(&buf);
1415
if (builtin.target.os.tag == .windows) {
15-
try url.writer().print("https://wttr.in/{s}?AFT&lang={s}", .{ city, lang });
16+
try url.fixedWriter().print("https://wttr.in/{s}?AFT&lang={s}", .{ city, lang });
1617
} else {
17-
try url.writer().print("https://wttr.in/{s}?AF&lang={s}", .{ city, lang });
18+
try url.fixedWriter().print("https://wttr.in/{s}?AF&lang={s}", .{ city, lang });
1819
}
1920
var body = std.ArrayList(u8).init(allocator);
2021
errdefer body.deinit();
2122
var client: std.http.Client = .{ .allocator = allocator };
2223
defer client.deinit();
24+
var writer = body.writer().adaptToNewApi();
2325
_ = try client.fetch(.{
24-
.location = .{ .url = url.constSlice() },
25-
.response_storage = .{ .dynamic = &body },
26+
.location = .{ .url = &buf },
27+
.response_writer = &writer.new_interface,
2628
});
2729
return body.toOwnedSlice();
2830
}
@@ -33,9 +35,10 @@ fn getLatestZig(completed: *std.atomic.Value(u32), allocator: std.mem.Allocator)
3335
defer body.deinit();
3436
var client: std.http.Client = .{ .allocator = allocator };
3537
defer client.deinit();
38+
var writer = body.writer().adaptToNewApi();
3639
_ = try client.fetch(.{
3740
.location = .{ .url = "https://ziglang.org/download/index.json" },
38-
.response_storage = .{ .dynamic = &body },
41+
.response_writer = &writer.new_interface,
3942
});
4043
const Index = struct {
4144
master: struct { version: []const u8 },
@@ -102,18 +105,20 @@ pub fn main() !void {
102105
// don't really have to call this, but I want the defer that cleans the progress bar to run
103106
ltask.cancel();
104107

108+
var buf: [std.heap.pageSize()]u8 = undefined;
109+
var writer = std.fs.File.stdout().writer(&buf);
105110
for (tasks.items, 0..) |task, idx| {
106111
if (task.complete(.wait)) |body| {
107112
defer allocator.free(body);
108113
if (idx == 3) {
109-
try std.io.getStdOut().writer().print("\nAaand the current master zig version is... ", .{});
114+
try writer.interface.print("\nAaand the current master zig version is... ", .{});
110115
}
111-
try std.io.getStdOut().writeAll(body);
112-
try std.io.getStdOut().writeAll("\n");
116+
try writer.interface.writeAll(body);
117+
try writer.interface.writeAll("\n");
113118
} else |err| {
114-
try std.io.getStdOut().writer().print("request {} failed with: {}\n", .{ idx, err });
119+
try writer.interface.print("request {} failed with: {}\n", .{ idx, err });
115120
}
116121
}
117122

118-
try std.io.getStdOut().writer().print("\nThat's all folks\n", .{});
123+
try writer.interface.print("\nThat's all folks\n", .{});
119124
}

src/aio/IoUring.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const aio = @import("../aio.zig");
33
const Operation = @import("ops.zig").Operation;
44
const posix = @import("posix/posix.zig");
55
const linux = @import("posix/linux.zig");
6-
const BoundedArray = @import("minilib").BoundedArray;
76
const log = std.log.scoped(.aio_io_uring);
87

98
const Supported = struct {
@@ -160,14 +159,15 @@ pub fn queue(self: *@This(), pairs: anytype, handler: anytype) aio.Error!void {
160159
const saved_sq = self.io.sq;
161160
errdefer self.io.sq = saved_sq;
162161
if (comptime pairs.len > 1) {
163-
var ids: BoundedArray(aio.Id, pairs.len) = .{};
164-
errdefer inline for (ids.constSlice(), pairs) |id, pair| {
162+
var buf: [pairs.len]aio.Id = undefined;
163+
var ids: std.ArrayListUnmanaged(aio.Id) = .initBuffer(&buf);
164+
errdefer inline for (buf, pairs) |id, pair| {
165165
debug("dequeue: {f}: {any}, {s}", .{ id, pair.tag, @tagName(pair.link) });
166166
self.ops.release(id) catch unreachable;
167167
};
168-
inline for (pairs) |pair| ids.append(try self.queueOperation(pair.tag, pair.op, pair.link)) catch unreachable;
168+
inline for (pairs) |pair| ids.appendAssumeCapacity(try self.queueOperation(pair.tag, pair.op, pair.link));
169169
if (@TypeOf(handler) != void) {
170-
inline for (ids.constSlice(), pairs) |id, pair| {
170+
inline for (buf, pairs) |id, pair| {
171171
handler.aio_queue(id, pair.op.userdata);
172172
}
173173
}

src/aio/posix/wasi.zig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,12 @@ fn clock(userdata: usize, timeout: i32) std.os.wasi.subscription_t {
207207
pub fn poll(fds: []std.posix.pollfd, timeout: i32) std.posix.PollError!usize {
208208
// TODO: maybe use thread local arena instead?
209209
const MAX_POLL_FDS = 4096;
210-
var subs: std.BoundedArray(std.os.wasi.subscription_t, MAX_POLL_FDS) = .{};
210+
var buf: [MAX_POLL_FDS]std.os.wasi.subscription_t = undefined;
211+
var subs: std.ArrayListUnmanaged(std.os.wasi.subscription_t) = .initBuffer(&buf);
211212
for (fds) |*pfd| {
212213
pfd.revents = 0;
213214
if (pfd.events & std.posix.POLL.IN != 0) {
214-
subs.append(.{
215+
subs.appendBounded(.{
215216
.userdata = @intFromPtr(pfd),
216217
.u = .{
217218
.tag = .FD_READ,
@@ -220,7 +221,7 @@ pub fn poll(fds: []std.posix.pollfd, timeout: i32) std.posix.PollError!usize {
220221
}) catch return error.SystemResources;
221222
}
222223
if (pfd.events & std.posix.POLL.OUT != 0) {
223-
subs.append(.{
224+
subs.appendBounded(.{
224225
.userdata = @intFromPtr(pfd),
225226
.u = .{
226227
.tag = .FD_WRITE,
@@ -235,12 +236,12 @@ pub fn poll(fds: []std.posix.pollfd, timeout: i32) std.posix.PollError!usize {
235236
}
236237

237238
if (timeout >= 0) {
238-
subs.append(clock(0, timeout)) catch return error.SystemResources;
239+
subs.appendBounded(clock(0, timeout)) catch return error.SystemResources;
239240
}
240241

241242
var n: usize = 0;
242243
var events: [MAX_POLL_FDS]std.os.wasi.event_t = undefined;
243-
switch (std.os.wasi.poll_oneoff(@ptrCast(subs.constSlice().ptr), @ptrCast(events[0..subs.len].ptr), subs.len, &n)) {
244+
switch (std.os.wasi.poll_oneoff(@ptrCast(&buf), @ptrCast(events[0..subs.len].ptr), subs.len, &n)) {
244245
.SUCCESS => {},
245246
else => |e| {
246247
log.err("poll: {}", .{e});

src/aio/uringlator.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const aio = @import("../aio.zig");
55
const Operation = @import("ops.zig").Operation;
66
const DoubleBufferedFixedArrayList = @import("minilib").DoubleBufferedFixedArrayList;
77
const FixedArrayList = @import("minilib").FixedArrayList;
8-
const BoundedArray = @import("minilib").BoundedArray;
98
const posix = @import("posix/posix.zig");
109
const log = std.log.scoped(.aio_uringlator);
1110

@@ -284,16 +283,17 @@ pub fn Uringlator(BackendOperation: type) type {
284283

285284
pub fn queue(self: *@This(), pairs: anytype, backend: anytype, handler: anytype) aio.Error!void {
286285
if (comptime pairs.len > 1) {
287-
var ids: BoundedArray(aio.Id, pairs.len) = .{};
288-
errdefer inline for (ids.constSlice(), pairs) |id, pair| {
289-
debug("dequeue: {f}: {any}, {s} ({?f})", .{ id, pair.tag, @tagName(pair.link), self.prev_id });
286+
var buf: [pairs.len]aio.Id = undefined;
287+
var ids: std.ArrayListUnmanaged(aio.Id) = .initBuffer(&buf);
288+
errdefer inline for (buf, pairs) |id, pair| {
289+
debug("dequeue: {f}: {any}, {s} ({any})", .{ id, pair.tag, @tagName(pair.link), self.prev_id });
290290
backend.uringlator_dequeue(id, pair.tag, pair.op);
291291
self.ops.release(id) catch unreachable;
292292
};
293-
inline for (pairs) |pair| ids.append(try self.queueOperation(pair.tag, pair.op, pair.link, backend)) catch unreachable;
294-
inline for (ids.constSlice()[0..pairs.len]) |id| self.queued.add(id) catch unreachable;
293+
inline for (pairs) |pair| ids.appendAssumeCapacity(try self.queueOperation(pair.tag, pair.op, pair.link, backend));
294+
inline for (buf) |id| self.queued.add(id) catch unreachable;
295295
if (@TypeOf(handler) != void) {
296-
inline for (ids.constSlice(), pairs) |id, pair| handler.aio_queue(id, pair.op.userdata);
296+
inline for (buf, pairs) |id, pair| handler.aio_queue(id, pair.op.userdata);
297297
}
298298
} else {
299299
inline for (pairs) |pair| {

src/minilib.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pub const FixedArrayList = @import("minilib/fixed_array_list.zig").FixedArrayLis
33
pub const DoubleBufferedFixedArrayList = @import("minilib/fixed_array_list.zig").DoubleBufferedFixedArrayList;
44
pub const Id = @import("minilib/id.zig").Id;
55
pub const TimerQueue = @import("minilib/TimerQueue.zig");
6-
pub const BoundedArray = @import("minilib/bounded_array.zig").BoundedArray;
76

87
const std = @import("std");
98

0 commit comments

Comments
 (0)