Halloween Challenge: Cursed Zig

I’ve got one for you that I literally wrote earlier today:

/// Attempt to start a concurrent task, eventually erroring out if we can't start the task
/// after a stupidly high number of retries.
fn start_task(
	io: std.Io, group: *std.Io.Group,
	task: anytype, args: std.meta.ArgsTuple(@TypeOf(task))
) error{ConcurrencyUnavailable}!void {
	const MAX_RETRIES = @as(usize, 1) << 12;
	var retry_count: usize = 0;

	// Convoluted way to add a task.
	// We optimistically assume concurrency will only be unavailable if we have too
	// many tasks going at once, so if it's unavailable we await the group, then try
	// again.
	// This is probably maximally cursed Zig code.
	while(if(group.concurrent(io, task, args)) |_| false else |_| true) : (retry_count += 1) {
		if(retry_count >= MAX_RETRIES) return error.ConcurrencyUnavailable;
		group.await(io) catch std.log.err("Tasks were somehow cancelled!", .{});
	}{}
}
2 Likes