C layer of the Containers module: a lock free queue for one producer and one consumer, in the caller memory.
The three C++ containers are templates with the element type and the capacity fixed at compile time. Only the queue gains anything in C, because passing data between two threads without a lock is the part that is easy to get wrong.
Here the element is a block of fixed size, given at creation, and copied in and out with 'memcpy'. The capacity is also given at creation. The algorithm is the one of 'CSpscQueue' (ethereal/cpp/containers/spsc_queue.hpp): a head the consumer writes, a tail the producer writes, each on its own cache line, release on the write and acquire on the read. The template cannot be instantiated with a size known only at run time, so this layer has its own copy of the algorithm.
Left out, on purpose:
- 'CFixedVector': in C an array and a counter do the same.
- 'CRingBuffer': it is not thread safe, and in C it is an array with two indices. Nothing would be gained.
Threads: only one thread calls TryPush and only one thread calls TryPop, during the whole life of the queue. Size and Capacity may be called from any thread, and Size is a snapshot. Create and Destroy run with no other call in progress.
Every export returns 'int'. Zero is success, negative is an error. The values are part of the ABI.