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