VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
vrfCallbackQueue.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2020 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
9 
10 #pragma once
11 
12 #include "vrfutil/vrfutilDefines.h"
13 #include <vrfutil/objectCounter.h>
14 #include <atomic>
15 #include <boost/function.hpp>
16 #include <boost/noncopyable.hpp>
17 #include <tbb/tick_count.h>
18 #include <thread>
19 
20 #include <list>
21 #include <map>
22 #include <mutex>
23 #include <unordered_map>
24 #include <iosfwd>
25 
26 #include <condition_variable>
27 
29 
31 class DT_DLL_vrfutil DtVrfCallbackQueue : boost::noncopyable
32 {
33  struct ThreadInfo
34 #ifdef DtObjectDebugger_Enable
35  : public DtObjectDebugger<ThreadInfo>
36 #endif
37  {
38  typedef std::shared_ptr<ThreadInfo> Ptr;
39  typedef std::list<Ptr> List;
40 
41  static Ptr Make(DtVrfCallbackQueue&, unsigned threadnum);
42 
43  bool quitFlag;
44  std::string name;
45  std::thread thread;
46 
47  protected:
48  explicit ThreadInfo(DtVrfCallbackQueue&, const std::string& name);
49  };
50 
51  friend struct ThreadInfo;
52 
53 public:
54  typedef boost::function<void ()> Callback;
55 
58 #ifdef DtObjectDebugger_Enable
59  : public DtObjectDebugger<CallbackInterface>
60 #endif
61  {
62  public:
63  typedef std::shared_ptr<CallbackInterface> Ptr;
64 
65  class Wrapper;
66  class RefWrapper;
67 
69  virtual ~CallbackInterface();
70 
72  virtual std::string label() const;
73 
75  virtual void run() = 0;
76 
78  virtual void cancel();
79 
81  virtual bool valid() const;
82  };
83 
86  explicit DtVrfCallbackQueue(
87  const std::string& label,
88  unsigned maxTasks);
89 
91  virtual ~DtVrfCallbackQueue();
92 
94  virtual std::string label() const;
95 
97  static void cleanup();
98 
100  virtual void setWarnTime(double seconds);
101  virtual void setWarnTime(double seconds, std::ostream& output);
102 
104  virtual void setWarnQueueMinInterval(double seconds);
105 
107  virtual void setWarnLabelMinInterval(double seconds);
108 
110  virtual void printInfo(std::ostream&, size_t width=0) const;
111 
113  virtual size_t totalNumberOfCallbacks() const;
114 
116  virtual unsigned size() const;
117 
119  virtual size_t numExecuting() const;
120 
122  virtual unsigned maxTasks() const;
123 
126  virtual void setMaxTasks(unsigned tasks, bool wait=true);
127 
131  void shutdown(bool wait = true);
132 
134  virtual void clear(bool wait = true);
135 
137  virtual void add_back(const Callback&, const std::string& label = "");
138 
140  virtual void add_front(const Callback&, const std::string& label = "");
141 
144  virtual void add_back(CallbackInterface*);
145 
148  virtual void add_front(CallbackInterface*);
149 
151  virtual void add_back(const CallbackInterface::Ptr&);
152 
154  virtual void add_front(const CallbackInterface::Ptr&);
155 
158  virtual void add_back(CallbackInterface&);
159 
162  virtual void add_front(CallbackInterface&);
163 
168  virtual bool runOne(bool block=false, ThreadInfo* info=0);
169 
173  virtual void runAll(bool useThisThread=false);
174 
175 private:
180  virtual bool pop(
181  CallbackInterface::Ptr&, bool block=true, ThreadInfo* info=0);
182 
184  void execute(ThreadInfo*);
185 
187  struct Record
188 #ifdef DtObjectDebugger_Enable
189  : public DtObjectDebugger<Record>
190 #endif
191  {
192  typedef std::list<Record> List;
193 
195  const tbb::tick_count timeAdded;
196 
197  explicit Record(CallbackInterface*);
198  explicit Record(const CallbackInterface::Ptr&);
199  };
200 
201  typedef std::mutex Mutex;
202  typedef std::condition_variable Condition;
203  typedef std::unique_lock<Mutex> Lock;
204 
205  mutable Mutex myMutex;
208 
211 
214 
215  std::atomic<size_t> myNumExecuting;
216 
218 
219  std::string myLabel;
220 
221  typedef std::multimap<double, std::ostream*, std::greater<double> > StreamMap;
223 
224  mutable std::mutex myWarnThrottleMutex;
227  tbb::tick_count myLastQueueWarnTime;
228  std::unordered_map<std::string, tbb::tick_count> myLastWarnByLabel;
229  tbb::tick_count myLastWarnMapSweep;
230 
232  void sweepWarnMap(tbb::tick_count now);
233 };
234 
237 {
238 public:
240  : myCallback(i)
241  {
242  }
243  virtual ~Wrapper() override { delete myCallback; }
244  virtual std::string label() const override { return myCallback->label(); }
245  virtual void run() override { myCallback->run(); }
246  virtual void cancel() override { myCallback->cancel(); }
247 
248 protected:
250 };
251 
254 {
255 public:
257  : myCallback(i)
258  {
259  }
260  virtual std::string label() const override { return myCallback.label(); }
261  virtual void run() override { myCallback.run(); }
262  virtual void cancel() override { myCallback.cancel(); }
263 
264 protected:
266 };
267 
std::string name
Definition: vrfCallbackQueue.h:44
Definition: vrfCallbackQueue.h:235
Definition: vrfCallbackQueue.h:33
virtual void cancel() override
Will be called if the callback will not be run before it&#39;s discarded.
Definition: vrfCallbackQueue.h:246
std::mutex myWarnThrottleMutex
Definition: vrfCallbackQueue.h:224
Record::List myCallbacks
Definition: vrfCallbackQueue.h:210
ThreadInfo::List myThreads
Definition: vrfCallbackQueue.h:209
std::thread thread
Definition: vrfCallbackQueue.h:45
virtual void cancel() override
Will be called if the callback will not be run before it&#39;s discarded.
Definition: vrfCallbackQueue.h:262
DtVrfCallbackQueue::CallbackInterface & myCallback
Definition: vrfCallbackQueue.h:265
voidpf void uLong size
Definition: ioapi.h:39
const tbb::tick_count timeAdded
Definition: vrfCallbackQueue.h:195
Condition myCondition
Definition: vrfCallbackQueue.h:206
#define DT_DEFINE_OBJECT_DEBUGGER_NAME(type)
Definition: objectCounter.h:31
Default on in debug, off in release.
Definition: objectCounter.h:27
Simple asynchronous callback queue.
Definition: vrfCallbackQueue.h:31
virtual void run()=0
Run callback.
std::unique_lock< Mutex > Lock
Definition: vrfCallbackQueue.h:203
std::atomic< size_t > myNumExecuting
Definition: vrfCallbackQueue.h:215
This is a class which maintains a list of string command names and the callback functions that are to...
Definition: consoleCommandManager.h:55
Definition: vrfCallbackQueue.h:252
Callback record.
Definition: vrfCallbackQueue.h:187
boost::function< void()> Callback
Definition: vrfCallbackQueue.h:54
std::string myLabel
Definition: vrfCallbackQueue.h:219
std::list< Ptr > List
Definition: vrfCallbackQueue.h:39
std::shared_ptr< CallbackInterface > Ptr
Definition: vrfCallbackQueue.h:63
virtual std::string label() const override
Name of callback, used only for logging and debugging.
Definition: vrfCallbackQueue.h:244
virtual void cancel()
Will be called if the callback will not be run before it&#39;s discarded.
API for writing callbacks.
Definition: vrfCallbackQueue.h:57
RefWrapper(DtVrfCallbackQueue::CallbackInterface &i)
Definition: vrfCallbackQueue.h:256
tbb::tick_count myLastQueueWarnTime
Definition: vrfCallbackQueue.h:227
std::unordered_map< std::string, tbb::tick_count > myLastWarnByLabel
Definition: vrfCallbackQueue.h:228
size_t myTotalNumCallbacks
Definition: vrfCallbackQueue.h:213
Condition myDoneCondition
Definition: vrfCallbackQueue.h:207
double myWarnLabelMinInterval
Definition: vrfCallbackQueue.h:226
bool myShutdownFlag
Definition: vrfCallbackQueue.h:212
std::list< Record > List
Definition: vrfCallbackQueue.h:192
std::multimap< double, std::ostream *, std::greater< double > > StreamMap
Definition: vrfCallbackQueue.h:221
virtual void run() override
Run callback.
Definition: vrfCallbackQueue.h:245
DtVrfCallbackQueue::CallbackInterface * myCallback
Definition: vrfCallbackQueue.h:249
virtual ~Wrapper() override
Definition: vrfCallbackQueue.h:243
std::shared_ptr< ThreadInfo > Ptr
Definition: vrfCallbackQueue.h:38
const CallbackInterface::Ptr callback
Definition: vrfCallbackQueue.h:194
#define DT_DLL_vrfutil
This file is used to determine how to build Disable inconsistent dll linkage warning Visual Studio 6...
Definition: vrfutilDefines.h:34
virtual std::string label() const override
Name of callback, used only for logging and debugging.
Definition: vrfCallbackQueue.h:260
std::condition_variable Condition
Definition: vrfCallbackQueue.h:202
bool quitFlag
Definition: vrfCallbackQueue.h:43
virtual void run() override
Run callback.
Definition: vrfCallbackQueue.h:261
Wrapper(DtVrfCallbackQueue::CallbackInterface *i)
Definition: vrfCallbackQueue.h:239
virtual std::string label() const
Name of callback, used only for logging and debugging.
StreamMap myWarnTimes
Definition: vrfCallbackQueue.h:222
std::mutex Mutex
Definition: vrfCallbackQueue.h:201
tbb::tick_count myLastWarnMapSweep
Definition: vrfCallbackQueue.h:229
DtConsoleCommandManager * myConsoleManager
Definition: vrfCallbackQueue.h:217
double myWarnQueueMinInterval
Definition: vrfCallbackQueue.h:225
Mutex myMutex
Definition: vrfCallbackQueue.h:205

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)