Blender  V3.3
task.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __UTIL_TASK_H__
5 #define __UTIL_TASK_H__
6 
7 #include "util/list.h"
8 #include "util/string.h"
9 #include "util/tbb.h"
10 #include "util/thread.h"
11 #include "util/vector.h"
12 
14 
15 class TaskPool;
16 class TaskScheduler;
17 
18 typedef function<void(void)> TaskRunFunction;
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 
28 class 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();
44  ~TaskPool();
45 
46  void push(TaskRunFunction &&task);
47 
48  void wait_work(Summary *stats = NULL); /* 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 
65 /* Task Scheduler
66  *
67  * Central scheduler that holds running threads ready to execute tasks. A single
68  * queue holds the task from all pools. */
69 
71  public:
72  static void init(int num_threads = 0);
73  static void exit();
74  static void free_memory();
75 
76  /* Maximum number of threads that will work on task. Use as little as
77  * possible and leave scheduling and splitting up tasks to the scheduler. */
78  static int max_concurrency();
79 
80  protected:
82  static int users;
83  static int active_num_threads;
84 
85 #ifdef WITH_TBB_GLOBAL_CONTROL
86  static tbb::global_control *global_control;
87 #endif
88 };
89 
90 /* Dedicated Task Pool
91  *
92  * Like a TaskPool, but will launch one dedicated thread to execute all tasks.
93  *
94  * The run callback that actually executes the task may be created like this:
95  * function_bind(&MyClass::task_execute, this, _1, _2) */
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(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;
126  bool do_cancel;
127  bool do_exit;
128 
130 };
131 
133 
134 #endif
thread_condition_variable num_cond
Definition: task.h:119
bool do_exit
Definition: task.h:127
bool do_cancel
Definition: task.h:126
void thread_run()
Definition: task.cpp:200
void clear()
Definition: task.cpp:217
thread_mutex queue_mutex
Definition: task.h:122
thread_mutex num_mutex
Definition: task.h:118
void num_decrease(int done)
Definition: task.cpp:165
void push(TaskRunFunction &&run, bool front=false)
Definition: task.cpp:127
void cancel()
Definition: task.cpp:150
thread * worker_thread
Definition: task.h:129
thread_condition_variable queue_cond
Definition: task.h:123
bool canceled()
Definition: task.cpp:160
void num_increase()
Definition: task.cpp:175
list< TaskRunFunction > queue
Definition: task.h:121
bool thread_wait_pop(TaskRunFunction &task)
Definition: task.cpp:182
static void free_memory()
Definition: task.cpp:94
static void exit()
Definition: task.cpp:83
static void init(int num_threads=0)
Definition: task.cpp:62
static thread_mutex mutex
Definition: task.h:81
static int users
Definition: task.h:82
static int active_num_threads
Definition: task.h:83
static int max_concurrency()
Definition: task.cpp:99
Definition: thread.h:34
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
SyclQueue void void size_t num_bytes void
struct blender::compositor::@179::@181 task
string full_report() const
Definition: task.cpp:231
int num_tasks_handled
Definition: task.h:35
double time_total
Definition: task.h:32
void push(TaskRunFunction &&task)
Definition: task.cpp:23
static bool canceled()
Definition: task.cpp:50
int num_tasks_pushed
Definition: task.h:62
tbb::task_group tbb_group
Definition: task.h:54
double start_time
Definition: task.h:59
~TaskPool()
Definition: task.cpp:18
void cancel()
Definition: task.cpp:41
TaskPool()
Definition: task.cpp:14
void wait_work(Summary *stats=NULL)
Definition: task.cpp:29
function< void(void)> TaskRunFunction
Definition: task.h:16
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: thread.h:27
std::condition_variable thread_condition_variable
Definition: thread.h:29