feature. See also
. The project being documented here (as the example) is the Zig library itself.
Kqueue.init
pub fn init(k: *Kqueue, gpa: Allocator, options: InitOptions) !void
File
Code
pub fn init(k: *Kqueue, gpa: Allocator, options: InitOptions) !void {
assert(options.n_threads != 0);
const n_threads = @max(1, options.n_threads orelse std.Thread.getCpuCount() catch 1);
const threads_size = n_threads * @sizeOf(Thread);
const idle_stack_end_offset = std.mem.alignForward(usize, threads_size + idle_stack_size, std.heap.page_size_max);
const allocated_slice = try gpa.alignedAlloc(u8, .of(Thread), idle_stack_end_offset);
errdefer gpa.free(allocated_slice);
k.* = .{
.gpa = gpa,
.mutex = .init,
.main_fiber_buffer = undefined,
.threads = .{
.allocated = @ptrCast(allocated_slice[0..threads_size]),
.reserved = 1,
.active = 1,
},
};
const main_fiber: *Fiber = @ptrCast(&k.main_fiber_buffer);
main_fiber.* = .{
.required_align = {},
.context = undefined,
.awaiter = null,
.queue_next = null,
.cancel_thread = null,
.awaiting_completions = .empty,
};
const main_thread = &k.threads.allocated[0];
Thread.self = main_thread;
const idle_stack_end: [*]align(16) usize = @ptrCast(@alignCast(allocated_slice[idle_stack_end_offset..].ptr));
(idle_stack_end - 1)[0..1].* = .{@intFromPtr(k)};
main_thread.* = .{
.thread = undefined,
.idle_context = switch (builtin.cpu.arch) {
.aarch64 => .{
.sp = @intFromPtr(idle_stack_end),
.fp = 0,
.pc = @intFromPtr(&mainIdleEntry),
},
.x86_64 => .{
.rsp = @intFromPtr(idle_stack_end - 1),
.rbp = 0,
.rip = @intFromPtr(&mainIdleEntry),
},
else => @compileError("unimplemented architecture"),
},
.current_context = &main_fiber.context,
.ready_queue = null,
.kq_fd = try createFileDescriptor(),
.idle_search_index = 1,
.steal_ready_search_index = 1,
.wait_queues = .empty,
};
errdefer closeFd(main_thread.kq_fd);
std.log.debug("created main idle {*}", .{&main_thread.idle_context});
std.log.debug("created main {*}", .{main_fiber});
}