VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtBackgroundLoader.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * Copyright (c) 2026 MAK Technologies, Inc.
3 * All rights reserved.
4 *****************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <vrvCore/vrvCore.h>
14 
16 
17 #include <tbb/task_arena.h>
18 
19 #include <boost/thread.hpp>
20 
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 
25 
26 namespace makVrv
27 {
28  class DtDe;
29  class DtModel;
30  class DtBackgroundLoader;
31  struct DtLoaderHandle;
32 
82  {
83  public:
84 
88  {
89  int myActiveTasks; // background TBB tasks currently in flight
90  int myProducerQueueDepth; // models queued, not yet moved to foreground queue
91  int myForegroundQueueDepth; // models waiting for foreground (ICO) processing
92  int myForegroundFullLoadQueueDepth; // objects waiting for full foreground-only load
93  int myActivelyLoadingModelDefsCount; // model definitions currently being loaded by a task
94  int myDefsToIcoCount; // definitions awaiting ICO upload
95  double myForegroundTimeSec; // foreground time consumed this frame (seconds)
96  };
97 
99  static DtBackgroundLoader& instance(DtDe& de);
100 
102  virtual ~DtBackgroundLoader();
103  protected:
106  private:
108  DtBackgroundLoader() = delete;
110  DtBackgroundLoader(const DtBackgroundLoader&) = delete;
112  DtBackgroundLoader& operator=(const DtBackgroundLoader&) = delete;
113  public:
114 
118  virtual void cleanup();
119 
122  virtual void updateQueue(int max_thread_count, int active_threads);
123 
125  virtual DtLoaderHandle* queueForLoad(DtModel* object);
126 
131  virtual void setForegroundTimeBudget(double budget);
132 
134  virtual double foregroundTimeBudget() const;
135 
137  virtual void incrementForegroundTime(double time_elapse);
138 
140  virtual bool isForegroundTimeBudgetExpired() const;
141 
143  virtual double timeSpentThisFrame() const;
145 
151  virtual void loadIfPossibleOrAddToQueue(DtModel* object);
152 
159  virtual void executeBackgroundLoad(DtModel* object);
160 
162  virtual bool hasActiveAsyncTasks() const;
163 
165  virtual int numModelDefinitionsToIco() const;
166 
168  virtual std::string nextModelDefinitionToIco();
169 
171  virtual int numOfActiveTasks() const;
172 
174  virtual bool multithreadedLoadingActive() const;
175 
177  virtual void setMultithreadedLoadingActive(bool val);
178 
184  virtual DtBackgroundLoadStats queryStats();
185 
191  virtual void waitForActiveTasksAndCommit();
192 
193  protected:
194 
195  virtual void ts_setModelDefinitionLoaded(const std::string& modelDefinition);
196  virtual bool ts_isModelDefinitionLoaded(const std::string& modelDefinition) const;
197 
202  virtual void ts_stagePayload(DtDefinitionCachePayload* payload);
203 
208  virtual void commitStagedPayloads();
209 
222  virtual void disposeLoaderHandle(DtLoaderHandle* handle);
223 
225  virtual void timedFullLoad(DtModel* object);
226 
229  virtual void drainCompletedTBBTasks();
230 
234  virtual void processFailedTBBTasks();
235 
239  virtual void processForegroundLoadBudgeted();
240 
244  virtual void processForegroundFullLoadBudgeted();
245 
249  virtual void deleteAllPendingHandles();
250 
259  virtual void dispatchBackgroundTasks(int maxThreadCount, int activeThreads);
260 
261  protected:
262 
264 
266 
267  // myProducerQueueMutex protects myProducerQueue, which is written by any
268  // thread calling queueForLoad() or loadIfPossibleOrAddToQueue() and drained
269  // on the main thread at the top of updateQueue(). Held only for push/pop.
270  boost::mutex myProducerQueueMutex;
271  std::list<DtLoaderHandle*> myProducerQueue;
272  std::list<DtLoaderHandle*> myForegroundQueue;
273  std::list<DtLoaderHandle*> myForegroundFullLoadQueue;
274 
277  {
278  std::list<std::string> myQueue; // ordered -- one entry per unique definition
279  std::unordered_set<std::string> myQueued; // fast membership test to prevent duplicates
280  };
281 
282  // myModelDefsToIco is main-thread only -- no mutex required.
284 
285  // myTaskFinishedMutex protects myObjectsToForegroundLoad and myFailedObjects,
286  // which are written by TBB tasks on completion and read by the main thread in
287  // drainCompletedTBBTasks(). Held only for push/splice operations.
288  boost::mutex myTaskFinishedMutex;
289  std::list<DtLoaderHandle*> myObjectsToForegroundLoad;
290  std::list<DtLoaderHandle*> myFailedObjects;
291 
292  // Main-thread only -- populated from myObjectsToForegroundLoad and
293  // myFailedObjects by drainCompletedTBBTasks(); no mutex required.
294  std::list<DtLoaderHandle*> myObjectsFinishedWithTBBTask;
295  std::list<DtLoaderHandle*> myObjectsFailedInTBBTask;
296 
297  std::map<std::string, bool> myActivelyLoadingModelDefs;
298  std::atomic<int> myNumberOfActiveTasks;
299 
301 
302  // myMutableLoadedDefMapMutex protects myLoadedDefMap, which is written by
303  // TBB tasks via ts_setModelDefinitionLoaded() and read by any thread via
304  // ts_isModelDefinitionLoaded(). Marked mutable so the const reader can lock.
305  mutable boost::mutex myMutableLoadedDefMapMutex;
306  std::unordered_map<std::string, bool> myLoadedDefMap;
307 
309  std::atomic<bool> myMultithreadedLoadingActive;
310 
311  tbb::task_arena myTaskArena;
312 
313  // myTaskDrainMutex + myTaskDrainCondition implement a barrier that allows
314  // the main thread to block in waitForActiveTasksAndCommit() or cleanup()
315  // until myNumberOfActiveTasks reaches zero. TBB tasks notify the condition
316  // variable after decrementing the active count.
317  boost::mutex myTaskDrainMutex;
318  boost::condition_variable myTaskDrainCondition;
319 
320  // myPayloadStagingMutex protects myStagedPayloads, which is written by
321  // background TBB tasks via ts_stagePayload() and drained on the main
322  // thread by commitStagedPayloads(). The mutex is held only for the
323  // duration of a push_back or a swap -- never across I/O or heavy work.
324  boost::mutex myPayloadStagingMutex;
325  std::vector<DtDefinitionCachePayload*> myStagedPayloads;
326  };
327 
328 } // namespace makVrv
std::unordered_set< std::string > myQueued
Definition: DtBackgroundLoader.h:279
std::unordered_map< std::string, bool > myLoadedDefMap
Definition: DtBackgroundLoader.h:306
Virtual base class. Allows for RTTI and proper virtual destruction. Used by Creators, Factories, Providers, Assemblers and User Interfaces.
Definition: DtVirtualBaseClass.h:19
std::list< DtLoaderHandle * > myObjectsToForegroundLoad
Definition: DtBackgroundLoader.h:289
int myDefsToIcoCount
Definition: DtBackgroundLoader.h:94
std::list< DtLoaderHandle * > myForegroundFullLoadQueue
Definition: DtBackgroundLoader.h:273
double myForegroundTimeSec
Definition: DtBackgroundLoader.h:95
std::list< DtLoaderHandle * > myForegroundQueue
Definition: DtBackgroundLoader.h:272
std::map< std::string, bool > myActivelyLoadingModelDefs
Definition: DtBackgroundLoader.h:297
std::vector< DtDefinitionCachePayload * > myStagedPayloads
Definition: DtBackgroundLoader.h:325
std::list< DtLoaderHandle * > myObjectsFinishedWithTBBTask
Definition: DtBackgroundLoader.h:294
tbb::task_arena myTaskArena
Definition: DtBackgroundLoader.h:311
std::atomic< bool > myMultithreadedLoadingActive
Definition: DtBackgroundLoader.h:309
async task that loads a DtModel in the background
Definition: DtBackgroundLoadAsyncTask.h:24
int myProducerQueueDepth
Definition: DtBackgroundLoader.h:90
Manages asynchronous and budgeted foreground loading for all DtModel subclasses.
Definition: DtBackgroundLoader.h:81
boost::mutex myMutableLoadedDefMapMutex
Definition: DtBackgroundLoader.h:305
std::list< DtLoaderHandle * > myProducerQueue
Definition: DtBackgroundLoader.h:271
std::list< DtLoaderHandle * > myObjectsFailedInTBBTask
Definition: DtBackgroundLoader.h:295
DtDe & myDe
Definition: DtBackgroundLoader.h:263
double myForegroundTimeBudget
Definition: DtBackgroundLoader.h:308
boost::mutex myPayloadStagingMutex
Definition: DtBackgroundLoader.h:324
Contains makVrv::DtVirtualBaseClass class.
int myForegroundQueueDepth
Definition: DtBackgroundLoader.h:91
boost::mutex myTaskDrainMutex
Definition: DtBackgroundLoader.h:317
boost::mutex myTaskFinishedMutex
Definition: DtBackgroundLoader.h:288
Wrapper object that insulates the loader from other threads destroying an object. The DtModelLoadPara...
Definition: DtLoaderHandle.h:28
#define DT_DLL_VRVCORE
Definition: vrvCore.h:20
Groups the queue and de-duplication set for pending ICO submissions.
Definition: DtBackgroundLoader.h:276
boost::condition_variable myTaskDrainCondition
Definition: DtBackgroundLoader.h:318
Contains makVrv::DtDefinitionCachePayload class.
int myForegroundFullLoadQueueDepth
Definition: DtBackgroundLoader.h:92
int myActivelyLoadingModelDefsCount
Definition: DtBackgroundLoader.h:93
DtPendingIcoQueue myModelDefsToIco
Definition: DtBackgroundLoader.h:283
A model which owns a single model instance. A DtModel is a managing container for a single DtModelIns...
Definition: DtModel.h:57
std::list< DtLoaderHandle * > myFailedObjects
Definition: DtBackgroundLoader.h:290
Snapshot of DtBackgroundLoader state for GUI / diagnostic display. This is used by queryStats()...
Definition: DtBackgroundLoader.h:87
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer, DtDeCommunicator, as well as a DtDisplay and other things.
Definition: DtDe.h:76
std::atomic< int > myNumberOfActiveTasks
Definition: DtBackgroundLoader.h:298
boost::mutex myProducerQueueMutex
Definition: DtBackgroundLoader.h:270
double myTimeSpentThisFrame
Definition: DtBackgroundLoader.h:300
int myActiveTasks
Definition: DtBackgroundLoader.h:89
Accumulates definition cache entries produced by a background load.
Definition: DtDefinitionCachePayload.h:37
std::list< std::string > myQueue
Definition: DtBackgroundLoader.h:278
Contains DLL export macros.

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)