feature. See also
. The project being documented here (as the example) is the Zig library itself.
Threaded.worker
fn worker(t: *Threaded) void
File
Code
fn worker(t: *Threaded) void {
var thread: Thread = .{
.next = undefined,
.id = std.Thread.getCurrentId(),
.handle = handle: {
if (std.Thread.use_pthreads) break :handle std.c.pthread_self();
if (is_windows) break :handle undefined;
},
.status = .init(.{
.cancelation = .none,
.awaitable = .null,
}),
.cancel_protection = .unblocked,
.futex_waiter = undefined,
.unpark_flag = unpark_flag_init,
.csprng = .uninitialized,
};
Thread.current = &thread;
if (is_windows) {
assert(windows.ntdll.NtOpenThread(
&thread.handle,
.{
.SPECIFIC = .{
.THREAD = .{
.TERMINATE = true,
.ALERT = true,
},
},
},
&.{ .ObjectName = null },
&windows.teb().ClientId,
) == .SUCCESS);
}
defer if (is_windows) {
windows.CloseHandle(thread.handle);
};
{
var head = t.worker_threads.load(.monotonic);
while (true) {
thread.next = head;
head = t.worker_threads.cmpxchgWeak(
head,
&thread,
.release,
.monotonic,
) orelse break;
}
}
defer t.wait_group.finish();
mutexLock(&t.mutex);
defer mutexUnlock(&t.mutex);
while (true) {
while (t.run_queue.popFirst()) |runnable_node| {
mutexUnlock(&t.mutex);
thread.cancel_protection = .unblocked;
const runnable: *Runnable = @fieldParentPtr("node", runnable_node);
runnable.startFn(runnable, &thread, t);
mutexLock(&t.mutex);
t.busy_count -= 1;
}
if (t.join_requested) break;
condWait(&t.cond, &t.mutex);
}
}