Debian Codegraph @

Xpjproject/pjlib/src/pj/atomic_slist.c

raw symbols SCIP
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
19 /* 2022-2025 Leonid Goltsblat <[email protected]> */
20
21 #include <pj/os.h>
22 #include <pj/assert.h>
23 #include <pj/log.h>
24 #include <pj/pool.h>
25 #include <pj/string.h>
26 #include <pj/errno.h>
27
28 #define THIS_FILE "atomic_slist.c"
29
30 #if defined(PJ_ATOMIC_SLIST_IMPLEMENTATION) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32
31
32 #if !defined(PJ_WIN32)
33 # error atomic slist PJ_ATOMIC_SLIST_WIN32 implementation should be compiled only for Windows platform
34 #elif !defined(MEMORY_ALLOCATION_ALIGNMENT)
35 # include <windows.h>
36 #endif // PJ_WIN32
37
38 //uncomment next line to check alignment in runtime
39 //#define PJ_ATOMIC_SLIST_ALIGN_DEBUG
40
41
42 #ifndef PJ_IS_ALIGNED
43 # define PJ_IS_ALIGNED(PTR, ALIGNMENT) (!((pj_ssize_t)(PTR) & ((ALIGNMENT)-1)))
44 #endif //PJ_IS_ALIGNED
45
46 #endif //defined(PJ_ATOMIC_SLIST_IMPLEMENTATION) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32
47
48 #include <pj/atomic_slist.h>
49
50 #if defined(PJ_ATOMIC_SLIST_IMPLEMENTATION) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_GENERIC
51
52 //#pragma message("compilation of cross-platform slist implementation")
53
54 /**
55 * This structure describes generic slist node.
56 */
57 typedef struct pj_atomic_slist_node
58 {
59 PJ_DECL_ATOMIC_SLIST_MEMBER(struct pj_atomic_slist_node);
60 } PJ_ATTR_MAY_ALIAS pj_atomic_slist_node; /* may_alias avoids warning with gcc-4.4 -Wall -O2 */
61
62
63 struct pj_atomic_slist
64 {
65 pj_atomic_slist_node head;
66 pj_mutex_t *mutex;
67 };
68
69 PJ_DEF(pj_status_t) pj_atomic_slist_create(pj_pool_t *pool, pj_atomic_slist **slist)
70 {
71 pj_atomic_slist *p_slist;
72 pj_status_t rc;
73 char name[PJ_MAX_OBJ_NAME];
74
75 PJ_ASSERT_RETURN(pool && slist, PJ_EINVAL);
76
77 p_slist = PJ_POOL_ZALLOC_T(pool, pj_atomic_slist);
78 if (!p_slist)
79 return PJ_ENOMEM;
80
81 /* Set name. */
82 pj_ansi_snprintf(name, PJ_MAX_OBJ_NAME, "slst%p", p_slist);
83
84 rc = pj_mutex_create_simple(pool, name, &p_slist->mutex);
85 if (rc != PJ_SUCCESS)
86 return rc;
87
88
89 p_slist->head.next = &p_slist->head;
90 *slist = p_slist;
91
92 PJ_LOG(6, (THIS_FILE, "Atomic slist created slst%p", p_slist));
93 return PJ_SUCCESS;
94
95 }
96
97
98 PJ_DEF(pj_status_t) pj_atomic_slist_destroy(pj_atomic_slist *slist)
99 {
100 pj_status_t rc;
101
102 PJ_ASSERT_RETURN(slist, PJ_EINVAL);
103 rc = pj_mutex_destroy(slist->mutex);
104 if (rc == PJ_SUCCESS)
105 PJ_LOG(6, (THIS_FILE, "Atomic slist destroyed slst%p", slist));
106 return rc;
107 }
108
109
110 PJ_DEF(pj_status_t) pj_atomic_slist_push(pj_atomic_slist *slist, pj_atomic_slist_node_t *node)
111 {
112 pj_status_t status;
113
114 PJ_ASSERT_RETURN(node && slist, PJ_EINVAL);
115 if ((status = pj_mutex_lock(slist->mutex)) != PJ_SUCCESS) {
116 PJ_PERROR(1, ("pj_atomic_slist_push", status, "Error locking mutex for slist slst%p", slist));
117 return status;
118 }
119 ((pj_atomic_slist_node*)node)->next = slist->head.next;
120 slist->head.next = node;
121 if ((status = pj_mutex_unlock(slist->mutex)) != PJ_SUCCESS)
122 PJ_PERROR(1, ("pj_atomic_slist_push", status, "Error unlocking mutex for slist slst%p", slist));
123
124 return status;
125 }
126
127
128 PJ_DEF(pj_atomic_slist_node_t*) pj_atomic_slist_pop(pj_atomic_slist *slist)
129 {
130 pj_status_t status;
131 pj_atomic_slist_node *node;
132
133 PJ_ASSERT_RETURN(slist, NULL);
134 if ((status = pj_mutex_lock(slist->mutex)) != PJ_SUCCESS) {
135 PJ_PERROR(1, ("pj_atomic_slist_pop", status, "Error locking mutex for slist slst%p", slist));
136 return NULL;
137 }
138
139 if ((node = slist->head.next) != &slist->head) {
140 slist->head.next = node->next;
141 node->next = NULL;
142 } else
143 node = NULL;
144
145 if ((status = pj_mutex_unlock(slist->mutex)) != PJ_SUCCESS)
146 PJ_PERROR(1, ("pj_atomic_slist_pop", status, "Error unlocking mutex for slist slst%p", slist));
147
148 return node;
149 }
150
151
152 /**
153 * Traverse the slist and get it's elements quantity.
154 * The return value of pj_atomic_slist_size should not be relied upon in multithreaded applications
155 * because the item count can be changed at any time by another thread.
156 */
157 PJ_DEF(pj_size_t) pj_atomic_slist_size(/*const*/ pj_atomic_slist *slist)
158 {
159 const pj_atomic_slist_node *node;
160 pj_size_t count;
161 pj_status_t status;
162
163 PJ_ASSERT_RETURN(slist, 0);
164
165 if ((status = pj_mutex_lock(slist->mutex)) != PJ_SUCCESS) {
166 PJ_PERROR(1, ("pj_atomic_slist_size", status, "Error locking mutex for slist slst%p", slist));
167 return 0;
168 }
169
170 for (node = slist->head.next, count = 0; node != &slist->head; node = node->next) {
171 pj_assert(node);
172 ++count;
173 }
174
175 if ((status = pj_mutex_unlock(slist->mutex)) != PJ_SUCCESS)
176 PJ_PERROR(1, ("pj_atomic_slist_size", status, "Error unlocking mutex for slist slst%p", slist));
177
178 return count;
179 }
180
181 PJ_DEF(void*) pj_atomic_slist_calloc(pj_pool_t *pool, pj_size_t count, pj_size_t elem)
182 {
183 return pj_pool_calloc(pool, count, elem);
184 }
185
186
187 #elif defined(PJ_ATOMIC_SLIST_IMPLEMENTATION) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32
188
189 //#pragma message("compilation of Windows platform slist implementation")
190
191
192 struct pj_atomic_slist {
193 SLIST_HEADER head;
194 };
195
196
197 PJ_DEF(pj_status_t) pj_atomic_slist_create(pj_pool_t *pool, pj_atomic_slist **slist)
198 {
199 pj_atomic_slist *p_slist;
200
201 PJ_ASSERT_RETURN(pool && slist, PJ_EINVAL);
202
203 p_slist = pj_pool_aligned_alloc(pool, MEMORY_ALLOCATION_ALIGNMENT, sizeof(pj_atomic_slist));
204 if (!p_slist)
205 return PJ_ENOMEM;
206
207 #ifdef PJ_ATOMIC_SLIST_ALIGN_DEBUG
208 PJ_ASSERT_ON_FAIL(PJ_IS_ALIGNED(&p_slist->head, MEMORY_ALLOCATION_ALIGNMENT), {
209 PJ_LOG(1, (THIS_FILE, "Windows platform's pj_atomic_slist implementation requires alignment not less than %d (slst%p).", MEMORY_ALLOCATION_ALIGNMENT, p_slist));
210 return PJ_EINVALIDOP;
211 });
212 #endif //PJ_ATOMIC_SLIST_ALIGN_DEBUG
213
214 InitializeSListHead(&p_slist->head);
215
216 *slist = p_slist;
217 PJ_LOG(6, (THIS_FILE, "Atomic slist created slst%p", p_slist));
218 return PJ_SUCCESS;
219
220 }
221
222
223 PJ_DEF(pj_status_t) pj_atomic_slist_destroy(pj_atomic_slist *slist)
224 {
225 PJ_ASSERT_RETURN(slist, PJ_EINVAL);
226 //nothing to do here
227 PJ_LOG(6, (THIS_FILE, "Atomic slist destroyed slst%p", slist));
228 return PJ_SUCCESS;
229 }
230
231
232 PJ_DEF(pj_status_t) pj_atomic_slist_push(pj_atomic_slist *slist, pj_atomic_slist_node_t *node)
233 {
234 #ifdef PJ_ATOMIC_SLIST_ALIGN_DEBUG
235 PJ_ASSERT_RETURN(node && slist, PJ_EINVAL);
236
237 PJ_ASSERT_ON_FAIL(PJ_IS_ALIGNED(&slist->head, MEMORY_ALLOCATION_ALIGNMENT) &&
238 PJ_IS_ALIGNED(node, MEMORY_ALLOCATION_ALIGNMENT), {
239 PJ_LOG(1, (THIS_FILE, "Windows platform's pj_atomic_slist implementation requires alignment not less than %d (slst%p).", MEMORY_ALLOCATION_ALIGNMENT, slist));
240 return PJ_EINVALIDOP;
241 });
242 #endif //PJ_ATOMIC_SLIST_ALIGN_DEBUG
243
244 /*pFirstEntry =*/ InterlockedPushEntrySList(&slist->head, node);
245 return PJ_SUCCESS;
246 }
247
248
249 PJ_DEF(pj_atomic_slist_node_t*) pj_atomic_slist_pop(pj_atomic_slist *slist)
250 {
251 #ifdef PJ_ATOMIC_SLIST_ALIGN_DEBUG
252 PJ_ASSERT_RETURN(slist, NULL);
253 PJ_ASSERT_ON_FAIL(PJ_IS_ALIGNED(&slist->head, MEMORY_ALLOCATION_ALIGNMENT), {
254 PJ_LOG(1, (THIS_FILE, "Windows platform's pj_atomic_slist implementation requires alignment not less than %d (slst%p).", MEMORY_ALLOCATION_ALIGNMENT, slist));
255 return NULL;
256 });
257 #endif //PJ_ATOMIC_SLIST_ALIGN_DEBUG
258
259 return InterlockedPopEntrySList(&slist->head);
260 }
261
262 /**
263 * Get the slist's elements quantity.
264 * The return value of pj_atomic_slist_size should not be relied upon in multithreaded applications
265 * because the item count can be changed at any time by another thread.
266 * For Windows platform returns the number of entries in the slist modulo 65535. For example,
267 * if the specified slist contains 65536 entries, pj_atomic_slist_size returns zero.
268 */
269 PJ_DEF(pj_size_t) pj_atomic_slist_size(/*const*/ pj_atomic_slist *slist)
270 {
271 #ifdef PJ_ATOMIC_SLIST_ALIGN_DEBUG
272 PJ_ASSERT_RETURN(slist, 0);
273 PJ_ASSERT_ON_FAIL(PJ_IS_ALIGNED(&slist->head, MEMORY_ALLOCATION_ALIGNMENT), {
274 PJ_LOG(1, (THIS_FILE, "Windows platform's pj_atomic_slist implementation requires alignment not less than %d (slst%p).", MEMORY_ALLOCATION_ALIGNMENT, slist));
275 return 0;
276 });
277 #endif //PJ_ATOMIC_SLIST_ALIGN_DEBUG
278
279 return QueryDepthSList(&slist->head);
280 }
281
282 PJ_DEF(void*) pj_atomic_slist_calloc(pj_pool_t *pool, pj_size_t count, pj_size_t elem)
283 {
284 #ifdef PJ_ATOMIC_SLIST_ALIGN_DEBUG
285 PJ_ASSERT_ON_FAIL(PJ_IS_ALIGNED(elem, MEMORY_ALLOCATION_ALIGNMENT), {
286 PJ_LOG(1, (THIS_FILE, "Windows platform's pj_atomic_slist implementation requires alignment not less than %d.", MEMORY_ALLOCATION_ALIGNMENT));
287 return 0;
288 });
289 #endif //PJ_ATOMIC_SLIST_ALIGN_DEBUG
290
291 void *p = pj_pool_aligned_alloc(pool, MEMORY_ALLOCATION_ALIGNMENT, elem*count);
292 if (p)
293 pj_bzero(p, elem*count);
294 return p;
295 }
296
297 #endif // PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32