Loading...
Searching...
No Matches
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 PXR_BASE_WORK_SINGULAR_TASK_H
25#define PXR_BASE_WORK_SINGULAR_TASK_H
26
28
29#include "pxr/pxr.h"
30
31#include <atomic>
32#include <functional>
33#include <type_traits>
34
35PXR_NAMESPACE_OPEN_SCOPE
36
37class WorkDispatcher;
38
57{
58public:
59
60 WorkSingularTask(WorkSingularTask const &) = delete;
61 WorkSingularTask &operator=(WorkSingularTask const &) = delete;
62
63#ifdef doxygen
64
73 template <class Callable, class A1, class A2, ... class AN>
75 Callable &&c, A1 &&a1, A2 &&a2, ... AN &&aN);
76
77#else // doxygen
78
79 template <class Callable, class... Args>
80 WorkSingularTask(WorkDispatcher &d, Callable &&c, Args&&... args)
81 : _waker(_MakeWaker(d, std::bind(std::forward<Callable>(c),
82 std::forward<Args>(args)...)))
83 , _count(0) {}
84
85#endif // doxygen
86
90 inline void Wake() {
91 if (++_count == 1)
92 _waker(_count);
93 }
94
95private:
96 template <class Dispatcher, class Fn>
97 struct _Waker {
98 explicit _Waker(Dispatcher &d, Fn &&fn)
99 : _dispatcher(d), _fn(std::move(fn)) {}
100
101 void operator()(std::atomic_size_t &count) const {
102 _dispatcher.Run(
103 [this, &count]() {
104 // We read the current refCount into oldCount, then we
105 // invoke the task function. Finally we try to CAS the
106 // refCount to zero. If we fail, it means some other
107 // clients have invoked Wake() in the meantime. In that
108 // case we go again to ensure the task can do whatever it
109 // was awakened to do. Once we successfully take the count
110 // to zero, we stop.
111 std::size_t old = count;
112 do { _fn(); } while (
113 !count.compare_exchange_strong(old, 0));
114 });
115 }
116 Dispatcher &_dispatcher;
117 Fn _fn;
118 };
119
120 template <class Dispatcher, class Fn>
121 static std::function<void (std::atomic_size_t &)>
122 _MakeWaker(Dispatcher &d, Fn &&fn) {
123 return std::function<void (std::atomic_size_t &)>(
124 _Waker<Dispatcher, typename std::decay<Fn>::type>(
125 d, std::forward<Fn>(fn)));
126 }
127
128 std::function<void (std::atomic_size_t &)> _waker;
129 std::atomic_size_t _count;
130};
131
132PXR_NAMESPACE_CLOSE_SCOPE
133
134#endif // PXR_BASE_WORK_SINGULAR_TASK_H
A work dispatcher runs concurrent tasks.
Definition: dispatcher.h:76
A WorkSingularTask runs a task in a WorkDispatcher, but never concurrently with itself.
Definition: singularTask.h:57
void Wake()
Ensure that this task runs at least once after this call.
Definition: singularTask.h:90
WorkSingularTask(WorkDispatcher &dispatcher, Callable &&c, A1 &&a1, A2 &&a2,... AN &&aN)
Create a singular task to be run in dispatcher.
STL namespace.