BLI_task_test.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. #include <string.h>
  4. #include "atomic_ops.h"
  5. extern "C" {
  6. #include "BLI_utildefines.h"
  7. #include "BLI_listbase.h"
  8. #include "BLI_mempool.h"
  9. #include "BLI_task.h"
  10. #include "MEM_guardedalloc.h"
  11. };
  12. #define NUM_ITEMS 10000
  13. /* *** Parallel iterations over mempool items. *** */
  14. static void task_mempool_iter_func(void *userdata, MempoolIterData *item)
  15. {
  16. int *data = (int *)item;
  17. int *count = (int *)userdata;
  18. EXPECT_TRUE(data != NULL);
  19. *data += 1;
  20. atomic_sub_and_fetch_uint32((uint32_t *)count, 1);
  21. }
  22. TEST(task, MempoolIter)
  23. {
  24. int *data[NUM_ITEMS];
  25. BLI_threadapi_init();
  26. BLI_mempool *mempool = BLI_mempool_create(
  27. sizeof(*data[0]), NUM_ITEMS, 32, BLI_MEMPOOL_ALLOW_ITER);
  28. int i;
  29. /* 'Randomly' add and remove some items from mempool, to create a non-homogenous one. */
  30. int num_items = 0;
  31. for (i = 0; i < NUM_ITEMS; i++) {
  32. data[i] = (int *)BLI_mempool_alloc(mempool);
  33. *data[i] = i - 1;
  34. num_items++;
  35. }
  36. for (i = 0; i < NUM_ITEMS; i += 3) {
  37. BLI_mempool_free(mempool, data[i]);
  38. data[i] = NULL;
  39. num_items--;
  40. }
  41. for (i = 0; i < NUM_ITEMS; i += 7) {
  42. if (data[i] == NULL) {
  43. data[i] = (int *)BLI_mempool_alloc(mempool);
  44. *data[i] = i - 1;
  45. num_items++;
  46. }
  47. }
  48. for (i = 0; i < NUM_ITEMS - 5; i += 23) {
  49. for (int j = 0; j < 5; j++) {
  50. if (data[i + j] != NULL) {
  51. BLI_mempool_free(mempool, data[i + j]);
  52. data[i + j] = NULL;
  53. num_items--;
  54. }
  55. }
  56. }
  57. BLI_task_parallel_mempool(mempool, &num_items, task_mempool_iter_func, true);
  58. /* Those checks should ensure us all items of the mempool were processed once, and only once - as
  59. * expected. */
  60. EXPECT_EQ(num_items, 0);
  61. for (i = 0; i < NUM_ITEMS; i++) {
  62. if (data[i] != NULL) {
  63. EXPECT_EQ(*data[i], i);
  64. }
  65. }
  66. BLI_mempool_destroy(mempool);
  67. BLI_threadapi_exit();
  68. }
  69. /* *** Parallel iterations over double-linked list items. *** */
  70. static void task_listbase_iter_func(void *userdata, Link *item, int index)
  71. {
  72. LinkData *data = (LinkData *)item;
  73. int *count = (int *)userdata;
  74. data->data = POINTER_FROM_INT(POINTER_AS_INT(data->data) + index);
  75. atomic_sub_and_fetch_uint32((uint32_t *)count, 1);
  76. }
  77. TEST(task, ListBaseIter)
  78. {
  79. ListBase list = {NULL, NULL};
  80. LinkData *items_buffer = (LinkData *)MEM_calloc_arrayN(
  81. NUM_ITEMS, sizeof(*items_buffer), __func__);
  82. BLI_threadapi_init();
  83. int i;
  84. int num_items = 0;
  85. for (i = 0; i < NUM_ITEMS; i++) {
  86. BLI_addtail(&list, &items_buffer[i]);
  87. num_items++;
  88. }
  89. BLI_task_parallel_listbase(&list, &num_items, task_listbase_iter_func, true);
  90. /* Those checks should ensure us all items of the listbase were processed once, and only once -
  91. * as expected. */
  92. EXPECT_EQ(num_items, 0);
  93. LinkData *item;
  94. for (i = 0, item = (LinkData *)list.first; i < NUM_ITEMS && item != NULL;
  95. i++, item = item->next) {
  96. EXPECT_EQ(POINTER_AS_INT(item->data), i);
  97. }
  98. EXPECT_EQ(NUM_ITEMS, i);
  99. MEM_freeN(items_buffer);
  100. BLI_threadapi_exit();
  101. }