VR-Forces 5.0.1 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 
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
35 #ifdef DtObjectDebugger_Enable
36  : public DtObjectDebugger<ThreadInfo>
37 #endif
38  {
39  typedef boost::shared_ptr<ThreadInfo> Ptr;
40  typedef std::list<Ptr> List;
41 
42  static Ptr Make(DtVrfCallbackQueue&, unsigned threadnum);
43 
44  bool quitFlag;
45  std::string name;
46  tbb::tbb_thread thread;
47 
48  protected:
49  explicit ThreadInfo(DtVrfCallbackQueue&, const std::string& name);
50  };
51 
52  friend struct ThreadInfo;
53 
54 public:
55  typedef boost::function<void ()> Callback;
56 
59 #ifdef DtObjectDebugger_Enable
60  : public DtObjectDebugger<CallbackInterface>
61 #endif
62  {
63  public:
64  typedef boost::shared_ptr<CallbackInterface> Ptr;
65 
66  class Wrapper;
67  class RefWrapper;
68 
70  virtual ~CallbackInterface();
71 
73  virtual std::string label() const;
74 
76  virtual void run() = 0;
77 
79  virtual void cancel();
80 
82  virtual bool valid() const;
83  };
84 
87  explicit DtVrfCallbackQueue(
88  const std::string& label,
89  unsigned maxTasks);
90 
92  virtual ~DtVrfCallbackQueue();
93 
95  virtual std::string label() const;
96 
98  static void cleanup();
99 
101  virtual void setWarnTime(double seconds);
102  virtual void setWarnTime(double seconds, std::ostream& output);
103 
105  virtual void printInfo(std::ostream&, size_t width=0) const;
106 
108  virtual size_t totalNumberOfCallbacks() const;
109 
111  virtual unsigned size() const;
112 
114  virtual size_t numExecuting() const;
115 
117  virtual unsigned maxTasks() const;
118 
121  virtual void setMaxTasks(unsigned tasks, bool wait=true);
122 
126  void shutdown(bool wait = true);
127 
129  virtual void clear(bool wait = true);
130 
132  virtual void add_back(const Callback&, const std::string& label = "");
133 
135  virtual void add_front(const Callback&, const std::string& label = "");
136 
138  virtual void add_back(CallbackInterface*);
139 
141  virtual void add_front(CallbackInterface*);
142 
144  virtual void add_back(const CallbackInterface::Ptr&);
145 
147  virtual void add_front(const CallbackInterface::Ptr&);
148 
151  virtual void add_back(CallbackInterface&);
152 
155  virtual void add_front(CallbackInterface&);
156 
161  virtual bool runOne(bool block=false, ThreadInfo* info=0);
162 
166  virtual void runAll(bool useThisThread=false);
167 
168 private:
173  virtual bool pop(
174  CallbackInterface::Ptr&, bool block=true, ThreadInfo* info=0);
175 
177  void execute(ThreadInfo*);
178 
180  struct Record
181 #ifdef DtObjectDebugger_Enable
182  : public DtObjectDebugger<Record>
183 #endif
184  {
185  typedef std::list<Record> List;
186 
188  const tbb::tick_count timeAdded;
189 
190  explicit Record(CallbackInterface*);
191  explicit Record(const CallbackInterface::Ptr&);
192  };
193 
194  typedef tbb::mutex Mutex;
195  typedef tbb::interface5::condition_variable Condition;
196  typedef tbb::interface5::unique_lock<Mutex> Lock;
197 
198  mutable Mutex myMutex;
201 
204 
207 
208  tbb::atomic<size_t> myNumExecuting;
209 
211 
212  std::string myLabel;
213 
214  typedef std::multimap<double, std::ostream*, std::greater<double> > StreamMap;
216 };
217 
220 {
221 public:
223  ~Wrapper() { delete myCallback; }
224  std::string label() const { return myCallback->label(); }
225  void run() { myCallback->run(); }
226  void cancel() { myCallback->cancel(); }
227 
228 protected:
230 };
231 
234 {
235 public:
237  std::string label() const { return myCallback.label(); }
238  void run() { myCallback.run(); }
239  void cancel() { myCallback.cancel(); }
240 
241 protected:
243 };
244 
std::string name
Definition: vrfCallbackQueue.h:45
Definition: vrfCallbackQueue.h:218
Definition: vrfCallbackQueue.h:34
~Wrapper()
Definition: vrfCallbackQueue.h:223
void run()
Run callback.
Definition: vrfCallbackQueue.h:225
tbb::mutex Mutex
Definition: vrfCallbackQueue.h:194
Record::List myCallbacks
Definition: vrfCallbackQueue.h:203
ThreadInfo::List myThreads
Definition: vrfCallbackQueue.h:202
DtVrfCallbackQueue::CallbackInterface & myCallback
Definition: vrfCallbackQueue.h:242
voidpf void uLong size
Definition: ioapi.h:39
const tbb::tick_count timeAdded
Definition: vrfCallbackQueue.h:188
Condition myCondition
Definition: vrfCallbackQueue.h:199
#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:208
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:56
Definition: vrfCallbackQueue.h:232
Callback record.
Definition: vrfCallbackQueue.h:180
boost::function< void()> Callback
Definition: vrfCallbackQueue.h:55
std::string myLabel
Definition: vrfCallbackQueue.h:212
std::list< Ptr > List
Definition: vrfCallbackQueue.h:40
tbb::interface5::condition_variable Condition
Definition: vrfCallbackQueue.h:195
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:58
void run()
Run callback.
Definition: vrfCallbackQueue.h:238
RefWrapper(DtVrfCallbackQueue::CallbackInterface &i)
Definition: vrfCallbackQueue.h:236
void cancel()
Will be called if the callback will not be run before it&#39;s discarded.
Definition: vrfCallbackQueue.h:226
size_t myTotalNumCallbacks
Definition: vrfCallbackQueue.h:206
Condition myDoneCondition
Definition: vrfCallbackQueue.h:200
bool myShutdownFlag
Definition: vrfCallbackQueue.h:205
std::list< Record > List
Definition: vrfCallbackQueue.h:185
std::multimap< double, std::ostream *, std::greater< double > > StreamMap
Definition: vrfCallbackQueue.h:214
DtVrfCallbackQueue::CallbackInterface * myCallback
Definition: vrfCallbackQueue.h:229
tbb::tbb_thread thread
Definition: vrfCallbackQueue.h:46
std::string label() const
Name of callback, used only for logging and debugging.
Definition: vrfCallbackQueue.h:224
const CallbackInterface::Ptr callback
Definition: vrfCallbackQueue.h:187
std::string label() const
Name of callback, used only for logging and debugging.
Definition: vrfCallbackQueue.h:237
void cancel()
Will be called if the callback will not be run before it&#39;s discarded.
Definition: vrfCallbackQueue.h:239
#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:44
tbb::interface5::unique_lock< Mutex > Lock
Definition: vrfCallbackQueue.h:196
boost::shared_ptr< ThreadInfo > Ptr
Definition: vrfCallbackQueue.h:39
Wrapper(DtVrfCallbackQueue::CallbackInterface *i)
Definition: vrfCallbackQueue.h:222
virtual std::string label() const
Name of callback, used only for logging and debugging.
StreamMap myWarnTimes
Definition: vrfCallbackQueue.h:215
DtConsoleCommandManager * myConsoleManager
Definition: vrfCallbackQueue.h:210
Mutex myMutex
Definition: vrfCallbackQueue.h:198
boost::shared_ptr< CallbackInterface > Ptr
Definition: vrfCallbackQueue.h:64

Document ID: Generated on Mon Jun 20 00:38:30 EDT 2022 from SVN revision 244029
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)