Search code examples
multithreadinglinux-kernelio-uring

Does Linux io_uring use SPSC or MPMC ring buffers?


Is Linux io_uring using SPSC or MPMC ring buffers? If it uses SPSC then is it possible to use it in multithreaded applications?


Solution

  • I assume that you do not want to use the library liburing, but to access the ring buffers directly.

    The submission ring buffer is single producer. It is not possible to atomically write the submission entry and the ring buffer tail index, even if head & tail are atomic. Use either a mutex here and/or a multiple producer secondary queue in case of that the ring buffer is busy (it is always possible that the writing thread is suspended inside the critical section). Another solution might be to use multiple submission queues. I would prefer a kernel change to invalidate the submission entry after reading.

    However, the completion ring buffer is multiple consumer.

    • Read the atomic head index.
    • Compare it with atomic tail.
    • If different: there is data available.
    • Read the regarding completion entry in the ring buffer.
    • If an atomic compare-exchange with the next head index succeeds, the completion entry is valid.
    • If not, another thread has consumed the entry. Retry to read the next entry.