Skip to content

Instantly share code, notes, and snippets.

View kprotty's full-sized avatar

protty kprotty

View GitHub Profile
@kprotty
kprotty / ThreadPool.zig
Last active March 20, 2024 16:12
simple http server compatible with wrk for linux using epoll
const std = @import("std");
const system = switch (std.builtin.os.tag) {
.linux => std.os.linux,
else => std.os.system,
};
const ThreadPool = @This();
max_threads: u16,
counter: u32 = 0,
@kprotty
kprotty / ParkingLot.zig
Last active June 18, 2025 15:46
Small & Fast synchronization primitives for Zig
pub fn ParkingLot(comptime Config: type) type {
return struct {
pub const Lock: type = Config.Lock;
pub const Event: type = Config.Event;
pub const nanotime: fn() u64 = switch (@hasDecl(Config, "nanotime")) {
true => Config.nanotime,
@kprotty
kprotty / thread_pool.zig
Created December 16, 2020 01:19
ThreadPool written inspired by stage2 a bit
const std = @import("std");
pub fn main() !void {
try (try ThreadPool.run(.{}, realMain, .{}));
}
/////// Using the Thread Pool //////
fn realMain() !void {
const WaitGroup = struct {
const std = @import("std");
// usage:
fn asyncMain() !void
{
// Start two interleaving tasks
var task_a = async waitUntilAndPrint(1000, 1200, "task a");
var task_b = async waitUntilAndPrint(500, 1300, "task b");
await task_a;
@kprotty
kprotty / fib.zig
Last active March 20, 2024 16:12
small event loop
const std = @import("std");
const allocator = std.heap.page_allocator;
pub fn main() !void {
const n = 10;
const ret = try (try Task.run(fib, .{n}));
std.debug.warn("fib({}) = {}\n", .{n, ret});
}
fn fib(n: usize) std.mem.Allocator.Error!usize {
@kprotty
kprotty / async_main.zig
Created July 29, 2020 13:49
Zig nanocoroutines
// Bit-vector + rand shuffle order scheduler
const std = @import("std");
const Allocator = std.mem.Allocator;
const workload_size = 1 << 20;
const n_workloads = 4;
const repetition = 4;
const use_async = true;
const use_manual_prefetch = true;
@kprotty
kprotty / client.zig
Created June 7, 2020 19:06
Zig-network throughput testing
const std = @import("std");
const net = @import("./zig-network/network.zig");
const PROTOCOL = net.Protocol.tcp;
pub fn main() !void {
var buf: [1 * 1024 * 1024]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
try net.init();
[sync_mutex_throughput] std::sync::Mutex threads=1 work_per_lock=1
time: [22.027 ns 22.117 ns 22.213 ns]
Found 12 outliers among 100 measurements (12.00%)
6 (6.00%) high mild
6 (6.00%) high severe
[sync_mutex_throughput] parking_lot::Mutex threads=1 work_per_lock=1
time: [19.025 ns 19.935 ns 20.868 ns]
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high mild
@kprotty
kprotty / mutex.zig
Last active March 5, 2020 00:48
Zig Mutex
const std = @import("std");
pub const Mutex = struct {
state: usize,
const MUTEX_LOCK: usize = 0b01;
const QUEUE_LOCK: usize = 0b10;
const QUEUE_MASK: usize = ~@as(usize, @alignOf(Node) - 1);
const Node = struct {
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.net.URL;
import java.net.URLEncoder;