Skip to main content

Lock-free, non-blocking MPMC queues.

Project description

Codecov CI Test Safety Test no_std Test Crates.io Docs.rs

nblf-queue

Non-Blocking Lock-Free Queue

An atomic lock-free MPMC queue based on the NBLFQ algorithm.

This repository provides multiple queue implementations with different storage and allocation strategies.

All queues in this repository are safe to use in a concurrent context and will never block the calling thread.

Queue variants

  • Static queues: fixed-capacity queues backed by static storage
  • Allocated queues: fixed-capacity queues backed by dynamically allocated storage, only available on feature alloc
  • Dynamic queues: dynamically growable queues, only available on feature dynamic
  • Pooled Queues: variants of other queues, which may store arbitrary types, only available on feature pool

Non-pooled queues store items in atomically updated slots, restricting the stored items to small, pointer-like values.

Usage

nblf_queue::StaticQueue:

  use nblf_queue::{StaticQueue, MPMCQueue};

  let q: StaticQueue<_, 2> = StaticQueue::new();

  assert!(q.push(&42).is_ok());
  assert!(q.push(&1).is_ok());
  assert!(q.push(&4242).is_err());

  assert_eq!(q.pop(), Some(&42));
  assert_eq!(q.pop(), Some(&1));
  assert!(q.pop().is_none());

nblf_queue::PooledStaticQueue:

  #[cfg(feature = "pool")]
  fn run() {
    use nblf_queue::{PooledStaticQueue, MPMCQueue};

    let q: PooledStaticQueue<_, 2> = PooledStaticQueue::new();

    assert!(q.push(42).is_ok());
    assert!(q.push(1).is_ok());
    assert!(q.push(4242).is_err());

    assert_eq!(q.pop(), Some(42));
    assert_eq!(q.pop(), Some(1));
    assert!(q.pop().is_none());
  }

  #[cfg(feature = "pool")]
  run();

Choosing a queue type

StaticQueue and Queue may only store small values and are optimized for this use case.

PooledStaticQueue and PooledQueue may store arbitrary types, at the cost of higher memory usage and runtime cost.

DynamicQueue and PooledDynamicQueue may be grown dynamically, at the cost of higher total memory usage and runtime cost. This is cost is even higher for PooledDynamicQueue.

Platform Support

Multiple storage types are available, dependent on platform:

  • Tagged64 - platforms with native 64-bit atomic operations or feature atomic-fallback

  • Tagged128 - platforms with native 128-bit atomic operations or feature atomic-fallback

Storage types will be chosen automatically, unless sepcified explicitly.

[!NOTE] ABA Safety & Storage Selection If it is plausible that other threads could perform (2^15 - 1) * queue_size pop and push operations while a single thread is paused/preempted in pop/push, Tagged128 slots should be used to ensure ABA safety.

Feature Flags

  • std: Enables std and alloc support

  • alloc: Enables alloc support, allowing usage of some dynamically allocated queues

  • pool: Enables pooled queues, which may store any type

  • dynamic: Enables dynamic queues, which may dynamically grow

  • atomic-fallback: Uses portable-atomic fallback feature for atomics if necessary. It is discouraged to use this feature, as fallback internally uses locks

  • default: pool

Python Bindings

Python bindings backed by PooledQueue and PooledDynamicQueue are available for concurrent applications. Core operations detach from the GIL to allow parallel execution.

[!NOTE] The Python bindings strictly use Auto slots without feature atomic-fallback. As a result, these bindings are only supported on platforms with native 64-bit or 128-bit atomic operations.

  from nblf_queue import Queue, DynamicQueue

  q: Queue[int] = Queue(10)

  q.push(42)
  item = q.pop()
  assert item == 42

  dq: DynamicQueue[str] = DynamicQueue(5)
  dq.push("hello")

  dq.grow()

Testing

The core test-suite of this crate was adapted from crossbeam-queue.

Current testing is based on:

  • Miri - to validate pointer arithmetic and catch UB
  • Loom and Shuttle - to test for race conditions
  • ASan - to check for memory corruption and leakage

References

Alexandre Denis, Charles Goedefroit. NBLFQ: a lock-free MPMC queue optimized for low contention. IPDPS 2025 - 39th International Parallel & Distributed Processing Symposium, IEEE, Jun 2025, Milan, Italy. hal-04851700v2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nblf_queue-0.1.0.tar.gz (521.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nblf_queue-0.1.0-cp312-abi3-win_amd64.whl (125.4 kB view details)

Uploaded CPython 3.12+Windows x86-64

nblf_queue-0.1.0-cp312-abi3-macosx_11_0_arm64.whl (228.3 kB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

nblf_queue-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl (232.8 kB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

nblf_queue-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (275.0 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

File details

Details for the file nblf_queue-0.1.0.tar.gz.

File metadata

  • Download URL: nblf_queue-0.1.0.tar.gz
  • Upload date:
  • Size: 521.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nblf_queue-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fe608ea62ff55801174464a96e7d8598e374ea2b8e6662ab56f5859893db1cc8
MD5 f51e53a28b2a9ce66419a79bbafbb15d
BLAKE2b-256 53d559d66a26258167b474dbb44c17dfafd44bac67385551a6770cd27ae798c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.1.0.tar.gz:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nblf_queue-0.1.0-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: nblf_queue-0.1.0-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 125.4 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nblf_queue-0.1.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 664cebcb59eca2b128ca5e0b48e19282d8b5e3e376f55e8bca886ac93a36b90e
MD5 e4fdc1b130667f81125462edc5aa88e9
BLAKE2b-256 cbefc98709b31b21486f244a282110af82eb0aead743301df4f0a1a6fba20004

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.1.0-cp312-abi3-win_amd64.whl:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nblf_queue-0.1.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nblf_queue-0.1.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e1804baa3aa1fd75ff6a320f6ad9057bc9936aa01e1a561195d2839ca4eb490e
MD5 c71fc0fd14133952b2b8c16cc50c8078
BLAKE2b-256 3250e6ee2d300ac2b360ae129543ffbe8e05f3612f5bb9a08ef7fcae293938e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.1.0-cp312-abi3-macosx_11_0_arm64.whl:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nblf_queue-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nblf_queue-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 beaed52e5cb49c95eede4f6c0a40920564f545c490899e2f28637d695052415e
MD5 a947524e436fa4f61e8d9faed1e3463b
BLAKE2b-256 d36aba8a99c9d4145233c2d73da3b192128929de8449b2c45ed11181ec30c8c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nblf_queue-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nblf_queue-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c9aceb07118c00751cc1fe8091935a6fc52bd731ef4d122bc30a9cadf53dbd96
MD5 2967db04fbb0393fc5cd669a628f827b
BLAKE2b-256 1b63b2d1dab72fab1c23e51da79e4ab8c99131d2f317a436105e6a9286724dd0

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page