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