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 <SYS/SYS_AtomicInt.h>
18 
19 #include <tbb/task_arena.h>
20 
21 #include <functional>
22 #include <utility>
23 
24 #include <stddef.h>
25 
26 
27 /// @file
28 /// @class UT_TaskArena
29 ///
30 /// Simple wrapper for running code within a tbb::task_arena. For most usages,
31 /// you want to do this if you're going to spawn tasks with a lock held.
33 {
34 public:
35  static const int automatic = -1;
36 
37  /// Creates task_arena with certain concurrency limits
38  ///
39  /// Sets up settings only, real construction is deferred till the first
40  /// method invocation.
41  /// @arg max_concurrency specifies total number of slots in arena where
42  /// threads work
43  /// @arg reserved_for_masters specifies number of slots to be used by
44  /// master threads only.
45  /// Value of 1 is default and reflects behavior of implicit arenas.
46  UT_TaskArena(int max_concurrency = automatic,
47  unsigned reserved_for_masters = 1);
48  ~UT_TaskArena();
49 
50  /// Joins the arena and executes a functor, then returns
51  ///
52  /// If not possible to join, wraps the functor into a task, enqueues it and
53  /// waits for task completion. Can decrement the arena demand for workers,
54  /// causing a worker to leave and free a slot to the calling thread.
55  /// @{
56  template <typename F>
57  void execute(F &functor)
58  {
59  internalExecute(&UT_TaskArena::callback<F>, &functor);
60  }
61 
62  template <typename F>
63  void execute(const F &functor)
64  {
65  internalExecute(&UT_TaskArena::callbackConst<F>, &functor);
66  }
67  /// @}
68 
69  /// Executes functor and by locking the given mutex and running it in a
70  /// task_arena for exclusive work isolation.
71  /// @{
72  template <typename M, typename F>
73  static void
74  lockedExecute(M &mutex, F &functor)
75  {
76  typename M::Scope scope(mutex);
77  UT_TaskArena arena;
78  arena.execute(functor);
79  }
80 
81  template <typename M, typename F>
82  static void
83  lockedExecute(M &mutex, const F &functor)
84  {
85  typename M::Scope scope(mutex);
86  UT_TaskArena arena;
87  arena.execute(functor);
88  }
89  /// @}
90 
91  /// Enqueues a task into the arena to process a functor, and immediately
92  /// returns. Does not require the calling thread to join the arena.
93  template<typename F>
94  static void
95  enqueue(F&& f)
96  {
97  tbb::task_arena arena(tbb::task_arena::attach{});
98  arena.enqueue(std::forward<F>(f));
99  }
100 
101  /// Call this before using the arena if execute may be called from multiple
102  /// threads.
103  void initialize();
104 
105  // Debug counter. Only modified on debug builds
107 
108 private:
109 
110  template <typename F>
111  static void callback(void *data)
112  {
113  (*reinterpret_cast<F *>(data))();
114  }
115  template <typename F>
116  static void callbackConst(const void *data)
117  {
118  (*reinterpret_cast<const F *>(data))();
119  }
120 
121  void internalExecute(void (*callback)(void *), void *data);
122  void internalExecute(void (*callback)(const void *), const void *data);
123 
124 private:
125  static const size_t SIZEOF_ARENA = 40;
126  uint8 myArena[SIZEOF_ARENA];
127 };
128 
129 #endif // __UT_TASKARENA_H_INCLUDED__
static void lockedExecute(M &mutex, const F &functor)
Definition: UT_TaskArena.h:83
void execute(F &functor)
Definition: UT_TaskArena.h:57
#define UT_API
Definition: UT_API.h:14
static void enqueue(F &&f)
Definition: UT_TaskArena.h:95
static void lockedExecute(M &mutex, F &functor)
Definition: UT_TaskArena.h:74
unsigned char uint8
Definition: SYS_Types.h:36
OPENVDB_API void initialize()
Global registration of native Grid, Transform, Metadata and Point attribute types. Also initializes blosc (if enabled).
Definition: logging.h:294
static SYS_AtomicInt32 theArenaCount
Definition: UT_TaskArena.h:106
void execute(const F &functor)
Definition: UT_TaskArena.h:63
GLboolean * data
Definition: glcorearb.h:131
GLfloat f
Definition: glcorearb.h:1926
Definition: format.h:895