Blender V5.3
BLI_linklist_lockfree_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "MEM_guardedalloc.h"
8
10#include "BLI_task_c.hh"
11#include "BLI_threads.hh"
12#include "BLI_utildefines.hh"
13
14namespace blender {
15
17{
20 EXPECT_EQ(list.head, &list.dummy_node);
21 EXPECT_EQ(list.tail, &list.dummy_node);
23}
24
36
37TEST(LockfreeLinkList, InsertMultiple)
38{
39 static const int nodes_num = 128;
41 LockfreeLinkNode nodes[nodes_num];
43 /* Insert all the nodes. */
44 for (LockfreeLinkNode &node : nodes) {
46 }
47 /* Check head and tail. */
48 EXPECT_EQ(list.head, &list.dummy_node);
49 EXPECT_EQ(list.tail, &nodes[nodes_num - 1]);
50 /* Check rest of the nodes. */
51 int node_index = 0;
53 node = node->next, ++node_index)
54 {
55 EXPECT_EQ(node, &nodes[node_index]);
56 if (node_index != nodes_num - 1) {
57 EXPECT_EQ(node->next, &nodes[node_index + 1]);
58 }
59 }
60 /* Free list. */
62}
63
64namespace {
65
66struct IndexedNode {
67 IndexedNode *next;
68 int index;
69};
70
71void concurrent_insert(TaskPool *__restrict pool, void *taskdata)
72{
74 CHECK_NOTNULL(list);
75 IndexedNode *node = MEM_new_uninitialized<IndexedNode>("test node");
76 node->index = POINTER_AS_INT(taskdata);
78}
79
80} // namespace
81
82TEST(LockfreeLinkList, InsertMultipleConcurrent)
83{
84 static const int nodes_num = 655360;
85 /* Initialize list. */
88 /* Initialize task scheduler and pool. */
90 /* Push tasks to the pool. */
91 for (int i = 0; i < nodes_num; ++i) {
92 BLI_task_pool_push(pool, concurrent_insert, POINTER_FROM_INT(i), false, nullptr);
93 }
94 /* Run all the tasks. */
96 /* Verify we've got all the data properly inserted. */
97 EXPECT_EQ(list.head, &list.dummy_node);
98 bool *visited_nodes = MEM_new_array_zeroed<bool>(nodes_num, "visited nodes");
99 /* First, we make sure that none of the nodes are added twice. */
100 for (LockfreeLinkNode *node_v = BLI_linklist_lockfree_begin(&list); node_v != nullptr;
101 node_v = node_v->next)
102 {
103 IndexedNode *node = reinterpret_cast<IndexedNode *>(node_v);
104 EXPECT_GE(node->index, 0);
105 EXPECT_LT(node->index, nodes_num);
106 EXPECT_FALSE(visited_nodes[node->index]);
107 visited_nodes[node->index] = true;
108 }
109 /* Then we make sure node was added. */
110 for (int node_index = 0; node_index < nodes_num; ++node_index) {
111 EXPECT_TRUE(visited_nodes[node_index]);
112 }
113 MEM_delete(visited_nodes);
114 /* Cleanup data. */
116 BLI_task_pool_free(pool);
117}
118
119} // namespace blender
Read Blender memory allocation and freeing.
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define index(i)
#define EXPECT_FALSE(result)
#define EXPECT_LT(result, expect)
#define EXPECT_GE(result, expect)
#define EXPECT_TRUE(result)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
@ TASK_PRIORITY_HIGH
Definition BLI_task_c.hh:61
TaskPool * BLI_task_pool_create_suspended(void *userdata, eTaskPriority priority)
Definition task_pool.cc:423
void MEM_delete_void(void *vmemh)
void BLI_linklist_lockfree_init(LockfreeLinkList *list)
TEST(array_store, Nop)
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:455
void * BLI_task_pool_user_data(TaskPool *pool)
Definition task_pool.cc:460
void BLI_linklist_lockfree_free(LockfreeLinkList *list, LockfreeeLinkNodeFreeFP free_func)
void BLI_linklist_lockfree_insert(LockfreeLinkList *list, LockfreeLinkNode *node)
LockfreeLinkNode * BLI_linklist_lockfree_begin(LockfreeLinkList *list)
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
struct LockfreeLinkNode * next