numam-dpdk/lib/librte_stack/rte_stack_lf.c
Gage Eads 3340202f59 stack: add lock-free implementation
This commit adds support for a lock-free (linked list based) stack to the
stack API. This behavior is selected through a new rte_stack_create() flag,
RTE_STACK_F_LF.

The stack consists of a linked list of elements, each containing a data
pointer and a next pointer, and an atomic stack depth counter.

The lock-free push operation enqueues a linked list of pointers by pointing
the tail of the list to the current stack head, and using a CAS to swing
the stack head pointer to the head of the list. The operation retries if it
is unsuccessful (i.e. the list changed between reading the head and
modifying it), else it adjusts the stack length and returns.

The lock-free pop operation first reserves num elements by adjusting the
stack length, to ensure the dequeue operation will succeed without
blocking. It then dequeues pointers by walking the list -- starting from
the head -- then swinging the head pointer (using a CAS as well). While
walking the list, the data pointers are recorded in an object table.

This algorithm stack uses a 128-bit compare-and-swap instruction, which
atomically updates the stack top pointer and a modification counter, to
protect against the ABA problem.

The linked list elements themselves are maintained in a lock-free LIFO
list, and are allocated before stack pushes and freed after stack pops.
Since the stack has a fixed maximum depth, these elements do not need to be
dynamically created.

Signed-off-by: Gage Eads <gage.eads@intel.com>
Reviewed-by: Olivier Matz <olivier.matz@6wind.com>
Reviewed-by: Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>
2019-04-04 22:06:16 +02:00

32 lines
676 B
C

/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2019 Intel Corporation
*/
#include "rte_stack.h"
void
rte_stack_lf_init(struct rte_stack *s, unsigned int count)
{
struct rte_stack_lf_elem *elems = s->stack_lf.elems;
unsigned int i;
for (i = 0; i < count; i++)
__rte_stack_lf_push_elems(&s->stack_lf.free,
&elems[i], &elems[i], 1);
}
ssize_t
rte_stack_lf_get_memsize(unsigned int count)
{
ssize_t sz = sizeof(struct rte_stack);
sz += RTE_CACHE_LINE_ROUNDUP(count * sizeof(struct rte_stack_lf_elem));
/* Add padding to avoid false sharing conflicts caused by
* next-line hardware prefetchers.
*/
sz += 2 * RTE_CACHE_LINE_SIZE;
return sz;
}