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