HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
detachedTask.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_DETACHED_TASK_H
8 #define PXR_BASE_WORK_DETACHED_TASK_H
9 
10 /// \file work/detachedTask.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/tf/errorMark.h"
14 #include "pxr/base/work/api.h"
16 #include "pxr/base/work/impl.h"
17 
18 #include <type_traits>
19 #include <utility>
20 
22 
23 template <class Fn>
25 {
26  explicit Work_DetachedTask(Fn &&fn) : _fn(std::move(fn)) {}
27  explicit Work_DetachedTask(Fn const &fn) : _fn(fn) {}
28  void operator()() const {
29  TfErrorMark m;
30  _fn();
31  m.Clear();
32  }
33 private:
34  Fn _fn;
35 };
36 
37 /// Invoke \p fn asynchronously, discard any errors it produces, and provide
38 /// no way to wait for it to complete.
39 template <class Fn>
40 void WorkRunDetachedTask(Fn &&fn)
41 {
42  using FnType = typename std::remove_reference<Fn>::type;
43  Work_DetachedTask<FnType> task(std::forward<Fn>(fn));
44  if (WorkHasConcurrency()) {
46  WorkImpl_RunDetachedTask<Work_DetachedTask<FnType>>(std::move(task));
47  }
48  else {
49  task();
50  }
51 }
52 
54 
55 #endif // PXR_BASE_WORK_DETACHED_TASK_H
type
Definition: core.h:556
Work_DetachedTask(Fn const &fn)
Definition: detachedTask.h:27
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
void WorkRunDetachedTask(Fn &&fn)
Definition: detachedTask.h:40
bool Clear() const
Definition: errorMark.h:92
Work_DetachedTask(Fn &&fn)
Definition: detachedTask.h:26
WORK_API bool WorkHasConcurrency()
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void operator()() const
Definition: detachedTask.h:28
#define PXR_WORK_IMPL_NAMESPACE_USING_DIRECTIVE
Definition: impl.h:17