23#ifndef vtkThreadedCallbackQueue_h
24#define vtkThreadedCallbackQueue_h
27#include "vtkParallelCoreModule.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>
359 friend class ThreadWorker;
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);
431 void Invoke(vtkSharedFutureBase* invoker, std::unique_lock<std::mutex>& lock);
437 bool TryInvoke(vtkSharedFutureBase* invoker);
443 template <
class FT,
class... ArgsT>
444 void PushControl(FT&& f, ArgsT&&... args);
449 template <
class SharedFutureContainerT>
450 static bool MustWait(SharedFutureContainerT&& priorSharedFutures);
455 std::deque<SharedFutureBasePointer> InvokerQueue;
465 std::mutex ControlMutex;
471 std::mutex DestroyMutex;
476 std::mutex ThreadIdToIndexMutex;
478 std::condition_variable ConditionVariable;
484 bool Destroying =
false;
489 std::atomic_int NumberOfThreads;
491 std::vector<std::thread> Threads;
500 std::unordered_map<std::thread::id, std::shared_ptr<std::atomic_int>> ThreadIdToIndex;
506 std::unordered_set<SharedFutureBasePointer> ControlFutures;
514#include "vtkThreadedCallbackQueue.txx"
a simple class to control print indentation
abstract base class for most VTK objects
abstract base class for most VTK objects
Hold a reference to a vtkObjectBase instance.
vtkSharedFutureBase is the base block to store, run, get the returned value of the tasks that are pus...
virtual void Wait() const
Blocks current thread until the task associated with this future has terminated.
vtkBaseTypeMacro(vtkSharedFutureBase, vtkObjectBase)
A vtkSharedFuture is an object returned by the methods Push and PushDependent.
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
simple threaded callback queue
int GetNumberOfThreads() const
Returns the number of allocated threads.
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.
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.
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.