23 #ifndef vtkThreadedCallbackQueue_h
24 #define vtkThreadedCallbackQueue_h
27 #include "vtkParallelCoreModule.h"
31 #include <condition_variable>
37 #include <unordered_map>
38 #include <unordered_set>
41 #if !defined(__WRAP__)
43 VTK_ABI_NAMESPACE_BEGIN
58 template <
class T,
class DummyT = std::
nullptr_t>
74 using InvokeResult =
typename Signature<DereferencedType<FT>>::InvokeResult;
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 std::atomic_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;
512 VTK_ABI_NAMESPACE_END
514 #include "vtkThreadedCallbackQueue.txx"
abstract base class for most VTK objects
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
typename ReturnValueWrapper< ReturnT >::ReturnLValueRef ReturnLValueRef
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...
vtkSharedFuture< ReturnT >::ReturnLValueRef Get(SharedFuturePointer< ReturnT > &future)
Get the returned value from the task associated with the input future.
A vtkSharedFuture is an object returned by the methods Push and PushDependent.
void Wait(SharedFutureContainerT &&priorSharedFuture)
This method blocks the current thread until all the tasks associated with each shared future inside p...
a simple class to control print indentation
int GetNumberOfThreads() const
Returns the number of allocated threads.
SharedFuturePointer< InvokeResult< FT > > Push(FT &&f, ArgsT &&...args)
Pushes a function f to be passed args...
abstract base class for most VTK objects
simple threaded callback queue
void SetNumberOfThreads(int numberOfThreads)
Sets the number of threads.
typename ReturnValueWrapper< ReturnT >::ReturnConstLValueRef ReturnConstLValueRef
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.
virtual void Wait() const
Blocks current thread until the task associated with this future has terminated.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...