VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Friends
makVrv::DtBackgroundLoader Class Reference

Manages asynchronous and budgeted foreground loading for all DtModel subclasses.

DtBackgroundLoader is a per-DtDe singleton that coordinates the two-phase load pipeline used by every DtModel-derived object:

Phase 1 – backgroundLoad(): CPU-heavy work (file I/O, mesh construction, etc.) that is safe to run on a background thread. Dispatched as TBB tasks via updateQueue().

Phase 2 – foregroundLoad(): work that must run on the main thread (OSG state upload, ICO submission, etc.). Executed within a per-frame time budget so the renderer is not stalled.

Objects that do not support multithreaded loading (supportsMultithreadedLoad() returns false) bypass Phase 1 and have both phases run on the main thread via processForegroundFullLoadBudgeted(), also within the time budget.

Load flow summary:

  1. A DtModel calls loadIfPossibleOrAddToQueue(); its handle enters myProducerQueue.
  2. updateQueue() drains myProducerQueue each frame, routing handles into either myForegroundFullLoadQueue (non-multithreaded) or myForegroundQueue (background-capable).
  3. Background-capable handles are dispatched to TBB tasks. On success the handle moves to myObjectsFinishedWithTBBTask; on failure to myObjectsFailedInTBBTask.
  4. processForegroundLoadBudgeted() and processForegroundFullLoadBudgeted() drain their respective queues within the configured foreground time budget.

Cache payload exchange: backgroundLoad() implementations accumulate cache entries via DtBackgroundLoadContext::addToPayload() instead of calling setCachedData() directly. When a TBB task finishes, ts_stagePayload() posts the payload to myStagedPayloads under a brief mutex lock. At the top of each updateQueue() call, commitStagedPayloads() drains that list on the main thread and commits each payload to its definition via setCachedData(). The mutex is held only for a pointer-swap – no I/O or heavy work occurs under it. In the synchronous bypass paths (executeBackgroundLoad), the payload is committed inline immediately after backgroundLoad() returns, since both calls occur on the same thread.

Book-keeping: myActivelyLoadingModelDefs tracks which model definition names currently have a TBB task in flight, preventing duplicate dispatches for the same definition. Entries are inserted at dispatch time and removed in processForegroundLoadBudgeted() (success) or processFailedTBBTasks() (failure).

Thread safety: Methods prefixed ts_ are safe to call from any thread. All other methods must be called from the main thread only.

Inheritance diagram for makVrv::DtBackgroundLoader:
Inheritance graph
[legend]

Classes

struct  DtBackgroundLoadStats
 
struct  DtPendingIcoQueue
 

Public Member Functions

virtual ~DtBackgroundLoader ()
 
virtual void cleanup ()
 
virtual void updateQueue (int max_thread_count, int active_threads)
 
virtual DtLoaderHandle * queueForLoad (DtModel *object)
 
virtual void loadIfPossibleOrAddToQueue (DtModel *object)
 
virtual void executeBackgroundLoad (DtModel *object)
 
virtual bool hasActiveAsyncTasks () const
 
virtual int numModelDefinitionsToIco () const
 
virtual std::string nextModelDefinitionToIco ()
 
virtual int numOfActiveTasks () const
 
virtual bool multithreadedLoadingActive () const
 
virtual void setMultithreadedLoadingActive (bool val)
 
virtual DtBackgroundLoadStats queryStats ()
 
virtual void waitForActiveTasksAndCommit ()
 
time management functions

how long the system can spend in the main thread before it's stopped. defaults to 2 ms

virtual void setForegroundTimeBudget (double budget)
 
virtual double foregroundTimeBudget () const
 
virtual void incrementForegroundTime (double time_elapse)
 
virtual bool isForegroundTimeBudgetExpired () const
 
virtual double timeSpentThisFrame () const
 
- Public Member Functions inherited from makVrv::DtVirtualBaseClass
 DtVirtualBaseClass ()
 
virtual ~DtVirtualBaseClass ()
 

Static Public Member Functions

static DtBackgroundLoader & instance (DtDe &de)
 

Protected Member Functions

 DtBackgroundLoader (DtDe &de)
 
virtual void ts_setModelDefinitionLoaded (const std::string &modelDefinition)
 
virtual bool ts_isModelDefinitionLoaded (const std::string &modelDefinition) const
 
virtual void ts_stagePayload (DtDefinitionCachePayload *payload)
 
virtual void commitStagedPayloads ()
 
virtual void disposeLoaderHandle (DtLoaderHandle *handle)
 
virtual void timedFullLoad (DtModel *object)
 
virtual void drainCompletedTBBTasks ()
 
virtual void processFailedTBBTasks ()
 
virtual void processForegroundLoadBudgeted ()
 
virtual void processForegroundFullLoadBudgeted ()
 
virtual void deleteAllPendingHandles ()
 
virtual void dispatchBackgroundTasks (int maxThreadCount, int activeThreads)
 

Protected Attributes

DtDe & myDe
 
boost::mutex myProducerQueueMutex
 
std::list< DtLoaderHandle * > myProducerQueue
 
std::list< DtLoaderHandle * > myForegroundQueue
 
std::list< DtLoaderHandle * > myForegroundFullLoadQueue
 
DtPendingIcoQueue myModelDefsToIco
 
boost::mutex myTaskFinishedMutex
 
std::list< DtLoaderHandle * > myObjectsToForegroundLoad
 
std::list< DtLoaderHandle * > myFailedObjects
 
std::list< DtLoaderHandle * > myObjectsFinishedWithTBBTask
 
std::list< DtLoaderHandle * > myObjectsFailedInTBBTask
 
std::map< std::string, bool > myActivelyLoadingModelDefs
 
std::atomic< int > myNumberOfActiveTasks
 
double myTimeSpentThisFrame
 
boost::mutex myMutableLoadedDefMapMutex
 
std::unordered_map
< std::string, bool > 
myLoadedDefMap
 
double myForegroundTimeBudget
 
std::atomic< bool > myMultithreadedLoadingActive
 
tbb::task_arena myTaskArena
 
boost::mutex myTaskDrainMutex
 
boost::condition_variable myTaskDrainCondition
 
boost::mutex myPayloadStagingMutex
 
std::vector
< DtDefinitionCachePayload * > 
myStagedPayloads
 

Private Member Functions

 DtBackgroundLoader ()=delete
 
 DtBackgroundLoader (const DtBackgroundLoader &)=delete
 
DtBackgroundLoader & operator= (const DtBackgroundLoader &)=delete
 

Friends

class DtBackgroundLoadAsyncTask
 

Constructor & Destructor Documentation

virtual makVrv::DtBackgroundLoader::~DtBackgroundLoader ( )
virtual

destructor

makVrv::DtBackgroundLoader::DtBackgroundLoader ( DtDe &  de)
protected

not allowed directly

makVrv::DtBackgroundLoader::DtBackgroundLoader ( )
privatedelete

not allowed

makVrv::DtBackgroundLoader::DtBackgroundLoader ( const DtBackgroundLoader &  )
privatedelete

not allowed

Member Function Documentation

static DtBackgroundLoader& makVrv::DtBackgroundLoader::instance ( DtDe &  de)
static

singleton access

DtBackgroundLoader& makVrv::DtBackgroundLoader::operator= ( const DtBackgroundLoader &  )
privatedelete

not allowed

virtual void makVrv::DtBackgroundLoader::cleanup ( )
virtual

Blocks until all in-flight background load tasks have completed. Must be called before destroying any resource that a background task may access (e.g. DtModelDefinitionManager).

virtual void makVrv::DtBackgroundLoader::updateQueue ( int  max_thread_count,
int  active_threads 
)
virtual

called from the foreground main event loop moves stuff from the producer queue into the consumer one, manages time and does foreground loading

virtual DtLoaderHandle* makVrv::DtBackgroundLoader::queueForLoad ( DtModel *  object)
virtual

takes ownership of the handle

virtual void makVrv::DtBackgroundLoader::setForegroundTimeBudget ( double  budget)
virtual

returns the configured foreground time budget in seconds

virtual double makVrv::DtBackgroundLoader::foregroundTimeBudget ( ) const
virtual

returns the configured foreground time budget in seconds

virtual void makVrv::DtBackgroundLoader::incrementForegroundTime ( double  time_elapse)
virtual

updates how much time has been spend already, allows per frame adjustment of budget

virtual bool makVrv::DtBackgroundLoader::isForegroundTimeBudgetExpired ( ) const
virtual

returns if foreground time is greater then budget

virtual double makVrv::DtBackgroundLoader::timeSpentThisFrame ( ) const
virtual

returns time spend working in the foreground.

virtual void makVrv::DtBackgroundLoader::loadIfPossibleOrAddToQueue ( DtModel *  object)
virtual

Spawns off a background task if definition hasn't been loaded yet, otherwise runs the foreground loading code if there's enough time. If there's not enough time for foreground loading, the object is queued for the next frame. Called by DtModel based classes when setModelDefinition() is called.

virtual void makVrv::DtBackgroundLoader::executeBackgroundLoad ( DtModel *  object)
virtual

Executes only the background load portion for the given model, setting up the lease, mutex and context. Does not call foregroundLoad() or update time budgeting. Used by setModelDefinition() in the non-multithreaded path where foregroundLoad() is called by the caller. The payload accumulated during backgroundLoad() is committed to the definition inline, since this always runs on the main thread.

virtual bool makVrv::DtBackgroundLoader::hasActiveAsyncTasks ( ) const
virtual

returns true if there are tasks active in the background

virtual int makVrv::DtBackgroundLoader::numModelDefinitionsToIco ( ) const
virtual

used by model preloader to decide if there's data to process

virtual std::string makVrv::DtBackgroundLoader::nextModelDefinitionToIco ( )
virtual

name of the next model to look up definition of and process its ICO data.

virtual int makVrv::DtBackgroundLoader::numOfActiveTasks ( ) const
virtual

number of current async tasks running

virtual bool makVrv::DtBackgroundLoader::multithreadedLoadingActive ( ) const
virtual

is the system active or disabled, defaults to on

virtual void makVrv::DtBackgroundLoader::setMultithreadedLoadingActive ( bool  val)
virtual

controls if the system is active

virtual DtBackgroundLoadStats makVrv::DtBackgroundLoader::queryStats ( )
virtual

Returns a point-in-time snapshot of loader state for display in a GUI.

Warning
Must be called from the main thread only. myForegroundQueue, and myModelDefsToIcoQueue are unprotected and are only safe to read when no concurrent writer exists (i.e. on the same thread that calls updateQueue()).
virtual void makVrv::DtBackgroundLoader::waitForActiveTasksAndCommit ( )
virtual

Blocks until all in-flight TBB tasks complete, then commits any staged payloads. Does not disable multithreaded loading or delete handles. Used by DtModelDefinitionManager::clearModelInstancingCache() to ensure no background thread is reading cached data before it is cleared. Must be called from the main thread.

virtual void makVrv::DtBackgroundLoader::ts_setModelDefinitionLoaded ( const std::string &  modelDefinition)
protectedvirtual
virtual bool makVrv::DtBackgroundLoader::ts_isModelDefinitionLoaded ( const std::string &  modelDefinition) const
protectedvirtual
virtual void makVrv::DtBackgroundLoader::ts_stagePayload ( DtDefinitionCachePayload *  payload)
protectedvirtual

Posts a completed payload to myStagedPayloads for later commit on the main thread. Takes ownership of the pointer. Safe to call from any thread. The mutex is held only for the duration of the push_back. Passing a null or empty payload is safe and is a no-op.

virtual void makVrv::DtBackgroundLoader::commitStagedPayloads ( )
protectedvirtual

Drains myStagedPayloads under a brief mutex lock, then commits each payload to its definition on the main thread. Called at the top of updateQueue() before any foreground processing begins. Main thread only.

virtual void makVrv::DtBackgroundLoader::disposeLoaderHandle ( DtLoaderHandle *  handle)
protectedvirtual

Releases a DtLoaderHandle, routing to the correct owner.

If handle->myObject is non-null, the model is still alive and owns the handle via myInLoadQueueHandle. removedFromLoadQueue() is called, which deletes the handle and clears that pointer. Do NOT delete the handle afterwards.

If handle->myObject is null, the model was destroyed before this call. DtModel::~DtModel cleared myObject but deliberately did not delete the handle, so the loader is the sole owner and must delete it here.

Main thread only.

virtual void makVrv::DtBackgroundLoader::timedFullLoad ( DtModel *  object)
protectedvirtual

Runs the entire load process (background and foreground) for the given model.

virtual void makVrv::DtBackgroundLoader::drainCompletedTBBTasks ( )
protectedvirtual

Drains the mutex-protected TBB result lists into their main-thread staging lists ready for processing this frame.

virtual void makVrv::DtBackgroundLoader::processFailedTBBTasks ( )
protectedvirtual

Cleans up handles from failed background tasks and removes their definition names from myActivelyLoadingModelDefs so future load attempts can be dispatched normally.

virtual void makVrv::DtBackgroundLoader::processForegroundLoadBudgeted ( )
protectedvirtual

Processes models that have finished background loading, calling foregroundLoad() on each. Stops early if the foreground time budget is exceeded, leaving remaining items for the next frame.

virtual void makVrv::DtBackgroundLoader::processForegroundFullLoadBudgeted ( )
protectedvirtual

Processes models that cannot use a background thread, calling foregroundLoad() and backgroundLoad() on each. Stops early if the foreground time budget is exceeded, leaving remaining items for the next frame.

virtual void makVrv::DtBackgroundLoader::deleteAllPendingHandles ( )
protectedvirtual

Deletes all pending handles across all four post-TBB lists without calling foregroundLoad(). Safe to call after DtModelDefinitionObjects have been torn down, specifically from cleanup().

virtual void makVrv::DtBackgroundLoader::dispatchBackgroundTasks ( int  maxThreadCount,
int  activeThreads 
)
protectedvirtual

Iterates myForegroundQueue and assigns background-capable models to new or existing DtBackgroundLoadAsyncTask instances, then enqueues those tasks into myTaskArena. New tasks are created only when spare thread capacity exists (maxThreadCount - activeThreads - myNumberOfActiveTasks > 2); additional models are batched onto an already-created task otherwise. Handles whose model was destroyed are disposed via disposeLoaderHandle() and removed from the queue. Called at the end of updateQueue(). Main thread only.

Friends And Related Function Documentation

friend class DtBackgroundLoadAsyncTask
friend

Member Data Documentation

DtDe& makVrv::DtBackgroundLoader::myDe
protected
boost::mutex makVrv::DtBackgroundLoader::myProducerQueueMutex
protected
std::list<DtLoaderHandle*> makVrv::DtBackgroundLoader::myProducerQueue
protected
std::list<DtLoaderHandle*> makVrv::DtBackgroundLoader::myForegroundQueue
protected
std::list<DtLoaderHandle*> makVrv::DtBackgroundLoader::myForegroundFullLoadQueue
protected
DtPendingIcoQueue makVrv::DtBackgroundLoader::myModelDefsToIco
protected
boost::mutex makVrv::DtBackgroundLoader::myTaskFinishedMutex
protected
std::list<DtLoaderHandle*> makVrv::DtBackgroundLoader::myObjectsToForegroundLoad
protected
std::list<DtLoaderHandle*> makVrv::DtBackgroundLoader::myFailedObjects
protected
std::list<DtLoaderHandle*> makVrv::DtBackgroundLoader::myObjectsFinishedWithTBBTask
protected
std::list<DtLoaderHandle*> makVrv::DtBackgroundLoader::myObjectsFailedInTBBTask
protected
std::map<std::string, bool> makVrv::DtBackgroundLoader::myActivelyLoadingModelDefs
protected
std::atomic<int> makVrv::DtBackgroundLoader::myNumberOfActiveTasks
protected
double makVrv::DtBackgroundLoader::myTimeSpentThisFrame
protected
boost::mutex makVrv::DtBackgroundLoader::myMutableLoadedDefMapMutex
mutableprotected
std::unordered_map<std::string, bool> makVrv::DtBackgroundLoader::myLoadedDefMap
protected
double makVrv::DtBackgroundLoader::myForegroundTimeBudget
protected
std::atomic<bool> makVrv::DtBackgroundLoader::myMultithreadedLoadingActive
protected
tbb::task_arena makVrv::DtBackgroundLoader::myTaskArena
protected
boost::mutex makVrv::DtBackgroundLoader::myTaskDrainMutex
protected
boost::condition_variable makVrv::DtBackgroundLoader::myTaskDrainCondition
protected
boost::mutex makVrv::DtBackgroundLoader::myPayloadStagingMutex
protected
std::vector<DtDefinitionCachePayload*> makVrv::DtBackgroundLoader::myStagedPayloads
protected

The documentation for this class was generated from the following file:

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)