Add build.zig to build an example
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const zigmulator = b.addModule("zigmulator", .{
|
||||
.root_source_file = b.path("src/simulator.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "example",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("examples/example.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
}),
|
||||
});
|
||||
exe.root_module.addImport("zigmulator", zigmulator);
|
||||
|
||||
b.installArtifact(exe);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
const Simulator = @import("zigmulator").Simulator;
|
||||
const Simulator = @import("zigmulator");
|
||||
|
||||
fn programA(init: std.process.Init) anyerror!void {
|
||||
var stdout = Io.File.stdout().writerStreaming(init.io, &.{});
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
const Simulator = @import("simulator.zig");
|
||||
|
||||
fn programA(init: std.process.Init) anyerror!void {
|
||||
var stdout = Io.File.stdout().writerStreaming(init.io, &.{});
|
||||
for (0..3) |_| {
|
||||
try stdout.interface.print("Hello from program A!\n", .{});
|
||||
try stdout.interface.flush();
|
||||
}
|
||||
}
|
||||
|
||||
fn programB(init: std.process.Init) anyerror!void {
|
||||
var stdout = Io.File.stdout().writerStreaming(init.io, &.{});
|
||||
for (0..3) |_| {
|
||||
try stdout.interface.print("Hello from program B!\n", .{});
|
||||
try stdout.interface.flush();
|
||||
}
|
||||
}
|
||||
|
||||
fn programC(init: std.process.Init) anyerror!void {
|
||||
var stdout = Io.File.stdout().writerStreaming(init.io, &.{});
|
||||
for (0..3) |_| {
|
||||
try stdout.interface.print("Hello from program C!\n", .{});
|
||||
try stdout.interface.flush();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
var sim: Simulator = undefined;
|
||||
sim.init(std.heap.page_allocator, init.io);
|
||||
defer sim.deinit();
|
||||
|
||||
// Associate executable names to zig entry functions
|
||||
try sim.addExecutable("program_a", programA);
|
||||
try sim.addExecutable("program_b", programB);
|
||||
try sim.addExecutable("program_c", programC);
|
||||
|
||||
// Now run commands in the form:
|
||||
// program_name arg1 arg2 arg3 ...
|
||||
// where program_name is one of the registered functions.
|
||||
try sim.spawn("program_a", .{});
|
||||
try sim.spawn("program_b", .{});
|
||||
try sim.spawn("program_c", .{});
|
||||
|
||||
// Advance the cluster's state by advancing the program's
|
||||
// states one by one. Exits when all programs have returned
|
||||
// or failed.
|
||||
while (sim.scheduleOne()) {}
|
||||
|
||||
std.debug.print("Simulation ended\n", .{});
|
||||
}
|
||||
Reference in New Issue
Block a user