BLI_linklist_lockfree_test.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. #include "MEM_guardedalloc.h"
  4. #include "BLI_utildefines.h"
  5. #include "BLI_linklist_lockfree.h"
  6. #include "BLI_task.h"
  7. #include "BLI_threads.h"
  8. TEST(LockfreeLinkList, Init)
  9. {
  10. LockfreeLinkList list;
  11. BLI_linklist_lockfree_init(&list);
  12. EXPECT_EQ(list.head, &list.dummy_node);
  13. EXPECT_EQ(list.tail, &list.dummy_node);
  14. BLI_linklist_lockfree_free(&list, NULL);
  15. }
  16. TEST(LockfreeLinkList, InsertSingle)
  17. {
  18. LockfreeLinkList list;
  19. LockfreeLinkNode node;
  20. BLI_linklist_lockfree_init(&list);
  21. BLI_linklist_lockfree_insert(&list, &node);
  22. EXPECT_EQ(list.head, &list.dummy_node);
  23. EXPECT_EQ(list.head->next, &node);
  24. EXPECT_EQ(list.tail, &node);
  25. BLI_linklist_lockfree_free(&list, NULL);
  26. }
  27. TEST(LockfreeLinkList, InsertMultiple)
  28. {
  29. static const int num_nodes = 128;
  30. LockfreeLinkList list;
  31. LockfreeLinkNode nodes[num_nodes];
  32. BLI_linklist_lockfree_init(&list);
  33. /* Insert all the nodes. */
  34. for (int i = 0; i < num_nodes; ++i) {
  35. BLI_linklist_lockfree_insert(&list, &nodes[i]);
  36. }
  37. /* Check head and tail. */
  38. EXPECT_EQ(list.head, &list.dummy_node);
  39. EXPECT_EQ(list.tail, &nodes[num_nodes - 1]);
  40. /* Check rest of the nodes. */
  41. int node_index = 0;
  42. for (LockfreeLinkNode *node = BLI_linklist_lockfree_begin(&list); node != NULL;
  43. node = node->next, ++node_index) {
  44. EXPECT_EQ(node, &nodes[node_index]);
  45. if (node_index != num_nodes - 1) {
  46. EXPECT_EQ(node->next, &nodes[node_index + 1]);
  47. }
  48. }
  49. /* Free list. */
  50. BLI_linklist_lockfree_free(&list, NULL);
  51. }
  52. namespace {
  53. struct IndexedNode {
  54. IndexedNode *next;
  55. int index;
  56. };
  57. void concurrent_insert(TaskPool *__restrict pool, void *taskdata, int /*threadid*/)
  58. {
  59. LockfreeLinkList *list = (LockfreeLinkList *)BLI_task_pool_userdata(pool);
  60. CHECK_NOTNULL(list);
  61. IndexedNode *node = (IndexedNode *)MEM_mallocN(sizeof(IndexedNode), "test node");
  62. node->index = POINTER_AS_INT(taskdata);
  63. BLI_linklist_lockfree_insert(list, (LockfreeLinkNode *)node);
  64. }
  65. } // namespace
  66. TEST(LockfreeLinkList, InsertMultipleConcurrent)
  67. {
  68. static const int num_threads = 512;
  69. static const int num_nodes = 655360;
  70. /* Initialize list. */
  71. LockfreeLinkList list;
  72. BLI_linklist_lockfree_init(&list);
  73. /* Initialize task scheduler and pool. */
  74. TaskScheduler *scheduler = BLI_task_scheduler_create(num_threads);
  75. TaskPool *pool = BLI_task_pool_create_suspended(scheduler, &list);
  76. /* Push tasks to the pool. */
  77. for (int i = 0; i < num_nodes; ++i) {
  78. BLI_task_pool_push(pool, concurrent_insert, POINTER_FROM_INT(i), false, TASK_PRIORITY_HIGH);
  79. }
  80. /* Run all the tasks. */
  81. BLI_threaded_malloc_begin();
  82. BLI_task_pool_work_and_wait(pool);
  83. BLI_threaded_malloc_end();
  84. /* Verify we've got all the data properly inserted. */
  85. EXPECT_EQ(list.head, &list.dummy_node);
  86. bool *visited_nodes = (bool *)MEM_callocN(sizeof(bool) * num_nodes, "visited nodes");
  87. /* First, we make sure that none of the nodes are added twice. */
  88. for (LockfreeLinkNode *node_v = BLI_linklist_lockfree_begin(&list); node_v != NULL;
  89. node_v = node_v->next) {
  90. IndexedNode *node = (IndexedNode *)node_v;
  91. EXPECT_GE(node->index, 0);
  92. EXPECT_LT(node->index, num_nodes);
  93. EXPECT_FALSE(visited_nodes[node->index]);
  94. visited_nodes[node->index] = true;
  95. }
  96. /* Then we make sure node was added. */
  97. for (int node_index = 0; node_index < num_nodes; ++node_index) {
  98. EXPECT_TRUE(visited_nodes[node_index]);
  99. }
  100. MEM_freeN(visited_nodes);
  101. /* Cleanup data. */
  102. BLI_linklist_lockfree_free(&list, MEM_freeN);
  103. BLI_task_pool_free(pool);
  104. BLI_task_scheduler_free(scheduler);
  105. }