Skip to content

Lock free data structures and patterns in Golang

License

Notifications You must be signed in to change notification settings

amirylm/lockfree

Repository files navigation

lockfree

WARNING: WIP, DO NOT USE in production

API Reference License Go version Github Actions Github Actions Go Report Card

Overview

Lock-free data structures implemented with native Golang, based on atomic compare-and-swap operations. These lock-free data structures are designed to provide concurrent access without relying on traditional locking mechanisms, improving performance and scalability in highly concurrent environments.

Data Structures

  • LL Stack - lock-free stack based on a linked list with atomic.Pointer elements.
  • LL Queue - lock-free queue based on a linked list with atomic.Pointer elements.
  • RB Queue - lock-free queue based on a ring buffer that uses a capped slice of atomic.Pointer elements.

NOTE: lock based data structures were implemented for benchmarking purposes (lock based ring buffer and channel based queue).

Extras

  • Reactor - lock-free reactor that provides thread-safe, non-blocking, asynchronous event processing.
    It uses a demultiplexer that is based on lock-free queues for events and control messages.
  • Pool Wrapper - wraps a function that is using some pooled resource.

Usage

go get github.com/amirylm/lockfree
import (
    "github.com/amirylm/lockfree/ringbuffer"
    "github.com/amirylm/lockfree/core"
)

func main() {
    ctx, cancel := context.WithCancel(context.Backgorund())
    defer cancel()

    q := ringbuffer.New[[]byte](core.WithCapacity(256))

    q.Enqueue([]byte("hello ring buffer"))

    val, _ := q.Dequeue()
    fmt.Println(val)
}

Detailed examples of usage can be found in ./examples folder. Additionally, you can find more examples in tests and benchmarks.

Contributing

Contributions to lockfree are welcomed and encouraged! If you find a bug or have an idea for an improvement, please open an issue or submit a pull request.

Before contributing, please make sure to read the Contributing Guidelines for more information.

License

lockfree is open-source software licensed under the MIT License. Feel free to use, modify, and distribute this library as permitted by the license.