Source
pub fn init(opts: Options) WebServer {
// The upcoming `std.Io` interface should allow us to use `Io.async` and `Io.concurrent`
// instead of threads, so that the web server can function in single-threaded builds.
comptime assert(!builtin.single_threaded);
const all_steps = opts.all_steps;
const step_names_trailing = opts.gpa.alloc(u8, len: {
var name_bytes: usize = 0;
for (all_steps) |step| name_bytes += step.name.len;
break :len name_bytes + all_steps.len * 4;
}) catch @panic("out of memory");
{
const step_name_lens: []align(1) u32 = @ptrCast(step_names_trailing[0 .. all_steps.len * 4]);
var idx: usize = all_steps.len * 4;
for (all_steps, step_name_lens) |step, *name_len| {
name_len.* = @intCast(step.name.len);
@memcpy(step_names_trailing[idx..][0..step.name.len], step.name);
idx += step.name.len;
}
assert(idx == step_names_trailing.len);
}
const step_status_bits = opts.gpa.alloc(
u8,
std.math.divCeil(usize, all_steps.len, 4) catch unreachable,
) catch @panic("out of memory");
@memset(step_status_bits, 0);
const time_reports_len: usize = if (opts.graph.time_report) all_steps.len else 0;
const time_report_msgs = opts.gpa.alloc([]u8, time_reports_len) catch @panic("out of memory");
const time_report_update_times = opts.gpa.alloc(i64, time_reports_len) catch @panic("out of memory");
@memset(time_report_msgs, &.{});
@memset(time_report_update_times, std.math.minInt(i64));
return .{
.gpa = opts.gpa,
.thread_pool = opts.thread_pool,
.graph = opts.graph,
.all_steps = all_steps,
.listen_address = opts.listen_address,
.ttyconf = opts.ttyconf,
.root_prog_node = opts.root_prog_node,
.watch = opts.watch,
.tcp_server = null,
.serve_thread = null,
.base_timestamp = std.time.nanoTimestamp(),
.step_names_trailing = step_names_trailing,
.step_status_bits = step_status_bits,
.fuzz = null,
.time_report_mutex = .{},
.time_report_msgs = time_report_msgs,
.time_report_update_times = time_report_update_times,
.build_status = .init(.idle),
.update_id = .init(0),
.runner_request_mutex = .{},
.runner_request_ready_cond = .{},
.runner_request_empty_cond = .{},
.runner_request = null,
};
}