pub fn deinit(k: *Kqueue) void k: *Kqueue222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237pub fn deinit(k: *Kqueue) void {
const active_threads = @atomicLoad(u32, &k.threads.active, .acquire);
for (k.threads.allocated[0..active_threads]) |*thread| {
const ready_fiber = @atomicLoad(?*Fiber, &thread.ready_queue, .monotonic);
assert(ready_fiber == null or ready_fiber == Fiber.finished); // pending async
}
k.yield(null, .exit);
const main_thread = &k.threads.allocated[0];
const gpa = k.gpa;
main_thread.deinit(gpa);
const allocated_ptr: [*]align(@alignOf(Thread)) u8 = @ptrCast(@alignCast(k.threads.allocated.ptr));
const idle_stack_end_offset = std.mem.alignForward(usize, k.threads.allocated.len * @sizeOf(Thread) + idle_stack_size, std.heap.page_size_max);
for (k.threads.allocated[1..active_threads]) |*thread| thread.thread.join();
gpa.free(allocated_ptr[0..idle_stack_end_offset]);
k.* = undefined;
}