Loading...
Searching...
No Matches
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/tf/pyLock.h"
16
17#include <tbb/task_arena.h>
18
19#include <utility>
20
21PXR_NAMESPACE_OPEN_SCOPE
22
97template <class Fn>
98auto
99WorkWithScopedParallelism(Fn &&fn, bool dropPythonGIL=true)
100{
101 if (dropPythonGIL) {
102 TF_PY_ALLOW_THREADS_IN_SCOPE();
103 return tbb::this_task_arena::isolate(std::forward<Fn>(fn));
104 }
105 else {
106 return tbb::this_task_arena::isolate(std::forward<Fn>(fn));
107 }
108}
109
116template <class Fn>
117auto
118WorkWithScopedDispatcher(Fn &&fn, bool dropPythonGIL=true)
119{
120 return WorkWithScopedParallelism([&fn]() {
121 WorkDispatcher dispatcher;
122 return std::forward<Fn>(fn)(dispatcher);
123 // dispatcher's destructor invokes Wait() here.
124 }, dropPythonGIL);
125}
126
127PXR_NAMESPACE_CLOSE_SCOPE
128
129#endif // PXR_BASE_WORK_WITH_SCOPED_PARALLELISM_H
130
A work dispatcher runs concurrent tasks.
Definition: dispatcher.h:66