Generic Ring Buffer

Data Structures

struct  ring_head
 Ring buffer management data. More...

Functions

static unsigned long ring_get_head (struct ring_head *ring, unsigned int ring_size)
 Get the offset of the next unused entry in the buffer.
static unsigned long ring_get_tail (struct ring_head *ring, unsigned int ring_size)
 Get the offset of the first used entry in the buffer.
static unsigned int ring_entries_used (struct ring_head *ring)
 Get the number of used entries in the buffer.
static unsigned int ring_entries_used_before_end (struct ring_head *ring, unsigned int ring_size)
 Get the number of used entries in the buffer before it wraps.
static unsigned int ring_entries_unused (struct ring_head *ring, unsigned int ring_size)
 Get the number of unused entries in the buffer.
static unsigned int ring_entries_unused_before_end (struct ring_head *ring, unsigned int ring_size)
 Get the number of unused entries in the buffer before it wraps.
static bool ring_is_empty (struct ring_head *ring)
 Test if the ring buffer is empty.
static bool ring_is_full (struct ring_head *ring, unsigned int ring_size)
 Test if the ring buffer is full.
static void ring_insert_entries (struct ring_head *ring, unsigned int nr_entries)
 Insert entries into the ring buffer.
static void ring_extract_entries (struct ring_head *ring, unsigned int nr_entries)
 Extract entries from the ring buffer.
static void ring_reset (struct ring_head *ring)
 Reset the ring buffer.

Detailed Description

This is a generic, lockless ring buffer abstration. Generic because it does not care about what kind of data is stored in the buffer, and lockless because the producer and consumer states are tracked separately. Therefore, the producer and consumer do not require protection from each other even if they may run in different contexts.

Note however that if there are multiple producers or multiple consumers running from different contexts, it may be necessary to protect multiple clients on the same side from each other by, for example, disabling interrupts.

The head of the buffer indicates the current producer state, i.e. the index at which the next produced object will be placed. The tail of the buffer indicates the current consumer state, i.e. the index of the oldest object which hasn't been consumed yet. If the producer and consumer may run from different contexts (i.e. one from an interrupt handler and the other from a workqueue), the caller must ensure that the object in the underlying buffer is not accessed after the head or tail has been updated. Both ring_extract_entries() and ring_insert_entries() include an optimization barrier to ensure that the compiler does not break this by reordering the data accesses.

Ring Buffer Restrictions

In order to keep the ring buffer implementation efficient, there are a few restrictions that must be observed by the user:


Function Documentation

static unsigned int ring_entries_unused ( struct ring_head ring,
unsigned int  ring_size 
) [inline, static]

Get the number of unused entries in the buffer.

Parameters:
ring The ring buffer
ring_size The total number of entries in the ring buffer
Returns:
The number of entries which can be inserted before the buffer is full.

Definition at line 219 of file ring.h.

References ring_entries_used().

static unsigned int ring_entries_unused_before_end ( struct ring_head ring,
unsigned int  ring_size 
) [inline, static]

Get the number of unused entries in the buffer before it wraps.

Parameters:
ring The ring buffer
ring_size The total number of entries in the ring buffer
Returns:
The number of entries which can be inserted before the buffer is full or wraps around to the beginning.

Definition at line 235 of file ring.h.

References ring_head::head, and ring_head::tail.

static unsigned int ring_entries_used ( struct ring_head ring  )  [inline, static]

Get the number of used entries in the buffer.

Parameters:
ring The ring buffer
Returns:
The number of entries which can be extracted before the buffer is empty.

Definition at line 182 of file ring.h.

References ring_head::head, and ring_head::tail.

Referenced by ring_entries_unused(), and ring_is_full().

static unsigned int ring_entries_used_before_end ( struct ring_head ring,
unsigned int  ring_size 
) [inline, static]

Get the number of used entries in the buffer before it wraps.

Parameters:
ring The ring buffer
ring_size The total number of entries in the ring buffer
Returns:
The number of entries which can be extracted before the buffer is empty or wraps around to the beginning.

Definition at line 196 of file ring.h.

References ring_head::head, and ring_head::tail.

static void ring_extract_entries ( struct ring_head ring,
unsigned int  nr_entries 
) [inline, static]

Extract entries from the ring buffer.

Adjust the buffer tail to account for entries being extracted from the buffer. Normally, this should be called after the actual buffer contents have been read; the barrier ensures that the compiler doesn't move any buffer accesses after updating the tail.

Parameters:
ring The ring buffer
nr_entries The number of entries to extract

Definition at line 306 of file ring.h.

References barrier, and ring_head::tail.

static unsigned long ring_get_head ( struct ring_head ring,
unsigned int  ring_size 
) [inline, static]

Get the offset of the next unused entry in the buffer.

Note that the value returned by this function is only meaningful if there actually are any unused entries in the buffer.

Parameters:
ring The ring buffer
ring_size The total number of entries in the ring buffer
Returns:
The offset into the buffer of the next unused entry.

Definition at line 147 of file ring.h.

References ring_head::head.

static unsigned long ring_get_tail ( struct ring_head ring,
unsigned int  ring_size 
) [inline, static]

Get the offset of the first used entry in the buffer.

Note that the value returned by this function is only meaningful if there actually are any used entries in the buffer.

Parameters:
ring The ring buffer
ring_size The total number of entries in the ring buffer
Returns:
The offset into the buffer of the first used entry.

Definition at line 166 of file ring.h.

References ring_head::tail.

static void ring_insert_entries ( struct ring_head ring,
unsigned int  nr_entries 
) [inline, static]

Insert entries into the ring buffer.

Adjust the buffer head to account for entries being inserted into the buffer. Normally, this should be called after the actual buffer contents have been updated; the barrier ensures that the compiler doesn't move any buffer accesses after updating the head.

Parameters:
ring The ring buffer
nr_entries The number of entries to insert

Definition at line 288 of file ring.h.

References barrier, and ring_head::head.

static bool ring_is_empty ( struct ring_head ring  )  [inline, static]

Test if the ring buffer is empty.

Parameters:
ring The ring buffer
Return values:
true There are no used entries in the buffer.
false There is at least one used entry in the buffer.

Definition at line 257 of file ring.h.

References ring_head::head, and ring_head::tail.

static bool ring_is_full ( struct ring_head ring,
unsigned int  ring_size 
) [inline, static]

Test if the ring buffer is full.

Parameters:
ring The ring buffer
ring_size The total number of entries in the ring buffer
Return values:
true The ring buffer is full
false There is room for at least one more entry in the buffer

Definition at line 271 of file ring.h.

References ring_entries_used().

static void ring_reset ( struct ring_head ring  )  [inline, static]

Reset the ring buffer.

This will mark the ring buffer as empty, and move both head and tail to the beginning of the buffer.

Parameters:
ring The ring buffer

Definition at line 321 of file ring.h.

References ring_head::head, and ring_head::tail.

Generated on Thu Apr 29 14:10:35 2010 for xplain-bc by  doxygen 1.6.3