| 1 | /* |
| 2 | * Copyright (C) 2008-2025 Teluu Inc. (http://www.teluu.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | #ifndef __PJ_ATOMIC_SLIST_H__ |
| 19 | #define __PJ_ATOMIC_SLIST_H__ |
| 20 | |
| 21 | /* 2022-2025 Leonid Goltsblat <[email protected]> */ |
| 22 | |
| 23 | /** |
| 24 | * @file atomic_slist.h |
| 25 | * @brief Single Linked List data structure. |
| 26 | */ |
| 27 | |
| 28 | #include <pj/types.h> |
| 29 | |
| 30 | #if defined(PJ_ATOMIC_SLIST_IMPLEMENTATION) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32 |
| 31 | |
| 32 | /* the Windows platform atomic_slist implementation. */ |
| 33 | |
| 34 | /** |
| 35 | * PJ_ATOMIC_SLIST_ALIGN_PREFIX, PJ_ATOMIC_SLIST_ALIGN_SUFFIX is a readable syntax to declare |
| 36 | * platform default alignment for the atomic_slist item (see example below). |
| 37 | */ |
| 38 | |
| 39 | # if defined(MEMORY_ALLOCATION_ALIGNMENT) |
| 40 | # define PJ_ATOMIC_SLIST_ALIGNMENT MEMORY_ALLOCATION_ALIGNMENT |
| 41 | # elif defined(_WIN64) || defined(_M_ALPHA) |
| 42 | # define PJ_ATOMIC_SLIST_ALIGNMENT 16 |
| 43 | # else |
| 44 | # define PJ_ATOMIC_SLIST_ALIGNMENT 8 |
| 45 | # endif |
| 46 | |
| 47 | # define PJ_ATOMIC_SLIST_ALIGN_PREFIX PJ_ALIGN_DATA_PREFIX(PJ_ATOMIC_SLIST_ALIGNMENT) |
| 48 | # define PJ_ATOMIC_SLIST_ALIGN_SUFFIX PJ_ALIGN_DATA_SUFFIX(PJ_ATOMIC_SLIST_ALIGNMENT) |
| 49 | |
| 50 | #else // PJ_ATOMIC_SLIST_IMPLEMENTATION != PJ_ATOMIC_SLIST_WIN32 |
| 51 | |
| 52 | /* compilation of crossplatform atomic_slist implementation */ |
| 53 | |
| 54 | # define PJ_ATOMIC_SLIST_ALIGNMENT 0 |
| 55 | |
| 56 | # define PJ_ATOMIC_SLIST_ALIGN_PREFIX |
| 57 | # define PJ_ATOMIC_SLIST_ALIGN_SUFFIX |
| 58 | |
| 59 | #endif // PJ_ATOMIC_SLIST_IMPLEMENTATION |
| 60 | |
| 61 | |
| 62 | PJ_BEGIN_DECL |
| 63 | |
| 64 | /* |
| 65 | * @defgroup PJ_DS Data Structure. |
| 66 | */ |
| 67 | |
| 68 | /** |
| 69 | * @defgroup PJ_ATOMIC_SLIST Single Linked List with LIFO in/out policy |
| 70 | * @ingroup PJ_DS |
| 71 | * @{ |
| 72 | * |
| 73 | * Atomic slist in PJLIB is single-linked list with First In Last Out logic. |
| 74 | * Atomic slist is thread safe. Common PJLIB slist implementation uses internal |
| 75 | * locking mechanism so is thread-safe. |
| 76 | * Implementation for Windows platform uses locking free Windows embeded single |
| 77 | * linked list implementation. The performance of pj_atomic_slist implementation |
| 78 | * for Windows platform is considerably higher than cross-platform. |
| 79 | * |
| 80 | * By default pjlib compile and link os independent "cross-platform" (generic) |
| 81 | * implementation. To select implementation you may optionaly define |
| 82 | * PJ_ATOMIC_SLIST_IMPLEMENTATION as PJ_ATOMIC_SLIST_WIN32 or |
| 83 | * PJ_ATOMIC_SLIST_GENERIC. |
| 84 | * The last option is default for all platforms except Windows |
| 85 | * where the default is PJ_ATOMIC_SLIST_WIN32. |
| 86 | * |
| 87 | * Windows single linked list implementation (PJ_ATOMIC_SLIST_WIN32) |
| 88 | * requires aligned data, both slist item and slist itself |
| 89 | * should be aligned by 8 (for x86) or 16 (for x64) byte. |
| 90 | * winnt.h define MEMORY_ALLOCATION_ALIGNMENT macro for this purpose. |
| 91 | * For the same purpose pjsip defines PJ_ATOMIC_SLIST_ALIGNMENT macro |
| 92 | * calculating value based on curent platform. |
| 93 | * To use MEMORY_ALLOCATION_ALIGNMENT macro in the build system as the value |
| 94 | * of PJ_ATOMIC_SLIST_ALIGNMENT macro we recomend (this is optional) to add |
| 95 | * #include <windows.h> to your config_site.h. |
| 96 | * For other implementation PJ_ATOMIC_SLIST_ALIGNMENT macro is defined as 0, |
| 97 | * which causes pj_pool_aligned_alloc() to use the default pool alignment. |
| 98 | * |
| 99 | * To allocate slist element (node) or array of slist nodes application |
| 100 | * should use call pj_pool_aligned_alloc() with PJ_ATOMIC_SLIST_ALIGNMENT |
| 101 | * as the value of an alignment parameter. |
| 102 | * To hide these implementation details from the application, |
| 103 | * the API provides a pj_atomic_slist_calloc() function that internally |
| 104 | * handles the implementation-specific alignment. |
| 105 | * However application developer should declare slist nodes types properly |
| 106 | * aligned. The macros PJ_ATOMIC_SLIST_ALIGN_PREFIX and |
| 107 | * PJ_ATOMIC_SLIST_ALIGN_SUFFIX are provided for this purpose (see below). |
| 108 | * |
| 109 | * Atomic slist won't require dynamic memory allocation (just as all PJLIB |
| 110 | * data structures). The slist here should be viewed more like a low level C |
| 111 | * slist instead of high level C++ slist (which normally are easier to use but |
| 112 | * requires dynamic memory allocations), therefore all caveats with C slist |
| 113 | * apply here too (such as you can NOT put a node in more than one slists). |
| 114 | * |
| 115 | * \section pj_atomic_slist_example_sec Examples |
| 116 | * |
| 117 | * See below for examples on how to manipulate slist: |
| 118 | * - @ref page_pjlib_atomic_slist_test |
| 119 | */ |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Use PJ_DECL_ATOMIC_SLIST_MEMBER macro in the start of the structure |
| 124 | * declaration to declare that the structure can be used in the slist |
| 125 | * operation. This macro simply declares additional member @a next to |
| 126 | * the structure. |
| 127 | * |
| 128 | * The full declaration of slist item should contain alignment macro |
| 129 | * and may look like this: |
| 130 | * |
| 131 | * typedef struct PJ_ATOMIC_SLIST_ALIGN_PREFIX slist_node { |
| 132 | * PJ_DECL_ATOMIC_SLIST_MEMBER(struct slist_node); |
| 133 | * ... |
| 134 | * your data here |
| 135 | * ... |
| 136 | * } PJ_ATOMIC_SLIST_ALIGN_SUFFIX slist_node; |
| 137 | * |
| 138 | * @hideinitializer |
| 139 | */ |
| 140 | #if defined(PJ_ATOMIC_SLIST_IMPLEMENTATION) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32 |
| 141 | # define PJ_DECL_ATOMIC_SLIST_MEMBER(type) \ |
| 142 | /** Slist @a next. */ \ |
| 143 | SLIST_ENTRY next |
| 144 | #else // PJ_ATOMIC_SLIST_IMPLEMENTATION != PJ_ATOMIC_SLIST_WIN32 |
| 145 | # define PJ_DECL_ATOMIC_SLIST_MEMBER(type) \ |
| 146 | /** Slist @a next. */ \ |
| 147 | type *next |
| 148 | #endif // PJ_ATOMIC_SLIST_IMPLEMENTATION |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Create the slist: allocate memory, allocate and initialize OS resources. |
| 153 | * Initially, the slist will have no member, |
| 154 | * and function pj_atomic_slist_pop() will always return NULL |
| 155 | * for the newly initialized slist (which indicates there are no any items |
| 156 | * in the slist currently). |
| 157 | * |
| 158 | * @param pool Pool to allocate memory from. |
| 159 | * @param slist The slist head. |
| 160 | * |
| 161 | * @return PJ_SUCCESS or the appropriate error code. |
| 162 | */ |
| 163 | PJ_DECL(pj_status_t) pj_atomic_slist_create(pj_pool_t *pool, |
| 164 | pj_atomic_slist **slist); |
| 165 | |
| 166 | /** |
| 167 | * Free OS resources allocated by pj_atomic_slist_create(). |
| 168 | * |
| 169 | * @param slist The target slist. |
| 170 | * |
| 171 | * @return PJ_SUCCESS or the appropriate error code. |
| 172 | */ |
| 173 | PJ_DECL(pj_status_t) pj_atomic_slist_destroy(pj_atomic_slist *slist); |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * Insert (push) the node to the front of the slist |
| 178 | * as atomic (thread safe) operation. |
| 179 | * |
| 180 | * @param slist The slist. |
| 181 | * @param node The element to be inserted. |
| 182 | * |
| 183 | * @return PJ_SUCCESS or the appropriate error code. |
| 184 | */ |
| 185 | PJ_DECL(pj_status_t) pj_atomic_slist_push(pj_atomic_slist *slist, |
| 186 | pj_atomic_slist_node_t *node); |
| 187 | |
| 188 | |
| 189 | /** |
| 190 | * Extract (pop) element from the front of the slist |
| 191 | * (removing it from the slist) as atomic (thread safe) operation. |
| 192 | * |
| 193 | * @param slist The target slist. |
| 194 | * |
| 195 | * @return NULL if the slist is empty, |
| 196 | * or else pointer to element extracted from slist. |
| 197 | */ |
| 198 | PJ_DECL(pj_atomic_slist_node_t*) pj_atomic_slist_pop(pj_atomic_slist *slist); |
| 199 | |
| 200 | /** |
| 201 | * Traverse the slist and get it's elements quantity. |
| 202 | * The return value of pj_atomic_slist_size should not be relied upon |
| 203 | * in multithreaded applications because the item count can be changed |
| 204 | * at any time by another thread. |
| 205 | * For Windows platform returns the number of entries in the slist |
| 206 | * modulo 65535. For example, if the specified slist contains 65536 entries, |
| 207 | * pj_atomic_slist_size returns zero. |
| 208 | * |
| 209 | * @param slist The target slist. |
| 210 | * |
| 211 | * @return Number of elements. |
| 212 | */ |
| 213 | PJ_DECL(pj_size_t) pj_atomic_slist_size(/*const*/ pj_atomic_slist *slist); |
| 214 | |
| 215 | /** |
| 216 | * Allocate storage for slist nodes array or single node from the pool |
| 217 | * and initialize it to zero. |
| 218 | * This is simple wrapper arround pj_pool_aligned_alloc() or pj_pool_calloc() |
| 219 | * that internally handles the slist implementation-specific alignment. |
| 220 | * |
| 221 | * @param pool the pool. |
| 222 | * @param count the number of slist elements in the array. |
| 223 | * @param elem the size of individual slist element. |
| 224 | * |
| 225 | * @return Pointer to the allocated memory. |
| 226 | */ |
| 227 | PJ_DECL(void*) pj_atomic_slist_calloc(pj_pool_t *pool, pj_size_t count, pj_size_t elem); |
| 228 | |
| 229 | /** |
| 230 | * @} |
| 231 | */ |
| 232 | |
| 233 | PJ_END_DECL |
| 234 | |
| 235 | #endif /* __PJ_ATOMIC_SLIST_H__ */ |
| 236 |