Blender V5.0
task.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "util/list.h"
8#include "util/string.h"
9#include "util/tbb.h"
10#include "util/thread.h"
11#include "util/unique_ptr.h"
12
14
15class TaskPool;
16class TaskScheduler;
17
18using TaskRunFunction = std::function<void()>;
19
20/* Task Pool
21 *
22 * Pool of tasks that will be executed by the central TaskScheduler. For each
23 * pool, we can wait for all tasks to be done, or cancel them before they are
24 * done.
25 *
26 * TaskRunFunction may be created with std::bind or lambda expressions. */
27
28class TaskPool {
29 public:
30 struct Summary {
31 /* Time spent to handle all tasks. */
32 double time_total;
33
34 /* Number of all tasks handled by this pool. */
36
37 /* A full multi-line description of the state of the pool after
38 * all work is done.
39 */
40 string full_report() const;
41 };
42
43 TaskPool();
45
46 void push(TaskRunFunction &&task);
47
48 void wait_work(Summary *stats = nullptr); /* work and wait until all tasks are done */
49 void cancel(); /* cancel all tasks and wait until they are no longer executing */
50
51 static bool canceled(); /* For worker threads, test if current task pool canceled. */
52
53 protected:
54 tbb::task_group tbb_group;
55
56 /* ** Statistics ** */
57
58 /* Time stamp of first task pushed. */
59 double start_time;
60
61 /* Number of all tasks pushed to the pool. Cleared after wait_work() and cancel(). */
63};
64
72 public:
73 static void init(const int num_threads = 0);
74 static void exit();
75 static void free_memory();
76
77 /* Maximum number of threads that will work on task. Use as little as
78 * possible and leave scheduling and splitting up tasks to the scheduler. */
79 static int max_concurrency();
80
81 protected:
83 static int users;
85
86#ifdef WITH_TBB_GLOBAL_CONTROL
87 static unique_ptr<tbb::global_control> global_control;
88#endif
89};
90
91/* Dedicated Task Pool
92 *
93 * Like a TaskPool, but will launch one dedicated thread to execute all tasks.
94 *
95 * The run callback can be a lambda without arguments. */
96
98 public:
101
102 void push(TaskRunFunction &&run, bool front = false);
103
104 void wait(); /* wait until all tasks are done */
105 void cancel(); /* cancel all tasks, keep worker thread running */
106
107 bool canceled(); /* for worker thread, test if canceled */
108
109 protected:
110 void num_decrease(const int done);
111 void num_increase();
112
113 void thread_run();
115
116 void clear();
117
120
121 list<TaskRunFunction> queue;
124
125 int num;
128
130};
131
void(* TaskRunFunction)(TaskPool *__restrict pool, void *taskdata)
Definition BLI_task.h:57
void init()
thread_condition_variable num_cond
Definition task.h:119
void num_decrease(const int done)
Definition task.cpp:165
void thread_run()
Definition task.cpp:202
thread_mutex queue_mutex
Definition task.h:122
thread_mutex num_mutex
Definition task.h:118
unique_ptr< thread > worker_thread
Definition task.h:129
void push(TaskRunFunction &&run, bool front=false)
Definition task.cpp:124
thread_condition_variable queue_cond
Definition task.h:123
bool canceled()
Definition task.cpp:160
void num_increase()
Definition task.cpp:176
list< TaskRunFunction > queue
Definition task.h:121
bool thread_wait_pop(TaskRunFunction &task)
Definition task.cpp:183
static void free_memory()
Definition task.cpp:91
static void exit()
Definition task.cpp:81
static thread_mutex mutex
Definition task.h:82
static int users
Definition task.h:83
static int active_num_threads
Definition task.h:84
static int max_concurrency()
Definition task.cpp:96
#define CCL_NAMESPACE_END
string full_report() const
Definition task.cpp:233
int num_tasks_handled
Definition task.h:35
double time_total
Definition task.h:32
void push(TaskRunFunction &&task)
Definition task.cpp:21
static bool canceled()
Definition task.cpp:48
int num_tasks_pushed
Definition task.h:62
tbb::task_group tbb_group
Definition task.h:54
double start_time
Definition task.h:59
void wait_work(Summary *stats=nullptr)
Definition task.cpp:27
void cancel()
TaskPool()
Definition task.cpp:14
std::function< void()> TaskRunFunction
Definition task.h:18
std::mutex thread_mutex
Definition thread.h:27
std::condition_variable thread_condition_variable
Definition thread.h:29