23#ifndef vtkThreadedCallbackQueue_h
24#define vtkThreadedCallbackQueue_h
26#include "vtkCommonCoreModule.h"
31#include <condition_variable>
37#include <unordered_map>
38#include <unordered_set>
43VTK_ABI_NAMESPACE_BEGIN
58 template <
class T,
class DummyT = std::
nullptr_t>
68 using DereferencedType =
typename std::decay<typename Dereference<T>::Type>::type;
74 using InvokeResult =
typename Signature<DereferencedType<FT>>::InvokeResult;
79 template <class ReturnT, bool IsLValueReference = std::is_lvalue_reference<ReturnT>::value>
80 class ReturnValueWrapper
82 class ReturnLValueRef;
83 class ReturnConstLValueRef;
108 : NumberOfPriorSharedFuturesRemaining(0)
109 , Status(CONSTRUCTING)
118 if (this->Status == READY)
122 std::unique_lock<std::mutex> lock(this->Mutex);
123 if (this->Status != READY)
125 this->ConditionVariable.wait(lock, [
this] {
return this->Status == READY; });
135 virtual void operator()() = 0;
140 std::atomic_int NumberOfPriorSharedFuturesRemaining;
147 std::atomic_int Status;
160 bool IsHighPriority =
false;
166 std::vector<vtkSmartPointer<vtkSharedFutureBase>> Dependents;
168 mutable std::mutex Mutex;
169 mutable std::condition_variable ConditionVariable;
178 template <
class ReturnT>
204 ReturnValueWrapper<ReturnT> ReturnValue;
211 template <
class ReturnT>
275 template <
class FT,
class... ArgsT>
286 template <
class SharedFutureContainerT,
class FT,
class... ArgsT>
288 SharedFutureContainerT&& priorSharedFutures, FT&& f, ArgsT&&... args);
304 template <
class SharedFutureContainerT>
305 void Wait(SharedFutureContainerT&& priorSharedFuture);
316 template <
class ReturnT>
318 template <
class ReturnT>
348 template <
class FT,
class... ArgsT>
354 template <
class FT,
class... ArgsT>
404 void Sync(
int startId = 0);
410 void PopFrontNullptr();
417 void SignalDependentSharedFutures(vtkSharedFutureBase* invoker);
424 template <
class SharedFutureContainerT,
class InvokerT>
425 void HandleDependentInvoker(SharedFutureContainerT&& priorSharedFutures, InvokerT&& invoker);
430 void Invoke(vtkSharedFutureBase* invoker);
436 bool TryInvoke(vtkSharedFutureBase* invoker);
442 template <
class FT,
class... ArgsT>
443 void PushControl(FT&& f, ArgsT&&... args);
448 template <
class SharedFutureContainerT>
449 static bool MustWait(SharedFutureContainerT&& priorSharedFutures);
454 std::deque<SharedFutureBasePointer> InvokerQueue;
464 std::mutex ControlMutex;
470 std::mutex DestroyMutex;
475 std::mutex ThreadIdToIndexMutex;
477 std::condition_variable ConditionVariable;
483 std::atomic_bool Destroying{
false };
488 std::atomic_int NumberOfThreads;
490 std::vector<std::thread> Threads;
499 std::unordered_map<std::thread::id, std::shared_ptr<std::atomic_int>> ThreadIdToIndex;
505 std::unordered_set<SharedFutureBasePointer> ControlFutures;
507 vtkThreadedCallbackQueue(
const vtkThreadedCallbackQueue&) =
delete;
508 void operator=(
const vtkThreadedCallbackQueue&) =
delete;
513#include "vtkThreadedCallbackQueue.txx"
a simple class to control print indentation
Deserialize VTK objects from JSON.
Hold a reference to a vtkObjectBase instance.
friend class vtkThreadedCallbackQueue
virtual void Wait() const
Blocks current thread until the task associated with this future has terminated.
vtkBaseTypeMacro(vtkSharedFutureBase, vtkObjectBase)
friend class vtkThreadedCallbackQueue
vtkAbstractTypeMacro(vtkSharedFuture< ReturnT >, vtkSharedFutureBase)
vtkSharedFuture()=default
ReturnConstLValueRef Get() const
This returns the return value of the pushed function.
ReturnLValueRef Get()
This returns the return value of the pushed function.
typename ReturnValueWrapper< ReturnT >::ReturnLValueRef ReturnLValueRef
typename ReturnValueWrapper< ReturnT >::ReturnConstLValueRef ReturnConstLValueRef
int GetNumberOfThreads() const
Returns the number of allocated threads.
vtkSmartPointer< vtkSharedFuture< ReturnT > > SharedFuturePointer
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkThreadedCallbackQueue()
vtkSharedFuture< ReturnT >::ReturnConstLValueRef Get(const SharedFuturePointer< ReturnT > &future)
Get the returned value from the task associated with the input future.
SharedFuturePointer< InvokeResult< FT > > Push(FT &&f, ArgsT &&... args)
Pushes a function f to be passed args... as arguments.
friend class ThreadWorker
void Wait(SharedFutureContainerT &&priorSharedFuture)
This method blocks the current thread until all the tasks associated with each shared future inside p...
static vtkThreadedCallbackQueue * New()
SharedFuturePointer< InvokeResult< FT > > PushDependent(SharedFutureContainerT &&priorSharedFutures, FT &&f, ArgsT &&... args)
This method behaves the same way Push does, with the addition of a container of futures.
vtkSmartPointer< vtkSharedFutureBase > SharedFutureBasePointer
void SetNumberOfThreads(int numberOfThreads)
Sets the number of threads.
~vtkThreadedCallbackQueue() override
Any remaining function that was not executed yet will be executed in this destructor.
vtkSharedFuture< ReturnT >::ReturnLValueRef Get(SharedFuturePointer< ReturnT > &future)
Get the returned value from the task associated with the input future.