All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
singularTask.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef WORK_SINGULARTASK_H
25 #define WORK_SINGULARTASK_H
26 
28 
29 #include "pxr/pxr.h"
30 
31 #include <atomic>
32 #include <functional>
33 #include <type_traits>
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 class WorkDispatcher;
39 
58 {
59 public:
60 
61  WorkSingularTask(WorkSingularTask const &) = delete;
62  WorkSingularTask &operator=(WorkSingularTask const &) = delete;
63 
64 #ifdef doxygen
65 
74  template <class Callable, class A1, class A2, ... class AN>
75  WorkSingularTask(WorkDispatcher &dispatcher,
76  Callable &&c, A1 &&a1, A2 &&a2, ... AN &&aN);
77 
79  template <class Callable, class A1, class A2, ... class AN>
80  WorkSingularTask(WorkArenaDispatcher &dispatcher,
81  Callable &&c, A1 &&a1, A2 &&a2, ... AN &&aN);
82 
83 #else // doxygen
84 
85  template <class Callable, class... Args>
86  WorkSingularTask(WorkDispatcher &d, Callable &&c, Args&&... args)
87  : _waker(_MakeWaker(d, std::bind(std::forward<Callable>(c),
88  std::forward<Args>(args)...)))
89  , _count(0) {}
90 
91  template <class Callable, class... Args>
92  WorkSingularTask(WorkArenaDispatcher &d, Callable &&c, Args&&... args)
93  : _waker(_MakeWaker(d, std::bind(std::forward<Callable>(c),
94  std::forward<Args>(args)...)))
95  , _count(0) {}
96 
97 #endif // doxygen
98 
102  inline void Wake() {
103  if (++_count == 1)
104  _waker(_count);
105  }
106 
107 private:
108  template <class Dispatcher, class Fn>
109  struct _Waker {
110  explicit _Waker(Dispatcher &d, Fn &&fn)
111  : _dispatcher(d), _fn(std::move(fn)) {}
112 
113  void operator()(std::atomic_size_t &count) const {
114  _dispatcher.Run(
115  [this, &count]() {
116  // We read the current refCount into oldCount, then we
117  // invoke the task function. Finally we try to CAS the
118  // refCount to zero. If we fail, it means some other
119  // clients have invoked Wake() in the meantime. In that
120  // case we go again to ensure the task can do whatever it
121  // was awakened to do. Once we successfully take the count
122  // to zero, we stop.
123  size_t old = count;
124  do { _fn(); } while (
125  !count.compare_exchange_strong(old, 0));
126  });
127  }
128  Dispatcher &_dispatcher;
129  Fn _fn;
130  };
131 
132  template <class Dispatcher, class Fn>
133  static std::function<void (std::atomic_size_t &)>
134  _MakeWaker(Dispatcher &d, Fn &&fn) {
135  return std::function<void (std::atomic_size_t &)>(
136  _Waker<Dispatcher, typename std::decay<Fn>::type>(
137  d, std::forward<Fn>(fn)));
138  }
139 
140  std::function<void (std::atomic_size_t &)> _waker;
141  std::atomic_size_t _count;
142 };
143 
144 PXR_NAMESPACE_CLOSE_SCOPE
145 
146 #endif // WORK_SINGULARTASK_H
void Wake()
Ensure that this task runs at least once after this call.
Definition: singularTask.h:102
A work dispatcher runs concurrent tasks.
Definition: dispatcher.h:71
This is a specialization of the WorkDispatcher that uses an isolated arena to Run() all its tasks in...
A WorkSingularTask runs a task in a WorkDispatcher, but never concurrently with itself.
Definition: singularTask.h:57