Work queue subsystem for multithreading. More...

#include <Urho3D/Core/WorkQueue.h>

Inheritance diagram for Urho3D::WorkQueue:
[legend]
Collaboration diagram for Urho3D::WorkQueue:
[legend]

Public Member Functions

 WorkQueue (Context *context)
 Construct.
 
 ~WorkQueue () override
 Destruct.
 
void CreateThreads (unsigned numThreads)
 Create worker threads. Can only be called once.
 
SharedPtr< WorkItemGetFreeItem ()
 Get pointer to an usable WorkItem from the item pool. Allocate one if no more free items.
 
void AddWorkItem (const SharedPtr< WorkItem > &item)
 Add a work item and resume worker threads.
 
bool RemoveWorkItem (SharedPtr< WorkItem > item)
 Remove a work item before it has started executing. Return true if successfully removed.
 
unsigned RemoveWorkItems (const Vector< SharedPtr< WorkItem > > &items)
 Remove a number of work items before they have started executing. Return the number of items successfully removed.
 
void Pause ()
 Pause worker threads.
 
void Resume ()
 Resume worker threads.
 
void Complete (unsigned priority)
 Finish all queued work which has at least the specified priority. Main thread will also execute priority work. Pause worker threads if no more work remains.
 
void SetTolerance (int tolerance)
 Set the pool telerance before it starts deleting pool items.
 
void SetNonThreadedWorkMs (int ms)
 Set how many milliseconds maximum per frame to spend on low-priority work, when there are no worker threads.
 
unsigned GetNumThreads () const
 Return number of worker threads.
 
bool IsCompleted (unsigned priority) const
 Return whether all work with at least the specified priority is finished.
 
bool IsCompleting () const
 Return whether the queue is currently completing work in the main thread.
 
int GetTolerance () const
 Return the pool tolerance.
 
int GetNonThreadedWorkMs () const
 Return how many milliseconds maximum to spend on non-threaded low-priority work.
 
- Public Member Functions inherited from Urho3D::Object
 Object (Context *context)
 Construct.
 
 ~Object () override
 Destruct. Clean up self from event sender & receiver structures.
 
virtual StringHash GetType () const =0
 
virtual const StringGetTypeName () const =0
 
virtual const TypeInfoGetTypeInfo () const =0
 Return type info.
 
virtual void OnEvent (Object *sender, StringHash eventType, VariantMap &eventData)
 Handle event.
 
bool IsInstanceOf (StringHash type) const
 Check current instance is type of specified type.
 
bool IsInstanceOf (const TypeInfo *typeInfo) const
 Check current instance is type of specified type.
 
template<typename T >
bool IsInstanceOf () const
 Check current instance is type of specified class.
 
template<typename T >
T * Cast ()
 Cast the object to specified most derived class.
 
template<typename T >
const T * Cast () const
 Cast the object to specified most derived class.
 
void SubscribeToEvent (StringHash eventType, EventHandler *handler)
 Subscribe to an event that can be sent by any sender.
 
void SubscribeToEvent (Object *sender, StringHash eventType, EventHandler *handler)
 Subscribe to a specific sender's event.
 
void SubscribeToEvent (StringHash eventType, const std::function< void(StringHash, VariantMap &)> &function, void *userData=nullptr)
 Subscribe to an event that can be sent by any sender.
 
void SubscribeToEvent (Object *sender, StringHash eventType, const std::function< void(StringHash, VariantMap &)> &function, void *userData=nullptr)
 Subscribe to a specific sender's event.
 
void UnsubscribeFromEvent (StringHash eventType)
 Unsubscribe from an event.
 
void UnsubscribeFromEvent (Object *sender, StringHash eventType)
 Unsubscribe from a specific sender's event.
 
void UnsubscribeFromEvents (Object *sender)
 Unsubscribe from a specific sender's events.
 
void UnsubscribeFromAllEvents ()
 Unsubscribe from all events.
 
void UnsubscribeFromAllEventsExcept (const PODVector< StringHash > &exceptions, bool onlyUserData)
 Unsubscribe from all events except those listed, and optionally only those with userdata (script registered events).
 
void SendEvent (StringHash eventType)
 Send event to all subscribers.
 
void SendEvent (StringHash eventType, VariantMap &eventData)
 Send event with parameters to all subscribers.
 
VariantMapGetEventDataMap () const
 Return a preallocated map for event data. Used for optimization to avoid constant re-allocation of event data maps.
 
template<typename... Args>
void SendEvent (StringHash eventType, Args... args)
 Send event with variadic parameter pairs to all subscribers. The parameter pairs is a list of paramID and paramValue separated by comma, one pair after another.
 
ContextGetContext () const
 Return execution context.
 
const VariantGetGlobalVar (StringHash key) const
 
const VariantMapGetGlobalVars () const
 
void SetGlobalVar (StringHash key, const Variant &value)
 
ObjectGetSubsystem (StringHash type) const
 Return subsystem by type.
 
ObjectGetEventSender () const
 Return active event sender. Null outside event handling.
 
EventHandlerGetEventHandler () const
 Return active event handler. Null outside event handling.
 
bool HasSubscribedToEvent (StringHash eventType) const
 Return whether has subscribed to an event without specific sender.
 
bool HasSubscribedToEvent (Object *sender, StringHash eventType) const
 Return whether has subscribed to a specific sender's event.
 
bool HasEventHandlers () const
 Return whether has subscribed to any event.
 
template<class T >
T * GetSubsystem () const
 Template version of returning a subsystem.
 
const StringGetCategory () const
 
void SetBlockEvents (bool block)
 Block object from sending and receiving events.
 
bool GetBlockEvents () const
 Return sending and receiving events blocking status.
 
- Public Member Functions inherited from Urho3D::RefCounted
 RefCounted ()
 Construct. Allocate the reference count structure and set an initial self weak reference.
 
virtual ~RefCounted ()
 Destruct. Mark as expired and also delete the reference count structure if no outside weak references exist.
 
 RefCounted (const RefCounted &rhs)=delete
 Prevent copy construction.
 
RefCountedoperator= (const RefCounted &rhs)=delete
 Prevent assignment.
 
void AddRef ()
 Increment reference count. Can also be called outside of a SharedPtr for traditional reference counting.
 
void ReleaseRef ()
 Decrement reference count and delete self if no more references. Can also be called outside of a SharedPtr for traditional reference counting.
 
int Refs () const
 
int WeakRefs () const
 
RefCountRefCountPtr ()
 Return pointer to the reference count structure.
 

Private Member Functions

 URHO3D_OBJECT (WorkQueue, Object)
 
void ProcessItems (unsigned threadIndex)
 Process work items until shut down. Called by the worker threads.
 
void PurgeCompleted (unsigned priority)
 Purge completed work items which have at least the specified priority, and send completion events as necessary.
 
void PurgePool ()
 Purge the pool to reduce allocation where its unneeded.
 
void ReturnToPool (SharedPtr< WorkItem > &item)
 Return a work item to the pool.
 
void HandleBeginFrame (StringHash eventType, VariantMap &eventData)
 Handle frame start event. Purge completed work from the main thread queue, and perform work if no threads at all.
 

Private Attributes

Vector< SharedPtr< WorkerThread > > threads_
 Worker threads.
 
List< SharedPtr< WorkItem > > poolItems_
 Work item pool for reuse to cut down on allocation. The bool is a flag for item pooling and whether it is available or not.
 
List< SharedPtr< WorkItem > > workItems_
 Work item collection. Accessed only by the main thread.
 
List< WorkItem * > queue_
 Work item prioritized queue for worker threads. Pointers are guaranteed to be valid (point to workItems).
 
Mutex queueMutex_
 Worker queue mutex.
 
std::atomic< bool > shutDown_
 Shutting down flag.
 
std::atomic< bool > pausing_
 Pausing flag. Indicates the worker threads should not contend for the queue mutex.
 
bool paused_
 Paused flag. Indicates the queue mutex being locked to prevent worker threads using up CPU time.
 
bool completing_
 Completing work in the main thread flag.
 
int tolerance_
 Tolerance for the shared pool before it begins to deallocate.
 
unsigned lastSize_
 Last size of the shared pool.
 
int maxNonThreadedWorkMs_
 Maximum milliseconds per frame to spend on low-priority work, when there are no worker threads.
 

Friends

class WorkerThread
 

Additional Inherited Members

- Static Public Member Functions inherited from Urho3D::Object
static const TypeInfoGetTypeInfoStatic ()
 Return type info static.
 
- Protected Attributes inherited from Urho3D::Object
Contextcontext_
 Execution context.
 

Detailed Description

Work queue subsystem for multithreading.


The documentation for this class was generated from the following files:
  • Source/Urho3D/Core/WorkQueue.h
  • Source/Urho3D/Core/WorkQueue.cpp