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 <iosfwd>
23 
24 #include <condition_variable>
25 
27 
29 class DT_DLL_vrfutil DtVrfCallbackQueue : boost::noncopyable
30 {
31  struct ThreadInfo
32 #ifdef DtObjectDebugger_Enable
33  : public DtObjectDebugger<ThreadInfo>
34 #endif
35  {
36  typedef std::shared_ptr<ThreadInfo> Ptr;
37  typedef std::list<Ptr> List;
38 
39  static Ptr Make(DtVrfCallbackQueue&, unsigned threadnum);
40 
41  bool quitFlag;
42  std::string name;
43  std::thread thread;
44 
45  protected:
46  explicit ThreadInfo(DtVrfCallbackQueue&, const std::string& name);
47  };
48 
49  friend struct ThreadInfo;
50 
51 public:
52  typedef boost::function<void ()> Callback;
53 
56 #ifdef DtObjectDebugger_Enable
57  : public DtObjectDebugger<CallbackInterface>
58 #endif
59  {
60  public:
61  typedef std::shared_ptr<CallbackInterface> Ptr;
62 
63  class Wrapper;
64  class RefWrapper;
65 
67  virtual ~CallbackInterface();
68 
70  virtual std::string label() const;
71 
73  virtual void run() = 0;
74 
76  virtual void cancel();
77 
79  virtual bool valid() const;
80  };
81 
84  explicit DtVrfCallbackQueue(
85  const std::string& label,
86  unsigned maxTasks);
87 
89  virtual ~DtVrfCallbackQueue();
90 
92  virtual std::string label() const;
93 
95  static void cleanup();
96 
98  virtual void setWarnTime(double seconds);
99  virtual void setWarnTime(double seconds, std::ostream& output);
100 
102  virtual void printInfo(std::ostream&, size_t width=0) const;
103 
105  virtual size_t totalNumberOfCallbacks() const;
106 
108  virtual unsigned size() const;
109 
111  virtual size_t numExecuting() const;
112 
114  virtual unsigned maxTasks() const;
115 
118  virtual void setMaxTasks(unsigned tasks, bool wait=true);
119 
123  void shutdown(bool wait = true);
124 
126  virtual void clear(bool wait = true);
127 
129  virtual void add_back(const Callback&, const std::string& label = "");
130 
132  virtual void add_front(const Callback&, const std::string& label = "");
133 
136  virtual void add_back(CallbackInterface*);
137 
140  virtual void add_front(CallbackInterface*);
141 
143  virtual void add_back(const CallbackInterface::Ptr&);
144 
146  virtual void add_front(const CallbackInterface::Ptr&);
147 
150  virtual void add_back(CallbackInterface&);
151 
154  virtual void add_front(CallbackInterface&);
155 
160  virtual bool runOne(bool block=false, ThreadInfo* info=0);
161 
165  virtual void runAll(bool useThisThread=false);
166 
167 private:
172  virtual bool pop(
173  CallbackInterface::Ptr&, bool block=true, ThreadInfo* info=0);
174 
176  void execute(ThreadInfo*);
177 
179  struct Record
180 #ifdef DtObjectDebugger_Enable
181  : public DtObjectDebugger<Record>
182 #endif
183  {
184  typedef std::list<Record> List;
185 
187  const tbb::tick_count timeAdded;
188 
189  explicit Record(CallbackInterface*);
190  explicit Record(const CallbackInterface::Ptr&);
191  };
192 
193  typedef std::mutex Mutex;
194  typedef std::condition_variable Condition;
195  typedef std::unique_lock<Mutex> Lock;
196 
197  mutable Mutex myMutex;
200 
203 
206 
207  std::atomic<size_t> myNumExecuting;
208 
210 
211  std::string myLabel;
212 
213  typedef std::multimap<double, std::ostream*, std::greater<double> > StreamMap;
215 };
216 
219 {
220 public:
222  : myCallback(i)
223  {
224  }
225  virtual ~Wrapper() override { delete myCallback; }
226  virtual std::string label() const override { return myCallback->label(); }
227  virtual void run() override { myCallback->run(); }
228  virtual void cancel() override { myCallback->cancel(); }
229 
230 protected:
232 };
233 
236 {
237 public:
239  : myCallback(i)
240  {
241  }
242  virtual std::string label() const override { return myCallback.label(); }
243  virtual void run() override { myCallback.run(); }
244  virtual void cancel() override { myCallback.cancel(); }
245 
246 protected:
248 };
249 
std::string name
Definition: vrfCallbackQueue.h:42
Definition: vrfCallbackQueue.h:217
Definition: vrfCallbackQueue.h:31
virtual void cancel() override
Will be called if the callback will not be run before it&#39;s discarded.
Definition: vrfCallbackQueue.h:228
Record::List myCallbacks
Definition: vrfCallbackQueue.h:202
ThreadInfo::List myThreads
Definition: vrfCallbackQueue.h:201
std::thread thread
Definition: vrfCallbackQueue.h:43
virtual void cancel() override
Will be called if the callback will not be run before it&#39;s discarded.
Definition: vrfCallbackQueue.h:244
DtVrfCallbackQueue::CallbackInterface & myCallback
Definition: vrfCallbackQueue.h:247
voidpf void uLong size
Definition: ioapi.h:39
const tbb::tick_count timeAdded
Definition: vrfCallbackQueue.h:187
Condition myCondition
Definition: vrfCallbackQueue.h:198
#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:29
virtual void run()=0
Run callback.
std::unique_lock< Mutex > Lock
Definition: vrfCallbackQueue.h:195
std::atomic< size_t > myNumExecuting
Definition: vrfCallbackQueue.h:207
This is a class which maintains a list of string command names and the callback functions that are to...
Definition: consoleCommandManager.h:56
Definition: vrfCallbackQueue.h:234
Callback record.
Definition: vrfCallbackQueue.h:179
boost::function< void()> Callback
Definition: vrfCallbackQueue.h:52
std::string myLabel
Definition: vrfCallbackQueue.h:211
std::list< Ptr > List
Definition: vrfCallbackQueue.h:37
std::shared_ptr< CallbackInterface > Ptr
Definition: vrfCallbackQueue.h:61
virtual std::string label() const override
Name of callback, used only for logging and debugging.
Definition: vrfCallbackQueue.h:226
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:55
RefWrapper(DtVrfCallbackQueue::CallbackInterface &i)
Definition: vrfCallbackQueue.h:238
size_t myTotalNumCallbacks
Definition: vrfCallbackQueue.h:205
Condition myDoneCondition
Definition: vrfCallbackQueue.h:199
bool myShutdownFlag
Definition: vrfCallbackQueue.h:204
std::list< Record > List
Definition: vrfCallbackQueue.h:184
std::multimap< double, std::ostream *, std::greater< double > > StreamMap
Definition: vrfCallbackQueue.h:213
virtual void run() override
Run callback.
Definition: vrfCallbackQueue.h:227
DtVrfCallbackQueue::CallbackInterface * myCallback
Definition: vrfCallbackQueue.h:231
virtual ~Wrapper() override
Definition: vrfCallbackQueue.h:225
std::shared_ptr< ThreadInfo > Ptr
Definition: vrfCallbackQueue.h:36
const CallbackInterface::Ptr callback
Definition: vrfCallbackQueue.h:186
#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:242
std::condition_variable Condition
Definition: vrfCallbackQueue.h:194
bool quitFlag
Definition: vrfCallbackQueue.h:41
virtual void run() override
Run callback.
Definition: vrfCallbackQueue.h:243
Wrapper(DtVrfCallbackQueue::CallbackInterface *i)
Definition: vrfCallbackQueue.h:221
virtual std::string label() const
Name of callback, used only for logging and debugging.
StreamMap myWarnTimes
Definition: vrfCallbackQueue.h:214
std::mutex Mutex
Definition: vrfCallbackQueue.h:193
DtConsoleCommandManager * myConsoleManager
Definition: vrfCallbackQueue.h:209
Mutex myMutex
Definition: vrfCallbackQueue.h:197

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)