eakova/zig-beam
Zig-Beam: Reusable building blocks for concurrent programming
This repository hosts multiple Zig libraries under one roof. Each library can be used on its own or together via a common wrapper. The workspace is designed to be practical: every library ships with small samples, focused tests, and repeatable benchmarks.
Dependency graph/flow: docs/utils/dependency_graph.md
@import("zig_beam").Utils.Arczig build samples-arczig build bench-arc docs/utils/arc_benchmark_results.mdArc(T).Inner allocations; fronted by a ThreadLocalCache and a global Treiber stack@import("zig_beam").Utils.ArcPoolzig build bench-arc docs/utils/arc_benchmark_results.md@import("zig_beam").Utils.ThreadLocalCachezig build samples-tlczig build bench-tlc docs/utils/thread_local_cache_benchmark_results.md@import("zig_beam").Utils.TaggedPointerzig build samples-tagged@import("zig_beam").Utils.ArcCycleDetectorutils/ — Foundational utilitieszig-rcu/ — Reserved for future libraries (or your own)Each library folder keeps its own build.zig, src/, and docs/ (for reports).
Run from repo root. The commands below target the utils library steps exposed at the top level.
zig build test # run all utils tests
zig build samples-arc # run ARC samples
zig build samples-tlc # run thread-local cache samples
zig build samples-tagged # run tagged pointer samples
zig build bench-tlc # run thread-local cache benchmarks
zig build bench-arc # ARC benchmarks (single-thread)
ARC_BENCH_RUN_MT=1 zig build bench-arc # ARC benchmarks (multi-thread)
For sandboxed or permission-limited environments, set local caches:
export ZIG_GLOBAL_CACHE_DIR=$PWD/.zig-global-cache
export ZIG_LOCAL_CACHE_DIR=$PWD/.zig-local-cache
Windows (PowerShell):
$env:ZIG_GLOBAL_CACHE_DIR = "$PWD/.zig-global-cache"
$env:ZIG_LOCAL_CACHE_DIR = "$PWD/.zig-local-cache"
TaggedPointer (encode/decode):
const beam = @import("zig_beam");
const TaggedPointer = beam.Utils.TaggedPointer;
const Ptr = TaggedPointer(*usize, 1);
var x: usize = 123;
const bits = (try Ptr.new(&x, 1)).toUnsigned();
const decoded = Ptr.fromUnsigned(bits);
// decoded.getPtr() == &x, decoded.getTag() == 1
Thread‑Local Cache (push/pop/clear):
const beam = @import("zig_beam");
const Cache = beam.Utils.ThreadLocalCache(*usize, null);
var cache: Cache = .{};
var v: usize = 1;
_ = cache.push(&v);
_ = cache.pop();
cache.clear(null);
Arc (init/clone/release):
const beam = @import("zig_beam");
const Arc = beam.Utils.Arc(u64);
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit();
const alloc = gpa.allocator();
var a = try Arc.init(alloc, 42); defer a.release();
var b = a.clone(); defer b.release();
// a.get().* == 42, b.get().* == 42
You can consume libraries from this repo via Zig’s package system (recommended) or by vendoring the code. Below shows the package approach with the zig_beam wrapper module (which exposes the utils library under Utils).
build.zig.zon// build.zig.zon (top-level of your app)
.{
.name = "your-app",
.version = "0.1.0",
.dependencies = .{
.zig_beam = .{
// Track a tag or commit tarball (recommended)
.url = "https://github.com/eakova/zig-beam/archive/refs/heads/main.tar.gz",
// Compute and fill the content hash:
// zig fetch https://github.com/eakova/zig-beam/archive/refs/heads/main.tar.gz --save
.hash = "<fill-with-zig-fetch-output>",
},
},
}
Tip: run zig fetch <url> --save to automatically compute and insert the hash.
build.zigconst std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Declare a dependency on zig-beam package
const beam_dep = b.dependency("zig_beam", .{ .target = target, .optimize = optimize });
const beam = beam_dep.module("zig_beam");
// Example: an executable that imports zig_beam
const exe = b.addExecutable(.{
.name = "your-app",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zig_beam", beam);
b.installArtifact(exe);
}
// src/main.zig
const std = @import("std");
const beam = @import("zig_beam");
pub fn main() !void {
// Tagged pointer
const TaggedPointer = beam.Utils.TaggedPointer;
// Thread-local cache
const ThreadLocalCache = beam.Utils.ThreadLocalCache;
// ARC core and pool
const Arc = beam.Utils.Arc;
const ArcPool = beam.Utils.ArcPool;
// minimal smoke check
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit();
const alloc = gpa.allocator();
var arc_u64 = try Arc(u64).init(alloc, 42);
defer arc_u64.release();
std.debug.print("arc.get() = {}\n", .{arc_u64.get().*});
}
zig_beam wrapper re-exports the utils library under Utils so a single import covers Tagged Pointer, Thread-Local Cache, Arc, Arc Pool, and Cycle Detector.docs/utils/*.md:The utils library groups several building blocks used together or separately.
Tagged Pointer
utils/src/tagged-pointer/tagged_pointer.zigzig build test-tagged, zig build samples-taggedThread-Local Cache
utils/src/thread-local-cache/thread_local_cache.zigzig build test-tlc, zig build samples-tlc, zig build bench-tlcdocs/utils/thread_local_cache_benchmark_results.mdARC (Atomic Reference Counted)
utils/src/arc/arc.zig, utils/src/arc/arc-pool/arc_pool.zig, utils/src/arc/cycle-detector/arc_cycle_detector.zigzig build test-arc, zig build test-arc-pool, zig build test-arc-cycle, zig build samples-arc, zig build bench-arcARC_BENCH_RUN_MT=1 zig build bench-arcdocs/utils/arc_benchmark_results.mdSee utils/README.md for module-level details and commands.
ARC_BENCH_RUN_MT=1)
and use only standard library threading (std.Thread).OS support: macOS, Linux, Windows
std.Thread, std.time, std.fs, std.atomic).-Dtarget if you need artifacts for another OS/arch.Zig version policy: pinned to 0.15.x.
src/ — library code (keep public API in files imported by the library’s build.zig)docs/ — generated benchmark reports and diagrams_..._samples.zig with pub fn main() !void so they can run via zig run_..._unit_tests.zig, _..._integration_tests.zig, _..._fuzz_tests.zig (as applicable)_..._benchmarks.zig, write to docs/ and print a short console summary/<lib>/src/ and /<lib>/docs//<lib>/build.zig with targets for:zig build test-<lib>zig build samples-<lib>zig build bench-<lib> (write a Markdown report)_..._samples.zig) with main()/<lib>/README.md with description and commandsContributions are welcome in the form of issues, PRs, and feedback. Please include:
Good first steps:
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Issues and PRs are welcome. Please include: