HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_TaskList.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_TaskList.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_TASKLIST_H_INCLUDED__
12 #define __UT_TASKLIST_H_INCLUDED__
13 
14 #include "UT_Array.h"
15 #include "UT_Assert.h"
16 #include "UT_Function.h"
17 #include "UT_TaskScope.h"
18 #include "UT_Thread.h"
19 #include <SYS/SYS_Deprecated.h>
20 #include <tbb/parallel_for.h>
21 #include <tbb/task_group.h>
22 #include <type_traits>
23 #include <utility>
24 
25 /// @note Consider using UT_TaskGroup instead, except that UT_TaskGroup will
26 /// NOT spawn with a UT_TaskScope (you need to do this yourself).
28 {
29 public:
30 
31  /// Returns true when number of tasks is non-zero
32  bool isEmpty() const
33  {
34  return myTasks.isEmpty();
35  }
36 
37  /// Removes all tasks
38  void clear()
39  {
40  myTasks.clear();
41  }
42 
43  /// Append a task
44  template <typename Body>
45  void append(Body&& task)
46  {
47  myTasks.emplace_back(std::forward<Body>(task));
48  }
49 
50  /// Spawn this task list as root tasks and wait
52  {
53  tbb::task_group tg;
54  const UT_TaskScope *parent = UT_TaskScope::getCurrent();
55  for (auto&& task : myTasks)
56  {
57  tg.run([parent, task]() {
58  UT_TaskScope task_scope(parent);
59  task();
60  });
61  }
62  (void) tg.wait();
63  }
64 
65  /// Run this task list in serial
66  void runSerial()
67  {
68  for (auto&& task : myTasks)
69  task();
70  }
71 
72 private:
74 };
75 
77 
78 /// Append a UTparallelFor() task to a UT_TaskList for later spawning en masse.
79 /// @see UTparallelFor(), UT_TaskList
80 template <typename RANGE, typename BODY>
82 void
84  UT_TaskList &task_list,
85  RANGE &&range,
86  BODY &&body)
87 {
88  // We forward range and body into the lambda so that if they were movable
89  // temporaries, this can avoid an extra copy.
90  task_list.append(
91  [range = std::forward<RANGE>(range), body = std::forward<BODY>(body)]()
92  {
94  tbb::parallel_for(range, body, tbb::simple_partitioner());
95  });
96 }
97 
99 
100 #endif // __UT_TASKLIST_H_INCLUDED__
void parallel_for(int64_t start, int64_t end, std::function< void(int64_t index)> &&task, parallel_options opt=parallel_options(0, Split_Y, 1))
Definition: parallel.h:127
GLenum GLint * range
Definition: glcorearb.h:1925
void spawnRootAndWait()
Spawn this task list as root tasks and wait.
Definition: UT_TaskList.h:51
void
Definition: png.h:1083
static const UT_TaskScope * getCurrent()
Definition: UT_TaskScope.h:96
#define SYS_DEPRECATED_PUSH_DISABLE()
#define SYS_DEPRECATED_POP_DISABLE()
void append(Body &&task)
Append a task.
Definition: UT_TaskList.h:45
static bool isThreadingEnabled()
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
void UTparallelForAppendToTaskList(UT_TaskList &task_list, RANGE &&range, BODY &&body)
Definition: UT_TaskList.h:83
void UTparallelForRunInTaskGroup(UT_TaskGroup &task_group, RANGE &&range, BODY &&body)
Definition: UT_TaskGroup.h:139
void clear()
Removes all tasks.
Definition: UT_TaskList.h:38
bool isEmpty() const
Returns true when number of tasks is non-zero.
Definition: UT_TaskList.h:32
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
void runSerial()
Run this task list in serial.
Definition: UT_TaskList.h:66