Skip to content

Comments

Thread local queue io queue#28

Merged
ming1 merged 2 commits intomainfrom
thread-local-queue-io-queue
Sep 4, 2025
Merged

Thread local queue io queue#28
ming1 merged 2 commits intomainfrom
thread-local-queue-io-queue

Conversation

@ming1
Copy link
Collaborator

@ming1 ming1 commented Sep 4, 2025

feat: replace q.q_ring with thread-local variable and introduce 3 APIs

feat: use LazyCell for thread-local CTRL_URING initialization

- Replace immediate initialization with LazyCell::new() for deferred allocation
- Add rust-version = "1.80" requirement to Cargo.toml for LazyCell support
- Improves performance by deferring expensive IoUring setup until first access

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Replace individual q.q_ring usage with a unified thread-local QUEUE_RING
using OnceCell for lazy initialization. This change provides cleaner API
access through helper macros and improves thread safety.

Key changes:
- Add thread-local QUEUE_RING with OnceCell<RefCell<IoUring>>
- Create with_queue_ring() and with_queue_ring_mut() for easier external access
- Replace all q.q_ring.borrow() and q.q_ring.borrow_mut() calls (20+ instances)
- Update both src/io.rs and src/uring_async.rs
- Add proper type annotations for compile-time safety
- Initialize thread-local ring when queue is created

Benefits:
- Cleaner, more ergonomic API through helper macros
- Thread-local access avoids cross-thread synchronization
- Lazy initialization reduces memory overhead
- Type-safe access with explicit annotations

Three new APIs are added:

- with_queue_ring()

- with_queue_ring_mut()

- init_task_ring()

The 1st two are for manipulating the queue's ring, and the 3rd one can
be used for customerizing the task ring.

Per-task io_uring is the most common usage, so take this way for
libublk.

- helps to use task ring to handle multiple queues from different
  devices

- helps to support async/.await control command because control ring
  actually depends on io ring if the two are in same task context

- with control command async/.await ready, it will be pretty
  easier/simpler to run arbitrary queues & tasks & devices combination,
  one typical example is to create many devices in single process, and
  let one thread to handle queues with same index from different
  devices.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
@ming1 ming1 force-pushed the thread-local-queue-io-queue branch from 0c73221 to c2a582b Compare September 4, 2025 04:12
@ming1 ming1 merged commit 8996969 into main Sep 4, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant