xref: /dpdk/doc/guides/prog_guide/stack_lib.rst (revision 9a710863decb1cdb98efbdd5e11df3ebcfcc37b6)
1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright(c) 2019 Intel Corporation.
3
4Stack Library
5=============
6
7DPDK's stack library provides an API for configuration and use of a bounded
8stack of pointers.
9
10The stack library provides the following basic operations:
11
12*  Create a uniquely named stack of a user-specified size and using a
13   user-specified socket, with either standard (lock-based) or lock-free
14   behavior.
15
16*  Push and pop a burst of one or more stack objects (pointers). These function
17   are multi-threading safe.
18
19*  Free a previously created stack.
20
21*  Lookup a pointer to a stack by its name.
22
23*  Query a stack's current depth and number of free entries.
24
25Implementation
26~~~~~~~~~~~~~~
27
28The library supports two types of stacks: standard (lock-based) and lock-free.
29Both types use the same set of interfaces, but their implementations differ.
30
31Lock-based Stack
32----------------
33
34The lock-based stack consists of a contiguous array of pointers, a current
35index, and a spinlock. Accesses to the stack are made multi-thread safe by the
36spinlock.
37
38Lock-free Stack
39------------------
40
41The lock-free stack consists of a linked list of elements, each containing a
42data pointer and a next pointer, and an atomic stack depth counter. The
43lock-free property means that multiple threads can push and pop simultaneously,
44and one thread being preempted/delayed in a push or pop operation will not
45impede the forward progress of any other thread.
46
47The lock-free push operation enqueues a linked list of pointers by pointing the
48list's tail to the current stack head, and using a CAS to swing the stack head
49pointer to the head of the list. The operation retries if it is unsuccessful
50(i.e. the list changed between reading the head and modifying it), else it
51adjusts the stack length and returns.
52
53The lock-free pop operation first reserves one or more list elements by
54adjusting the stack length, to ensure the dequeue operation will succeed
55without blocking. It then dequeues pointers by walking the list -- starting
56from the head -- then swinging the head pointer (using a CAS as well). While
57walking the list, the data pointers are recorded in an object table.
58
59The linked list elements themselves are maintained in a lock-free LIFO, and are
60allocated before stack pushes and freed after stack pops. Since the stack has a
61fixed maximum depth, these elements do not need to be dynamically created.
62
63The lock-free behavior is selected by passing the *RTE_STACK_F_LF* flag to
64rte_stack_create().
65
66Preventing the ABA Problem
67^^^^^^^^^^^^^^^^^^^^^^^^^^
68
69To prevent the ABA problem, this algorithm stack uses a 128-bit
70compare-and-swap instruction to atomically update both the stack top pointer
71and a modification counter. The ABA problem can occur without a modification
72counter if, for example:
73
741. Thread A reads head pointer X and stores the pointed-to list element.
752. Other threads modify the list such that the head pointer is once again X,
76   but its pointed-to data is different than what thread A read.
773. Thread A changes the head pointer with a compare-and-swap and succeeds.
78
79In this case thread A would not detect that the list had changed, and would
80both pop stale data and incorrect change the head pointer. By adding a
81modification counter that is updated on every push and pop as part of the
82compare-and-swap, the algorithm can detect when the list changes even if the
83head pointer remains the same.
84