xxcond

I'm Hanchin Hsieh, a developer passionate about FOSS, and a big Fan of LiSA(織部 里沙). You can ask me about Go, CloudNative, Nvim, and Asahi Linux.


xxcond

ci Go Reference License

xxcond provides a lightweight, garbage-collection-reduced condition variable for Go, operating on pre-allocated memory regions chosen by the user. It is designed for scenarios like manual memory management, embedded systems, or custom memory pools, where minimizing Go runtime allocations is desirable.

Unlike sync.Cond, xxcond.Cond does not support multiple concurrent waiters or broadcast signaling. Only a single goroutine may wait on a condition at a time, and signaling is strictly one-to-one. Internally, it uses a minimal Go channel for wakeup, so it is not strictly GC-free.

This library is suitable for applications where memory locality, low GC pressure, and manual memory lifecycle management are preferred, while still requiring basic condition variable synchronization between goroutines.

Features

Usage

Basic Example

package main

import (
    "unsafe"
    "go.yuchanns.xyz/xxcond"
)

func main() {
    // Calculate required memory size
    size := xxcond.Sizeof()
    buf := make([]byte, size)

    // Initialize condition variable using user-allocated memory
    cond := xxcond.Make(unsafe.Pointer(&buf[0]))

    // Example: one goroutine waits; another signals
    go func() {
        cond.Lock()
        defer cond.Unlock()
        cond.Signal() // wake up waiter
    }()

    cond.Lock()
    cond.Wait() // wait for signal
    cond.Unlock()
}

Manual Memory Management

You may allocate the required buffer from pools, arenas, or on the stack. Ensure that the buffer remains valid for the lifetime of the condition variable instance.

var buf [64]byte // size must be sufficient for xxcond.Sizeof()
cond := xxcond.Make(unsafe.Pointer(&buf[0]))

Memory Management & Thread Safety

License

Licensed under the Apache License, Version 2.0.

Contributing

Contributions are welcome. Please ensure that any changes maintain the thread safety and minimal GC pressure properties.

Limitations

Head to documentation.