xKit

Welcome to the xKit documentation. xKit is a collection of low-level C building blocks for event-driven, asynchronous programming on macOS and Linux. (Windows is on the roadmap but not a near-term priority).

  • Designed and reviewed by Leo X.
  • Coded by Codebuddy with claude-4.6-opus

Architecture Overview

graph TD
    subgraph "Application Layer"
        APP["User Application"]
    end

    subgraph "High-Level Modules"
        XHTTP["xhttp<br/>HTTP Client &amp; Server &amp; WebSocket"]
        XLOG["xlog<br/>Async Logging"]
    end

    subgraph "Networking Layer"
        XNET["xnet<br/>URL / DNS / TLS Config / TCP"]
    end

    subgraph "Buffer Layer"
        XBUF["xbuf<br/>Buffer Primitives"]
    end

    subgraph "Core Layer"
        XBASE["xbase<br/>Core Primitives"]
    end

    APP --> XHTTP
    APP --> XLOG
    APP --> XNET
    APP --> XBUF
    APP --> XBASE
    XHTTP --> XNET
    XHTTP --> XBASE
    XHTTP --> XBUF
    XNET --> XBASE
    XLOG --> XBASE
    XBUF -->|"atomic.h"| XBASE

    style XBASE fill:#50b86c,color:#fff
    style XBUF fill:#4a90d9,color:#fff
    style XNET fill:#e74c3c,color:#fff
    style XHTTP fill:#f5a623,color:#fff
    style XLOG fill:#9b59b6,color:#fff

Module Index

xbase — Core Primitives

The foundation of xKit. Provides event loop, timers, tasks, async sockets, memory management, and lock-free data structures.

Sub-ModuleDescription
event.hCross-platform event loop — kqueue (macOS) / epoll (Linux) / poll (fallback)
timer.hMonotonic timer with Push (thread-pool) and Poll (lock-free MPSC) fire modes
task.hN:M task model — lightweight tasks multiplexed onto a thread pool
socket.hAsync socket abstraction with idle-timeout support
memory.hReference-counted allocation with vtable-driven lifecycle
error.hUnified error codes and human-readable messages
heap.hMin-heap with index tracking (used by timer subsystem)
mpsc.hLock-free multi-producer / single-consumer queue
atomic.hCompiler-portable atomic operations (GCC/Clang builtins)
log.hPer-thread callback-based logging with optional backtrace
backtrace.hPlatform-adaptive stack trace (libunwind > execinfo > stub)
time.hTime utilities: xMonoMs() (monotonic) and xWallMs() (wall-clock)

xbuf — Buffer Primitives

Three buffer types for different I/O patterns — linear, ring, and block-chain.

Sub-ModuleDescription
buf.hLinear auto-growing byte buffer with 2× expansion
ring.hFixed-size ring buffer with power-of-2 mask indexing
io.hReference-counted block-chain I/O buffer with zero-copy split/cut

xnet — Networking Primitives

Shared networking utilities: URL parser, async DNS resolver, and TLS configuration types used by higher-level modules.

Sub-ModuleDescription
url.hLightweight URL parser with zero-copy component extraction
dns.hAsync DNS resolution via thread-pool offload
tls.hShared TLS configuration types (client & server)
tcp.hAsync TCP connection, connector & listener with optional TLS

xhttp — Async HTTP Client & Server & WebSocket

Full-featured async HTTP framework: libcurl-powered client with SSE streaming, event-driven server with HTTP/1.1 & HTTP/2 (h2c), TLS support (OpenSSL / mbedTLS), and RFC 6455 WebSocket (server & client).

Sub-ModuleDescription
client.hAsync HTTP client (GET / POST / PUT / DELETE / PATCH / HEAD)
sse.cSSE streaming client with W3C-compliant event parsing
server.hEvent-driven HTTP server with HTTP/1.1 and HTTP/2 (h2c)
ws.hRFC 6455 WebSocket server with handler-initiated upgrade
ws.hRFC 6455 WebSocket client with async connect
transport.hPluggable TLS transport layer (OpenSSL / mbedTLS / plain)

xlog — Async Logging

High-performance async logger with MPSC queue, three flush modes, and file rotation.

Sub-ModuleDescription
logger.hAsync logger with Timer / Notify / Mixed modes and XLOG_* macros

bench — End-to-End Benchmarks

End-to-end benchmark results comparing xKit against other frameworks in real-world scenarios.

BenchmarkDescription
HTTP/1.1 ServerxKit single-threaded HTTP/1.1 server vs Go net/http — GET/POST throughput and latency
HTTP/2 ServerxKit single-threaded HTTP/2 (h2c) server vs Go net/http h2c — GET/POST throughput and latency
HTTPS ServerxKit single-threaded HTTPS (TLS 1.3) server vs Go net/http — GET/POST throughput and latency

Quick Navigation Guide

By Use Case

I want to...Start here
Build an event-driven serverxbase/event.hxbase/socket.h
Schedule timersxbase/timer.h
Run tasks on a thread poolxbase/task.h
Make async HTTP requestsxhttp/client.h
Stream LLM API responses (SSE)xhttp/sse.c
Build an HTTP serverxhttp/server.h
Add WebSocket serverxhttp/ws.h
Connect as WebSocket clientxhttp/ws.h
Parse a URLxnet/url.h
Resolve DNS asynchronouslyxnet/dns.h
Make async TCP connectionsxnet/tcp.h
Build a TCP serverxnet/tcp.h
Configure TLSxnet/tls.h
Enable TLS (HTTPS)xhttp/transport.h
Add async loggingxlog/logger.h
Manage object lifecyclesxbase/memory.h
Choose the right buffer typexbuf overview
Build a lock-free producer/consumer pipelinexbase/mpsc.h
See micro-benchmark resultsEach module doc has a Benchmark section (e.g. mpsc.h)
See HTTP server benchmarksHTTP/1.1 · HTTP/2 · HTTPS

By Dependency Level

Level 0 (no deps)     : atomic.h, error.h, time.h
Level 1 (atomic only) : heap.h, mpsc.h
Level 2 (Level 0-1)   : memory.h, log.h, backtrace.h, buf.h, ring.h
Level 3 (Level 0-2)   : event.h, io.h, url.h, tls.h
Level 4 (event loop)  : timer.h, task.h, socket.h, dns.h, tcp.h, logger.h, client.h, server.h, ws.h

Module Dependency Graph

graph BT
    subgraph "Level 0"
        ATOMIC["atomic.h"]
        ERROR["error.h"]
        TIME["time.h"]
    end

    subgraph "Level 1"
        HEAP["heap.h"]
        MPSC["mpsc.h"]
    end

    subgraph "Level 2"
        MEMORY["memory.h"]
        LOG["log.h"]
        BT_["backtrace.h"]
        BUF["buf.h"]
        RING["ring.h"]
    end

    subgraph "Level 3"
        EVENT["event.h"]
        IO["io.h"]
        URL["url.h"]
        TLS_CONF["tls.h"]
    end

    subgraph "Level 4"
        TIMER["timer.h"]
        TASK["task.h"]
        SOCKET["socket.h"]
        DNS["dns.h"]
        TCP["tcp.h"]
        LOGGER["logger.h"]
        CLIENT["client.h"]
        SERVER["server.h"]
        WS["ws.h"]
    end

    HEAP --> ATOMIC
    MPSC --> ATOMIC
    MEMORY --> ERROR
    LOG --> BT_
    IO --> ATOMIC
    IO --> BUF
    EVENT --> HEAP
    EVENT --> MPSC
    EVENT --> TIME
    TIMER --> EVENT
    TASK --> EVENT
    SOCKET --> EVENT
    DNS --> EVENT
    TCP --> EVENT
    TCP --> DNS
    TCP --> SOCKET
    TCP --> TLS_CONF
    LOGGER --> EVENT
    LOGGER --> MPSC
    LOGGER --> LOG
    CLIENT --> EVENT
    CLIENT --> BUF
    CLIENT --> URL
    CLIENT --> DNS
    CLIENT --> TLS_CONF
    SERVER --> SOCKET
    SERVER --> BUF
    SERVER --> TLS_CONF
    WS --> SERVER
    WS --> URL

    style EVENT fill:#50b86c,color:#fff
    style URL fill:#e74c3c,color:#fff
    style DNS fill:#e74c3c,color:#fff
    style TCP fill:#e74c3c,color:#fff
    style TLS_CONF fill:#e74c3c,color:#fff
    style CLIENT fill:#f5a623,color:#fff
    style SERVER fill:#f5a623,color:#fff
    style WS fill:#f5a623,color:#fff
    style LOGGER fill:#9b59b6,color:#fff

Build & Test

# Build
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build --parallel

# Test
ctest --test-dir build --output-on-failure --parallel 4

See the project README for full build instructions, prerequisites, and container-based Linux testing.

Benchmark

Micro-benchmark results are included in each module's documentation page (see the Benchmark section at the bottom of each page, e.g. mpsc.h, buf.h).

End-to-end benchmarks:

BenchmarkDescription
HTTP/1.1 ServerxKit vs Go net/http — 152K req/s single-threaded, +15~60% faster across all scenarios
HTTP/2 ServerxKit vs Go h2c — single-threaded HTTP/2 (h2c) throughput comparison
HTTPS ServerxKit vs Go HTTPS — single-threaded TLS 1.3 throughput comparison

License

MIT © 2025-present Leo X. and xKit contributors