HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_TaskArena.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_TaskArena.h (UT Library, C++)
7  *
8  * COMMENTS: Wrapper for tbb::task_arena
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_TASKARENA_H_INCLUDED__
14 #define __UT_TASKARENA_H_INCLUDED__
15 
16 #include "UT_API.h"
17 #include "UT_NonCopyable.h"
18 #include <SYS/SYS_AtomicInt.h>
19 #include <stddef.h>
20 
21 
22 /// @file
23 /// @class UT_TaskArena
24 ///
25 /// Simple wrapper for running code within a tbb::task_arena. For most usages,
26 /// you want to do this if you're going to spawn tasks with a lock held.
28 {
29 public:
30  static const int automatic = -1;
31 
32  UT_TaskArena(int max_concurrency = automatic,
33  unsigned reserved_for_masters = 1);
34  ~UT_TaskArena();
35 
36  template <typename F>
37  void execute(F &functor)
38  {
39  internalExecute(&UT_TaskArena::callback<F>, &functor);
40  }
41 
42  template <typename F>
43  void execute(const F &functor)
44  {
45  internalExecute(&UT_TaskArena::callbackConst<F>, &functor);
46  }
47 
48  template <typename M, typename F>
49  static void
50  lockedExecute(M &mutex, F &functor)
51  {
52  typename M::Scope scope(mutex);
53  UT_TaskArena arena;
54  arena.execute(functor);
55  }
56 
57  template <typename M, typename F>
58  static void
59  lockedExecute(M &mutex, const F &functor)
60  {
61  typename M::Scope scope(mutex);
62  UT_TaskArena arena;
63  arena.execute(functor);
64  }
65 
66  /// Call this before using the arena if execute
67  /// may be called from multiple threads.
68  void initialize();
69 
70  // Debug counter. Only modified on debug builds
72 
73 private:
74 
75  template <typename F>
76  static void callback(void *data)
77  {
78  (*reinterpret_cast<F *>(data))();
79  }
80  template <typename F>
81  static void callbackConst(const void *data)
82  {
83  (*reinterpret_cast<const F *>(data))();
84  }
85 
86  void internalExecute(void (*callback)(void *), void *data);
87  void internalExecute(void (*callback)(const void *), const void *data);
88 
89 private:
90  static const size_t SIZEOF_ARENA = 40;
91  uint8 myArena[SIZEOF_ARENA];
92 };
93 
94 #endif // __UT_TASKARENA_H_INCLUDED__
static void lockedExecute(M &mutex, const F &functor)
Definition: UT_TaskArena.h:59
void execute(F &functor)
Definition: UT_TaskArena.h:37
#define UT_API
Definition: UT_API.h:13
static void lockedExecute(M &mutex, F &functor)
Definition: UT_TaskArena.h:50
GLboolean * data
Definition: glcorearb.h:130
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:318
static SYS_AtomicInt32 theArenaCount
Definition: UT_TaskArena.h:71
void execute(const F &functor)
Definition: UT_TaskArena.h:43
unsigned char uint8
Definition: SYS_Types.h:32