withScopedParallelism.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_WORK_WITH_SCOPED_PARALLELISM_H
8 #define PXR_BASE_WORK_WITH_SCOPED_PARALLELISM_H
9 
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/work/api.h"
15 #include "pxr/base/work/impl.h"
16 #include "pxr/base/tf/pyLock.h"
17 
18 
19 #include <utility>
20 
21 PXR_NAMESPACE_OPEN_SCOPE
22 
98 template <class Fn>
99 auto
100 WorkWithScopedParallelism(Fn &&fn, bool dropPythonGIL=true)
101 {
102  PXR_WORK_IMPL_NAMESPACE_USING_DIRECTIVE;
103 
104  if (dropPythonGIL) {
105  TF_PY_ALLOW_THREADS_IN_SCOPE();
106  return WorkImpl_WithScopedParallelism(std::forward<Fn>(fn));
107  }
108  return WorkImpl_WithScopedParallelism(std::forward<Fn>(fn));
109 }
110 
117 template <class Fn>
118 auto
119 WorkWithScopedDispatcher(Fn &&fn, bool dropPythonGIL=true)
120 {
121  return WorkWithScopedParallelism([&fn]() {
122  WorkDispatcher dispatcher;
123  return std::forward<Fn>(fn)(dispatcher);
124  // dispatcher's destructor invokes Wait() here.
125  }, dropPythonGIL);
126 }
127 
128 PXR_NAMESPACE_CLOSE_SCOPE
129 
130 #endif // PXR_BASE_WORK_WITH_SCOPED_PARALLELISM_H
131 
auto WorkWithScopedDispatcher(Fn &&fn, bool dropPythonGIL=true)
Similar to WorkWithScopedParallelism(), but pass a WorkDispatcher instance to fn for its use during t...
auto WorkWithScopedParallelism(Fn &&fn, bool dropPythonGIL=true)
Invoke fn, ensuring that all wait operations on concurrent constructs invoked by the calling thread o...