VR-Forces 4.8 Class Documentation
 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) 2013 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 
15 #include <boost/function.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/noncopyable.hpp>
18 
19 #include <tbb/mutex.h>
20 #include <tbb/tbb_thread.h>
21 #include <tbb/compat/condition_variable>
22 #include <tbb/spin_mutex.h>
23 #include <tbb/atomic.h>
24 
25 #include <list>
26 #include <map>
27 #include <iosfwd>
28 
30 
32 class DT_DLL_vrfutil DtVrfCallbackQueue : boost::noncopyable
33 {
34  struct ThreadInfo : public DtObjectDebugger<ThreadInfo>
35  {
36  typedef boost::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  tbb::tbb_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  : public DtObjectDebugger<CallbackInterface>
57  {
58  public:
59  typedef boost::shared_ptr<CallbackInterface> Ptr;
60 
61  class Wrapper;
62  class RefWrapper;
63 
65  virtual ~CallbackInterface();
66 
68  virtual std::string label() const;
69 
71  virtual void run() = 0;
72 
74  virtual void cancel();
75 
77  virtual bool valid() const;
78  };
79 
82  explicit DtVrfCallbackQueue(
83  const std::string& label,
84  unsigned maxTasks);
85 
87  virtual ~DtVrfCallbackQueue();
88 
90  virtual std::string label() const;
91 
93  static void cleanup();
94 
96  virtual void setWarnTime(double seconds);
97  virtual void setWarnTime(double seconds, std::ostream& output);
98 
100  virtual void printInfo(std::ostream&, size_t width=0) const;
101 
103  virtual size_t totalNumberOfCallbacks() const;
104 
106  virtual unsigned size() const;
107 
109  virtual size_t numExecuting() const;
110 
112  virtual unsigned maxTasks() const;
113 
116  virtual void setMaxTasks(unsigned tasks, bool wait=true);
117 
121  void shutdown(bool wait = true);
122 
124  virtual void clear(bool wait = true);
125 
127  virtual void add_back(const Callback&, const std::string& label = "");
128 
130  virtual void add_front(const Callback&, const std::string& label = "");
131 
133  virtual void add_back(CallbackInterface*);
134 
136  virtual void add_front(CallbackInterface*);
137 
139  virtual void add_back(const CallbackInterface::Ptr&);
140 
142  virtual void add_front(const CallbackInterface::Ptr&);
143 
146  virtual void add_back(CallbackInterface&);
147 
150  virtual void add_front(CallbackInterface&);
151 
156  virtual bool runOne(bool block=false, ThreadInfo* info=0);
157 
161  virtual void runAll(bool useThisThread=false);
162 
163 private:
168  virtual bool pop(
169  CallbackInterface::Ptr&, bool block=true, ThreadInfo* info=0);
170 
172  void execute(ThreadInfo*);
173 
175  struct Record : public DtObjectDebugger<Record>
176  {
177  typedef std::list<Record> List;
178 
180  const tbb::tick_count timeAdded;
181 
182  explicit Record(CallbackInterface*);
183  explicit Record(const CallbackInterface::Ptr&);
184  };
185 
186  typedef tbb::mutex Mutex;
187  typedef tbb::interface5::condition_variable Condition;
188  typedef tbb::interface5::unique_lock<Mutex> Lock;
189 
190  mutable Mutex myMutex;
193 
196 
199 
200  tbb::atomic<size_t> myNumExecuting;
201 
203 
204  std::string myLabel;
205 
206  typedef std::multimap<double, std::ostream*, std::greater<double> > StreamMap;
208 };
209 
212 {
213 public:
215  ~Wrapper() { delete myCallback; }
216  std::string label() const { return myCallback->label(); }
217  void run() { myCallback->run(); }
218  void cancel() { myCallback->cancel(); }
219 
220 protected:
222 };
223 
226 {
227 public:
229  std::string label() const { return myCallback.label(); }
230  void run() { myCallback.run(); }
231  void cancel() { myCallback.cancel(); }
232 
233 protected:
235 };
236 
std::string name
Definition: vrfCallbackQueue.h:42
Definition: vrfCallbackQueue.h:210
Definition: vrfCallbackQueue.h:34
~Wrapper()
Definition: vrfCallbackQueue.h:215
void run()
Run callback.
Definition: vrfCallbackQueue.h:217
tbb::mutex Mutex
Definition: vrfCallbackQueue.h:186
Record::List myCallbacks
Definition: vrfCallbackQueue.h:195
ThreadInfo::List myThreads
Definition: vrfCallbackQueue.h:194
DtVrfCallbackQueue::CallbackInterface & myCallback
Definition: vrfCallbackQueue.h:234
voidpf void uLong size
Definition: ioapi.h:39
const tbb::tick_count timeAdded
Definition: vrfCallbackQueue.h:180
Condition myCondition
Definition: vrfCallbackQueue.h:191
#define DT_DEFINE_OBJECT_DEBUGGER_NAME(type)
Definition: objectCounter.h:265
Default on in debug, off in release.
Definition: objectCounter.h:31
tbb::atomic< size_t > myNumExecuting
Definition: vrfCallbackQueue.h:200
Simple asynchronous callback queue.
Definition: vrfCallbackQueue.h:32
virtual void run()=0
Run callback.
This is a class which maintains a list of string command names and the callback functions that are to...
Definition: consoleCommandManager.h:48
Definition: vrfCallbackQueue.h:224
Callback record.
Definition: vrfCallbackQueue.h:175
boost::function< void()> Callback
Definition: vrfCallbackQueue.h:52
std::string myLabel
Definition: vrfCallbackQueue.h:204
std::list< Ptr > List
Definition: vrfCallbackQueue.h:37
tbb::interface5::condition_variable Condition
Definition: vrfCallbackQueue.h:187
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
void run()
Run callback.
Definition: vrfCallbackQueue.h:230
RefWrapper(DtVrfCallbackQueue::CallbackInterface &i)
Definition: vrfCallbackQueue.h:228
void cancel()
Will be called if the callback will not be run before it&#39;s discarded.
Definition: vrfCallbackQueue.h:218
size_t myTotalNumCallbacks
Definition: vrfCallbackQueue.h:198
Condition myDoneCondition
Definition: vrfCallbackQueue.h:192
bool myShutdownFlag
Definition: vrfCallbackQueue.h:197
std::list< Record > List
Definition: vrfCallbackQueue.h:177
std::multimap< double, std::ostream *, std::greater< double > > StreamMap
Definition: vrfCallbackQueue.h:206
DtVrfCallbackQueue::CallbackInterface * myCallback
Definition: vrfCallbackQueue.h:221
tbb::tbb_thread thread
Definition: vrfCallbackQueue.h:43
std::string label() const
Name of callback, used only for logging and debugging.
Definition: vrfCallbackQueue.h:216
const CallbackInterface::Ptr callback
Definition: vrfCallbackQueue.h:179
std::string label() const
Name of callback, used only for logging and debugging.
Definition: vrfCallbackQueue.h:229
void cancel()
Will be called if the callback will not be run before it&#39;s discarded.
Definition: vrfCallbackQueue.h:231
#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
bool quitFlag
Definition: vrfCallbackQueue.h:41
tbb::interface5::unique_lock< Mutex > Lock
Definition: vrfCallbackQueue.h:188
boost::shared_ptr< ThreadInfo > Ptr
Definition: vrfCallbackQueue.h:36
Wrapper(DtVrfCallbackQueue::CallbackInterface *i)
Definition: vrfCallbackQueue.h:214
virtual std::string label() const
Name of callback, used only for logging and debugging.
StreamMap myWarnTimes
Definition: vrfCallbackQueue.h:207
DtConsoleCommandManager * myConsoleManager
Definition: vrfCallbackQueue.h:202
Mutex myMutex
Definition: vrfCallbackQueue.h:190
boost::shared_ptr< CallbackInterface > Ptr
Definition: vrfCallbackQueue.h:59

Document ID: Generated on Thu Aug 27 10:56:05 EDT 2020 from SVN revision 217100
Copyright © 2005-2020 MAK Technologies. All Rights Reserved (www.mak.com)