authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-07 14:56:13-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-07 17:33:07-08:00
log2c6304efc798d8a3773ae126661902b3a3214854
tree6f13fca1541bc1444b72616546f74f47650e643c
parentbe0a4dc299c71dd916817ab6c181b8a441f22ddd

std: move posix.kqueue to Io.Kqueue.createFileDescriptor


3 files changed, 25 insertions(+), 21 deletions(-)

lib/std/Build/Watch.zig+3-1
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2
2const std = @import("../std.zig");3const std = @import("../std.zig");
4const Io = std.Io;
3const Step = std.Build.Step;5const Step = std.Build.Step;
4const Allocator = std.mem.Allocator;6const Allocator = std.mem.Allocator;
5const assert = std.debug.assert;7const assert = std.debug.assert;
...@@ -666,7 +668,7 @@ const Os = switch (builtin.os.tag) {...@@ -666,7 +668,7 @@ const Os = switch (builtin.os.tag) {
666 .dir_table = .{},668 .dir_table = .{},
667 .dir_count = 0,669 .dir_count = 0,
668 .os = .{670 .os = .{
669 .kq_fd = try posix.kqueue(),671 .kq_fd = try Io.Kqueue.createFileDescriptor(),
670 .handles = .empty,672 .handles = .empty,
671 },673 },
672 .generation = 0,674 .generation = 0,
lib/std/Io/Kqueue.zig+22-2
...@@ -157,8 +157,11 @@ pub const InitOptions = struct {...@@ -157,8 +157,11 @@ pub const InitOptions = struct {
157 n_threads: ?usize = null,157 n_threads: ?usize = null,
158};158};
159159
160pub const InitError = Allocator.Error || CreateFileDescriptorError;
161
160pub fn init(k: *Kqueue, gpa: Allocator, options: InitOptions) !void {162pub fn init(k: *Kqueue, gpa: Allocator, options: InitOptions) !void {
161 assert(options.n_threads != 0);163 assert(options.n_threads != 0);
164
162 const n_threads = @max(1, options.n_threads orelse std.Thread.getCpuCount() catch 1);165 const n_threads = @max(1, options.n_threads orelse std.Thread.getCpuCount() catch 1);
163 const threads_size = n_threads * @sizeOf(Thread);166 const threads_size = n_threads * @sizeOf(Thread);
164 const idle_stack_end_offset = std.mem.alignForward(usize, threads_size + idle_stack_size, std.heap.page_size_max);167 const idle_stack_end_offset = std.mem.alignForward(usize, threads_size + idle_stack_size, std.heap.page_size_max);
...@@ -204,7 +207,7 @@ pub fn init(k: *Kqueue, gpa: Allocator, options: InitOptions) !void {...@@ -204,7 +207,7 @@ pub fn init(k: *Kqueue, gpa: Allocator, options: InitOptions) !void {
204 },207 },
205 .current_context = &main_fiber.context,208 .current_context = &main_fiber.context,
206 .ready_queue = null,209 .ready_queue = null,
207 .kq_fd = try posix.kqueue(),210 .kq_fd = try createFileDescriptor(),
208 .idle_search_index = 1,211 .idle_search_index = 1,
209 .steal_ready_search_index = 1,212 .steal_ready_search_index = 1,
210 .wait_queues = .empty,213 .wait_queues = .empty,
...@@ -231,6 +234,23 @@ pub fn deinit(k: *Kqueue) void {...@@ -231,6 +234,23 @@ pub fn deinit(k: *Kqueue) void {
231 k.* = undefined;234 k.* = undefined;
232}235}
233236
237pub const CreateFileDescriptorError = error{
238 /// The per-process limit on the number of open file descriptors has been reached.
239 ProcessFdQuotaExceeded,
240 /// The system-wide limit on the total number of open files has been reached.
241 SystemFdQuotaExceeded,
242} || Io.Unexpected;
243
244pub fn createFileDescriptor() CreateFileDescriptorError!posix.fd_t {
245 const rc = posix.system.kqueue();
246 switch (posix.errno(rc)) {
247 .SUCCESS => return @intCast(rc),
248 .MFILE => return error.ProcessFdQuotaExceeded,
249 .NFILE => return error.SystemFdQuotaExceeded,
250 else => |err| return posix.unexpectedErrno(err),
251 }
252}
253
234fn findReadyFiber(k: *Kqueue, thread: *Thread) ?*Fiber {254fn findReadyFiber(k: *Kqueue, thread: *Thread) ?*Fiber {
235 if (@atomicRmw(?*Fiber, &thread.ready_queue, .Xchg, Fiber.finished, .acquire)) |ready_fiber| {255 if (@atomicRmw(?*Fiber, &thread.ready_queue, .Xchg, Fiber.finished, .acquire)) |ready_fiber| {
236 @atomicStore(?*Fiber, &thread.ready_queue, ready_fiber.queue_next, .release);256 @atomicStore(?*Fiber, &thread.ready_queue, ready_fiber.queue_next, .release);
...@@ -334,7 +354,7 @@ fn schedule(k: *Kqueue, thread: *Thread, ready_queue: Fiber.Queue) void {...@@ -334,7 +354,7 @@ fn schedule(k: *Kqueue, thread: *Thread, ready_queue: Fiber.Queue) void {
334 .idle_context = undefined,354 .idle_context = undefined,
335 .current_context = &new_thread.idle_context,355 .current_context = &new_thread.idle_context,
336 .ready_queue = ready_queue.head,356 .ready_queue = ready_queue.head,
337 .kq_fd = posix.kqueue() catch |err| {357 .kq_fd = createFileDescriptor() catch |err| {
338 @atomicStore(u32, &k.threads.reserved, new_thread_index, .release);358 @atomicStore(u32, &k.threads.reserved, new_thread_index, .release);
339 // no more access to `thread` after giving up reservation359 // no more access to `thread` after giving up reservation
340 std.log.warn("unable to create worker thread due to kqueue init failure: {t}", .{err});360 std.log.warn("unable to create worker thread due to kqueue init failure: {t}", .{err});
lib/std/posix.zig-18
...@@ -792,24 +792,6 @@ pub fn fstat(fd: fd_t) FStatError!Stat {...@@ -792,24 +792,6 @@ pub fn fstat(fd: fd_t) FStatError!Stat {
792 }792 }
793}793}
794794
795pub const KQueueError = error{
796 /// The per-process limit on the number of open file descriptors has been reached.
797 ProcessFdQuotaExceeded,
798
799 /// The system-wide limit on the total number of open files has been reached.
800 SystemFdQuotaExceeded,
801} || UnexpectedError;
802
803pub fn kqueue() KQueueError!i32 {
804 const rc = system.kqueue();
805 switch (errno(rc)) {
806 .SUCCESS => return @intCast(rc),
807 .MFILE => return error.ProcessFdQuotaExceeded,
808 .NFILE => return error.SystemFdQuotaExceeded,
809 else => |err| return unexpectedErrno(err),
810 }
811}
812
813pub const KEventError = error{795pub const KEventError = error{
814 /// The process does not have permission to register a filter.796 /// The process does not have permission to register a filter.
815 AccessDenied,797 AccessDenied,