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 
17 #include <type_traits>
18 #include <utility>
19 
21 
22 template <class Fn>
24 {
25  explicit Work_DetachedTask(Fn &&fn) : _fn(std::move(fn)) {}
26  explicit Work_DetachedTask(Fn const &fn) : _fn(fn) {}
27  void operator()() const {
28  TfErrorMark m;
29  _fn();
30  m.Clear();
31  }
32 private:
33  Fn _fn;
34 };
35 
38 
41 
42 /// Invoke \p fn asynchronously, discard any errors it produces, and provide
43 /// no way to wait for it to complete.
44 template <class Fn>
45 void WorkRunDetachedTask(Fn &&fn)
46 {
47  using FnType = typename std::remove_reference<Fn>::type;
48  Work_DetachedTask<FnType> task(std::forward<Fn>(fn));
49  if (WorkHasConcurrency()) {
50  Work_GetDetachedDispatcher().Run(std::move(task));
52  }
53  else {
54  task();
55  }
56 }
57 
59 
60 #endif // PXR_BASE_WORK_DETACHED_TASK_H
type
Definition: core.h:556
WORK_API void Work_EnsureDetachedTaskProgress()
Work_DetachedTask(Fn const &fn)
Definition: detachedTask.h:26
WORK_API WorkDispatcher & Work_GetDetachedDispatcher()
#define WORK_API
Definition: api.h:23
void WorkRunDetachedTask(Fn &&fn)
Definition: detachedTask.h:45
void Run(Callable &&c)
Definition: dispatcher.h:95
bool Clear() const
Definition: errorMark.h:92
Work_DetachedTask(Fn &&fn)
Definition: detachedTask.h:25
WORK_API bool WorkHasConcurrency()
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void operator()() const
Definition: detachedTask.h:27