HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Task.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_Task.h (UT Library, C++)
7  *
8  * COMMENTS: Wrapper for tbb::task
9  */
10 
11 #ifndef __UT_TASK_H_INCLUDED__
12 #define __UT_TASK_H_INCLUDED__
13 
14 #include "UT_API.h"
15 #include "UT_Array.h"
16 #include "UT_TaskScope.h"
17 #include <tbb/task.h>
18 #include <tbb/task_group.h>
19 
20 class UT_TaskList;
21 
22 class UT_API UT_Task : private tbb::task
23 {
24  typedef tbb::task me;
25 
26 public:
28  : myParentScope(nullptr)
29  {
30  }
31 
32  void setParentScope(const UT_TaskScope *parent)
33  {
34  myParentScope = parent;
35  }
36 
37  void setRefCount(int count)
38  {
39  me::set_ref_count(count);
40  }
42  {
43  me::increment_ref_count();
44  }
46  {
47  return me::decrement_ref_count();
48  }
49 
51  {
52  me::recycle_as_continuation();
53  }
55  {
56  me::recycle_as_safe_continuation();
57  }
58  void destroy()
59  {
60  me::destroy(*this);
61  }
62 
63  static void spawnRootAndWait(UT_Task &task)
64  {
66  me::spawn_root_and_wait(task);
67  }
68  void enqueue()
69  {
70  setParentScope(UT_TaskScope::getCurrent());
71  me::enqueue(*this);
72  }
73  void spawnChild(UT_Task &task)
74  {
75  task.setParentScope(myParentScope);
76  me::spawn(task);
77  }
78 
79  /// Should be overriden by derived classes.
80  /// Returns the next task to run, nullptr if none.
81  virtual UT_Task * run() = 0;
82 
83  // Methods imported from tbb::task
84  using me::allocate_root;
85  using me::allocate_child;
86 
88  {
89  me::cancel_group_execution();
90  }
91  bool isCancelled() const
92  {
93  return me::is_cancelled();
94  }
95  void waitForAll()
96  {
97  me::wait_for_all();
98  }
99 
100 private:
101 
102  // Wrapper so that we maintain the UT_TaskScope hierarchy for UT_TaskLock
103  UT_Task * execute() override
104  {
105  UT_TaskScope scope(myParentScope);
106  return run();
107  }
108 
109  friend class UT_TaskList;
110 
111 private:
112 
113  const UT_TaskScope * myParentScope;
114 };
115 
116 
118 {
119 public:
120  UT_Task *run() override
121  {
122  return nullptr;
123  }
124 };
125 
126 class UT_API UT_TaskList : private tbb::task_list
127 {
128  typedef tbb::task_list me;
129 public:
130 
131  /// Returns true when number of tasks is non-zero
132  bool isEmpty() const
133  {
134  return me::empty();
135  }
136 
137  /// Removes all tasks
138  void clear()
139  {
140  myList.clear();
141  me::clear();
142  }
143 
144  /// Append a task
145  void append(UT_Task &task)
146  {
147  myList.append(&task);
148  me::push_back(task);
149  }
150 
151  /// Spawn this task list as root tasks and wait
153  {
154  const UT_TaskScope *parent = UT_TaskScope::getCurrent();
155  for (int i = 0; i < myList.entries(); i++)
156  myList(i)->setParentScope(parent);
157 
158  tbb::task::spawn_root_and_wait(*this);
159  }
160 
161  /// Run this task list in serial
162  void runSerial()
163  {
164  for (int i = 0; i < myList.entries(); i++)
165  myList(i)->run();
166  }
167 
168 private:
169  UT_Array<UT_Task *> myList;
170 };
171 
172 class UT_API UT_TaskGroupContext : private tbb::task_group_context
173 {
174  typedef tbb::task_group_context me;
175 
176 public:
177 
178  enum KindType
179  {
180  ISOLATED = me::isolated,
181  BOUND = me::bound
182  };
183 
184  UT_TaskGroupContext(KindType relation_to_parent = BOUND)
185  : task_group_context((kind_type)relation_to_parent)
186  {
187  }
188 
189  void reset()
190  {
191  me::reset();
192  }
193 
195  {
196  return me::cancel_group_execution();
197  }
198 
200  {
201  return me::is_group_execution_cancelled();
202  }
203 };
204 
205 class UT_API UT_TaskGroup : private tbb::task_group
206 {
207  typedef tbb::task_group me;
208 
209 public:
211  : tbb::task_group()
212  {
213  }
215  {
216  }
217 
218  template <typename F>
219  void run(const F &f)
220  {
221  me::run(f);
222  }
223 
224  template <typename F>
225  void run(tbb::task_handle<F> &f)
226  {
227  me::run(f);
228  }
229 
230  template <typename F>
231  void runAndWait(const F &f)
232  {
233  me::run_and_wait(f);
234  }
235 
236  template <typename F>
237  void runAndWait(tbb::task_handle<F> &f)
238  {
239  me::run_and_wait(f);
240  }
241 
242  // American English
243  bool isCanceling() { return me::is_canceling(); }
244  // British English
245  bool isCancelling() { return me::is_canceling(); }
246 
247  tbb::task_group_status wait() { return me::wait(); }
248  void cancel() { me::cancel(); }
249 };
250 
251 #endif // __UT_TASK_H_INCLUDED__
void recycleAsContinuation()
Definition: UT_Task.h:50
static void spawnRootAndWait(UT_Task &task)
Definition: UT_Task.h:63
bool isCancelling()
Definition: UT_Task.h:245
int decrementRefCount()
Definition: UT_Task.h:45
void spawnRootAndWait()
Spawn this task list as root tasks and wait.
Definition: UT_Task.h:152
void append(UT_Task &task)
Append a task.
Definition: UT_Task.h:145
void destroy()
Definition: UT_Task.h:58
UT_TaskGroupContext(KindType relation_to_parent=BOUND)
Definition: UT_Task.h:184
UT_Task * run() override
Definition: UT_Task.h:120
static const UT_TaskScope * getCurrent()
Definition: UT_TaskScope.h:94
void cancelGroupExecution()
Definition: UT_Task.h:87
UT_TaskGroup()
Definition: UT_Task.h:210
#define UT_API
Definition: UT_API.h:13
GLboolean reset
Definition: glew.h:4959
void runAndWait(tbb::task_handle< F > &f)
Definition: UT_Task.h:237
~UT_TaskGroup()
Definition: UT_Task.h:214
void enqueue()
Definition: UT_Task.h:68
tbb::task_group_status wait()
Definition: UT_Task.h:247
void recycleAsSafeContinuation()
Definition: UT_Task.h:54
void runAndWait(const F &f)
Definition: UT_Task.h:231
void setParentScope(const UT_TaskScope *parent)
Definition: UT_Task.h:32
void setRefCount(int count)
Definition: UT_Task.h:37
GLclampf f
Definition: glew.h:3499
*tasks wait()
void incrementRefCount()
Definition: UT_Task.h:41
void spawnChild(UT_Task &task)
Definition: UT_Task.h:73
void run(tbb::task_handle< F > &f)
Definition: UT_Task.h:225
UT_Task()
Definition: UT_Task.h:27
void cancel()
Definition: UT_Task.h:248
void clear()
Removes all tasks.
Definition: UT_Task.h:138
bool isEmpty() const
Returns true when number of tasks is non-zero.
Definition: UT_Task.h:132
bool cancelGroupExecution()
Definition: UT_Task.h:194
GLuint GLuint GLsizei count
Definition: glew.h:1253
bool isCanceling()
Definition: UT_Task.h:243
void waitForAll()
Definition: UT_Task.h:95
void run(const F &f)
Definition: UT_Task.h:219
bool isGroupExecutionCancelled() const
Definition: UT_Task.h:199
bool isCancelled() const
Definition: UT_Task.h:91
void runSerial()
Run this task list in serial.
Definition: UT_Task.h:162