Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/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 "test.h"
22
23 /**
24 * \page page_pjlib_atomic_slist_test Test: Slist
25 *
26 * This file provides implementation of \b atomic_slist_test() and
27 * \b atomic_slist_mt_test(). It tests the functionality and perfomance
28 * of the atomic_slist API.
29 *
30 * \section slist_test_sec Scope of the Test
31 *
32 * API tested:
33 * - pj_atomic_slist_create()
34 * - pj_atomic_slist_push()
35 * - pj_atomic_slist_pop()
36 * - pj_atomic_slist_destroy()
37 * - pj_atomic_slist_calloc()
38 * - pj_atomic_slist_size()
39 *
40 * MACROS tested:
41 * - PJ_ATOMIC_SLIST_ALIGN_PREFIX
42 * - PJ_ATOMIC_SLIST_ALIGN_SUFFIX
43 *
44 * This file is <b>pjlib-test/atomic_slist.c</b>
45 *
46 * \include pjlib-test/atomic_slist.c
47 */
48
49 #if INCLUDE_ATOMIC_SLIST_TEST
50
51 #include <pjlib.h>
52
53 #if defined(PJ_WIN32) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32
54
55 #ifndef MEMORY_ALLOCATION_ALIGNMENT
56 # include <windows.h>
57 #endif // !MEMORY_ALLOCATION_ALIGNMENT
58
59 #ifndef PJ_IS_ALIGNED
60 # define PJ_IS_ALIGNED(PTR, ALIGNMENT) (!((pj_ssize_t)(PTR) & ((ALIGNMENT)-1)))
61 #endif // !PJ_IS_ALIGNED
62
63 #endif
64
65 #define THIS_FILE "atomic_slist.c"
66 #define MAX_RESERVED 16
67 #define MAX_SLOTS 100
68
69 #define MAX_WORKER_THREADS 3
70 #define MAX_REPEATS 100000
71
72 #define TRACE(log) PJ_LOG(3,log)
73
74 typedef struct PJ_ATOMIC_SLIST_ALIGN_PREFIX slist_node {
75 PJ_DECL_ATOMIC_SLIST_MEMBER(struct slist_node);
76 int value;
77 char unaligner; /* to unalign slist_node.
78 * Without PJ_ATOMIC_SLIST_ALIGN_*
79 * sizeof(slist_node)%MEMORY_ALLOCATION_ALIGNMENT != 0
80 */
81 } PJ_ATOMIC_SLIST_ALIGN_SUFFIX slist_node;
82
83
84 /* any useful data;
85 * here simply pj_thread_t* - thread owned this slot
86 * alignment of array's items used as slist node under Windows platform should not be less than MEMORY_ALLOCATION_ALIGNMENT
87 */
88 typedef struct PJ_ATOMIC_SLIST_ALIGN_PREFIX slot_data {
89 PJ_DECL_ATOMIC_SLIST_MEMBER(struct slot_data);
90
91 /* this member simulates "payload" - user data stored in this slot */
92 pj_thread_t *owner;
93 } PJ_ATOMIC_SLIST_ALIGN_SUFFIX slot_data;
94
95 typedef struct slist_test_desc {
96 struct {
97 const char *title;
98 int n_threads; /* number of worker threads */
99 unsigned repeat; /* number of successfull slot reservation on each concurrent thread*/
100 } cfg;
101
102 struct {
103 pj_pool_t *pool;
104 pj_atomic_slist *empty_slot_slist; /**< Empty slots slist. */
105
106 /* to unalign slots[].*/
107 char unaligner2;
108 /*
109 * In Visual Studio we will see the following warning here:
110 * warning C4324: '<unnamed-tag>': structure was padded due to alignment specifier
111 *
112 * The compiler ensures that the elements of the slots[] array are
113 * aligned if slist_test_desc allocated statically.
114 * Otherwise (dynamically allocated from the pool), the application
115 * must request correctly aligned memory for slist_test_desc.
116 */
117 # if defined(_MSC_VER)
118 # pragma warning(push)
119 # pragma warning(disable:4324) // structure padded due to align()
120 # endif // defined(_MSC_VER)
121 slot_data slots[MAX_SLOTS]; /**< Array of useful information
122 * "slots" (file players, for example)*/
123 # if defined(_MSC_VER)
124 # pragma warning(pop)
125 # endif // defined(_MSC_VER)
126
127 int retcode; /* test retcode. non-zero will abort. */
128 } state;
129 } slist_test_desc;
130
131 static int slist_stress_test_init(slist_test_desc* test);
132 static int slist_stress_test_destroy(slist_test_desc* test);
133
134 static int slist_stress_test(slist_test_desc* test);
135 static int worker_thread(void* p);
136
137 static slist_test_desc tests[] = {
138 {
139 .cfg.title = "slist (single thread)",
140 .cfg.n_threads = 0,
141 .cfg.repeat = MAX_REPEATS
142 },
143 {
144 .cfg.title = "slist (multi threads)",
145 .cfg.n_threads = MAX_WORKER_THREADS,
146 .cfg.repeat = MAX_REPEATS
147 }
148 };
149
150 int atomic_slist_test()
151 {
152 pj_pool_t *pool = NULL;
153 pj_atomic_slist *slist = NULL;
154 const int sz = 15;
155 slist_node *nodes;
156 slist_node *p;
157 int i; /* don't change to unsigned! */
158 int rc = 0;
159
160 pool = pj_pool_create(mem, NULL, 4096, 0, NULL);
161 PJ_TEST_NOT_NULL(pool, NULL, { rc=-10; goto error; });
162
163 nodes = pj_atomic_slist_calloc(pool, sz, sizeof(slist_node));
164 PJ_TEST_NOT_NULL(nodes, NULL, { rc=-15; goto error; });
165
166 #if defined(PJ_WIN32) && PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32
167
168 /* Here we check our alignment macros PJ_ATOMIC_SLIST_ALIGN_PREFIX, PJ_ATOMIC_SLIST_ALIGN_SUFFIX */
169
170 # if defined(_MSC_VER)
171 # pragma warning(push)
172 # pragma warning(disable:4324) // structure padded due to align()
173 # endif // defined(_MSC_VER)
174
175 PJ_TEST_GTE(TYPE_ALIGNMENT(slist_node), MEMORY_ALLOCATION_ALIGNMENT, NULL, { rc=-3; goto error; });
176
177 # if defined(_MSC_VER)
178 # pragma warning(pop)
179 # endif // defined(_MSC_VER)
180
181 for (i = 0; i < sz; ++i)
182 PJ_TEST_TRUE(PJ_IS_ALIGNED(&nodes[i], MEMORY_ALLOCATION_ALIGNMENT), NULL, { rc=-7; goto error; });
183
184 #endif // PJ_ATOMIC_SLIST_IMPLEMENTATION==PJ_ATOMIC_SLIST_WIN32
185
186 // test slist_create()
187 PJ_TEST_SUCCESS(pj_atomic_slist_create(pool, &slist), NULL, { rc=-20; goto error; });
188
189 // newly created slist is empty
190 PJ_TEST_EQ(pj_atomic_slist_pop(slist), NULL, NULL, { rc=-25; goto error; });
191 PJ_TEST_EQ(pj_atomic_slist_size(slist), 0, NULL, { rc = -27; goto error; });
192
193 // Test push().
194 for (i = 0; i < sz; ++i) {
195 nodes[i].value = i;
196 PJ_TEST_SUCCESS(pj_atomic_slist_push(slist, &nodes[i]), NULL, { rc=-30; goto error; });
197 PJ_TEST_EQ(pj_atomic_slist_size(slist), i+1, NULL, { rc = -35; goto error; });
198 }
199
200 // test pop().
201 while((p = pj_atomic_slist_pop(slist)) != NULL) {
202 --i;
203 pj_assert(p->value == i); //FILO !
204 PJ_TEST_TRUE(p->value == i && p == nodes+i, NULL, { rc=-40; goto error; });
205 PJ_TEST_EQ(pj_atomic_slist_size(slist), i, NULL, { rc = -45; goto error; });
206 }
207 PJ_TEST_EQ(i, 0, NULL, { rc=-50; goto error; });
208 PJ_TEST_EQ(pj_atomic_slist_size(slist), 0, NULL, { rc = -55; goto error; });
209
210 error:
211 if (slist != NULL)
212 PJ_TEST_SUCCESS(pj_atomic_slist_destroy(slist), NULL, if (!rc) rc = -60);
213
214 if (pool)
215 pj_pool_release(pool);
216
217 return rc;
218 }
219
220 int atomic_slist_mt_test()
221 {
222 pj_pool_t *pool = NULL;
223 int rc = 0;
224 int i;
225
226 pool = pj_pool_create(mem, NULL, 4096, 0, NULL);
227 PJ_TEST_NOT_NULL(pool, NULL, return -100);
228
229 for (i = 0; !rc && i < PJ_ARRAY_SIZE(tests); ++i) {
230 tests[i].state.pool = pool;
231 rc = slist_stress_test(&tests[i]);
232 }
233
234 if (pool)
235 pj_pool_release(pool);
236
237 return rc;
238 }
239
240 /*
241 * This test illustrates:
242 * 1) a multi-threaded use case for the pj_atomic_slist API (pj_atomic_slist is thread safe)
243 * 2) a useful idea: reserving an empty slot in a large array without having to lock the entire array
244 */
245 static int slist_stress_test(slist_test_desc* test) {
246 unsigned i;
247 pj_timestamp t1, t2;
248 int rc;
249 int ident;
250
251 TRACE((THIS_FILE, "%s", test->cfg.title));
252 ident = pj_log_get_indent(); /* worker_thread change ident on this thread */
253 pj_log_push_indent();
254
255 rc = slist_stress_test_init(test);
256 if (rc)
257 return rc;
258
259 pj_get_timestamp(&t1);
260
261 if (test->cfg.n_threads == 0)
262 worker_thread(test);
263 else {
264 unsigned n_threads = test->cfg.n_threads;
265 pj_thread_t* threads[MAX_WORKER_THREADS];
266
267 for (i = 0; i < n_threads; ++i)
268 PJ_TEST_SUCCESS((pj_thread_create(test->state.pool, "slist_stress_test",
269 &worker_thread, test,
270 0, PJ_THREAD_SUSPENDED,
271 &threads[i])),
272 "Unable to create thread", return -105);
273
274 for (i = 0; i < n_threads; ++i)
275 PJ_TEST_SUCCESS(pj_thread_resume(threads[i]), "Unable to resume thread", return -110);
276
277 worker_thread(test);
278
279 for (i = 0; i < n_threads; ++i) {
280 pj_thread_join(threads[i]);
281 pj_thread_destroy(threads[i]);
282 }
283 }
284
285 pj_get_timestamp(&t2);
286
287 rc = slist_stress_test_destroy(test);
288 if (rc)
289 return rc;
290
291 TRACE((THIS_FILE, "%s time: %d ms (total), %d nanosec/op", test->cfg.title,
292 pj_elapsed_msec(&t1, &t2),
293 pj_elapsed_nanosec(&t1, &t2) / ((test->cfg.n_threads+1)*test->cfg.repeat)));
294
295 pj_log_set_indent(ident); /* restore ident changed by worker_thread() instead of pj_log_pop_indent() */
296 return test->state.retcode;
297
298 }
299
300 static int slist_stress_test_init(slist_test_desc* test) {
301 slot_data* p;
302 PJ_TEST_SUCCESS(pj_atomic_slist_create(test->state.pool, &test->state.empty_slot_slist), "Unable to create slist", return -115);
303
304 for (p = test->state.slots + PJ_ARRAY_SIZE(test->state.slots) - 1; p > test->state.slots - 1; --p)
305 PJ_TEST_SUCCESS(pj_atomic_slist_push(test->state.empty_slot_slist, p), "Unable to init slist", return -120);
306
307 return 0;
308 }
309
310 static int slist_stress_test_destroy(slist_test_desc* test) {
311 PJ_TEST_SUCCESS(pj_atomic_slist_destroy(test->state.empty_slot_slist), "Unable to destroy slist", return -125);
312 return 0;
313 }
314
315 /* worker thread */
316 static int worker_thread(void* p) {
317 slist_test_desc* test = (slist_test_desc*)p;
318
319 unsigned n_events = 0;
320 unsigned reserved_slots[MAX_RESERVED];
321 unsigned reserved_count = 0;
322 unsigned slot_id;
323
324 pj_bzero(reserved_slots, sizeof(reserved_slots));
325
326 /* log indent is not propagated to other threads,
327 * so we set it explicitly here
328 */
329 pj_log_set_indent(3);
330
331 while (test->state.retcode == 0 && n_events < test->cfg.repeat) {
332 slot_data* slot = pj_atomic_slist_pop(test->state.empty_slot_slist);
333 if (slot != NULL) { /* we have got an empty slot */
334 PJ_TEST_EQ(slot->owner, NULL, "Reserved slot is not empty", {
335 test->state.retcode = -130;
336 break;
337 });
338 slot->owner = pj_thread_this(); /* slot reserved successfully */
339 slot_id = (unsigned)(slot - test->state.slots);
340 reserved_slots[reserved_count++] = slot_id;
341 ++n_events;
342 }
343 if (slot == NULL || /* no empty slots at all or */
344 reserved_count >= MAX_RESERVED || /* this thread has reserved the maximum number of slots allowed */
345 n_events >= test->cfg.repeat) { /* or test completed */
346 while (reserved_count) { /* clear slots reserved here */
347 slot_id = reserved_slots[--reserved_count];
348 slot = &test->state.slots[slot_id];
349 if (slot->owner == pj_thread_this()) {
350 slot->owner = NULL; /* free up slot before returning */
351 pj_atomic_slist_push(test->state.empty_slot_slist, slot); /* slot returned to empty slot's slist */
352 } else if (slot->owner == NULL) {
353 PJ_LOG(1, (THIS_FILE, "Anothed thread has freed up this thread's slot"));
354 test->state.retcode = -135;
355 } else {
356 PJ_LOG(1, (THIS_FILE, "Anothed thread has reserved this thread's slot"));
357 test->state.retcode = -140;
358 }
359 }
360 }
361
362 }
363
364 TRACE((THIS_FILE, "thread exiting %s, n_events=%d", test->cfg.title, n_events));
365 return 0;
366 }
367
368
369 #else
370 /* To prevent warning about "translation unit is empty"
371 * when this test is disabled.
372 */
373 int dummy_slist_test;
374 #endif /* INCLUDE_ATOMIC_SLIST_TEST */
375
376