Blender V4.3
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#ifndef __UTIL_TASK_H__
6#define __UTIL_TASK_H__
7
8#include "util/list.h"
9#include "util/string.h"
10#include "util/tbb.h"
11#include "util/thread.h"
12#include "util/vector.h"
13
15
16class TaskPool;
17class TaskScheduler;
18
19typedef function<void(void)> TaskRunFunction;
20
21/* Task Pool
22 *
23 * Pool of tasks that will be executed by the central TaskScheduler. For each
24 * pool, we can wait for all tasks to be done, or cancel them before they are
25 * done.
26 *
27 * TaskRunFunction may be created with std::bind or lambda expressions. */
28
29class TaskPool {
30 public:
31 struct Summary {
32 /* Time spent to handle all tasks. */
33 double time_total;
34
35 /* Number of all tasks handled by this pool. */
37
38 /* A full multi-line description of the state of the pool after
39 * all work is done.
40 */
41 string full_report() const;
42 };
43
44 TaskPool();
45 ~TaskPool();
46
47 void push(TaskRunFunction &&task);
48
49 void wait_work(Summary *stats = NULL); /* work and wait until all tasks are done */
50 void cancel(); /* cancel all tasks and wait until they are no longer executing */
51
52 static bool canceled(); /* For worker threads, test if current task pool canceled. */
53
54 protected:
55 tbb::task_group tbb_group;
56
57 /* ** Statistics ** */
58
59 /* Time stamp of first task pushed. */
60 double start_time;
61
62 /* Number of all tasks pushed to the pool. Cleared after wait_work() and cancel(). */
64};
65
73 public:
74 static void init(int num_threads = 0);
75 static void exit();
76 static void free_memory();
77
78 /* Maximum number of threads that will work on task. Use as little as
79 * possible and leave scheduling and splitting up tasks to the scheduler. */
80 static int max_concurrency();
81
82 protected:
84 static int users;
86
87#ifdef WITH_TBB_GLOBAL_CONTROL
88 static tbb::global_control *global_control;
89#endif
90};
91
92/* Dedicated Task Pool
93 *
94 * Like a TaskPool, but will launch one dedicated thread to execute all tasks.
95 *
96 * The run callback that actually executes the task may be created like this:
97 * function_bind(&MyClass::task_execute, this, _1, _2) */
98
100 public:
103
104 void push(TaskRunFunction &&run, bool front = false);
105
106 void wait(); /* wait until all tasks are done */
107 void cancel(); /* cancel all tasks, keep worker thread running */
108
109 bool canceled(); /* for worker thread, test if canceled */
110
111 protected:
112 void num_decrease(int done);
113 void num_increase();
114
115 void thread_run();
117
118 void clear();
119
122
123 list<TaskRunFunction> queue;
126
127 int num;
130
132};
133
135
136#endif
void init()
thread_condition_variable num_cond
Definition task.h:121
void thread_run()
Definition task.cpp:204
thread_mutex queue_mutex
Definition task.h:124
thread_mutex num_mutex
Definition task.h:120
void num_decrease(int done)
Definition task.cpp:167
void push(TaskRunFunction &&run, bool front=false)
Definition task.cpp:126
thread * worker_thread
Definition task.h:131
thread_condition_variable queue_cond
Definition task.h:125
bool canceled()
Definition task.cpp:162
void num_increase()
Definition task.cpp:178
list< TaskRunFunction > queue
Definition task.h:123
bool thread_wait_pop(TaskRunFunction &task)
Definition task.cpp:185
static void free_memory()
Definition task.cpp:93
static void exit()
Definition task.cpp:82
static thread_mutex mutex
Definition task.h:83
static int users
Definition task.h:84
static int active_num_threads
Definition task.h:85
static int max_concurrency()
Definition task.cpp:98
#define CCL_NAMESPACE_END
#define NULL
string full_report() const
Definition task.cpp:235
int num_tasks_handled
Definition task.h:36
double time_total
Definition task.h:33
void push(TaskRunFunction &&task)
Definition task.cpp:22
static bool canceled()
Definition task.cpp:49
int num_tasks_pushed
Definition task.h:63
tbb::task_group tbb_group
Definition task.h:55
double start_time
Definition task.h:60
~TaskPool()
Definition task.cpp:17
void cancel()
Definition task.cpp:40
TaskPool()
Definition task.cpp:15
void wait_work(Summary *stats=NULL)
Definition task.cpp:28
function< void(void)> TaskRunFunction
Definition task.h:19
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition thread.h:29
std::condition_variable thread_condition_variable
Definition thread.h:31