Blender V5.3
task_graph.cc File Reference
#include "MEM_guardedalloc.h"
#include "BLI_task_c.hh"
#include <memory>
#include <vector>

Go to the source code of this file.

Classes

struct  blender::TaskGraph
struct  blender::TaskNode

Namespaces

namespace  blender
 BMesh Iterators.

Functions

Task Graph Scheduling

Task Graphs can be used to create a forest of directional trees and schedule work to any tree. The nodes in the graph can be run in separate threads.

+---- [root] ----+
| |
v v
[node_1] +---- [node_2] ----+
| |
v v
[node_3] [node_4]
TaskNode *root = BLI_task_graph_node_create(task_graph, root_exec, NULL, NULL);
TaskNode *node_1 = BLI_task_graph_node_create(task_graph, node_exec, NULL, NULL);
TaskNode *node_2 = BLI_task_graph_node_create(task_graph, node_exec, NULL, NULL);
TaskNode *node_3 = BLI_task_graph_node_create(task_graph, node_exec, NULL, NULL);
TaskNode *node_4 = BLI_task_graph_node_create(task_graph, node_exec, NULL, NULL);
struct TaskGraph * BLI_task_graph_create()
Definition task_graph.cc:98
struct TaskNode * BLI_task_graph_node_create(struct TaskGraph *task_graph, TaskGraphNodeRunFunction run, void *user_data, TaskGraphNodeFreeFunction free_func)
void BLI_task_graph_edge_create(struct TaskNode *from_node, struct TaskNode *to_node)

Any node can be triggered to start a chain of tasks. Normally you would trigger a root node but it is supported to start the chain of tasks anywhere in the forest or tree. When a node completes, the execution flow is forwarded via the created edges. When a child node has multiple parents the child node will be triggered once for each parent.

BLI_task_graph_node_push_work(root);

In this example After root is finished, node_1 and node_2 will be started. Only after node_2 is finished node_3 and node_4 will be started.

After scheduling work we need to wait until all the tasks have been finished.

BLI_task_graph_work_and_wait();

When finished you can clean up all the resources by freeing the task_graph. Nodes are owned by the graph and are freed task_data will only be freed if a free_func was given.

BLI_task_graph_free(task_graph);

Work can enter a tree on any node. Normally this would be the root_node. A task_graph can be reused, but the caller needs to make sure the task_data is reset.

Task-Data

Typically you want give a task data to work on. Task data can be shared with other nodes, but be careful not to free the data multiple times. Task data is freed when calling BLI_task_graph_free.

MyData *task_data = MEM_new_zeroed<MyData>(__func__);
TaskNode *root = BLI_task_graph_node_create(task_graph, root_exec, task_data, MEM_delete);
TaskNode *node_1 = BLI_task_graph_node_create(task_graph, node_exec, task_data, NULL);
TaskNode *node_2 = BLI_task_graph_node_create(task_graph, node_exec, task_data, NULL);
TaskNode *node_3 = BLI_task_graph_node_create(task_graph, node_exec, task_data, NULL);
TaskNode *node_4 = BLI_task_graph_node_create(task_graph, node_exec, task_data, NULL);
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
struct TaskGraph * blender::BLI_task_graph_create ()
void blender::BLI_task_graph_free (struct TaskGraph *task_graph)
void blender::BLI_task_graph_work_and_wait (struct TaskGraph *task_graph)
struct TaskNode * blender::BLI_task_graph_node_create (struct TaskGraph *task_graph, TaskGraphNodeRunFunction run, void *user_data, TaskGraphNodeFreeFunction free_func)
bool blender::BLI_task_graph_node_push_work (struct TaskNode *task_node)
void blender::BLI_task_graph_edge_create (struct TaskNode *from_node, struct TaskNode *to_node)

Detailed Description

Task graph.

Definition in file task_graph.cc.