VR-Forces 4.3.1 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
callbackQueue.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 
28 
30 class DT_DLL_vrfutil DtCallbackQueue : boost::noncopyable
31 {
32  struct ThreadInfo : public DtObjectDebugger<ThreadInfo>
33  {
34  typedef boost::shared_ptr<ThreadInfo> Ptr;
35  typedef std::list<Ptr> List;
36 
37  static Ptr Make(DtCallbackQueue&);
38 
39  bool quitFlag;
40  tbb::tbb_thread thread;
41 
42  protected:
43  explicit ThreadInfo(DtCallbackQueue&);
44  };
45 
46  friend struct ThreadInfo;
47 
48 public:
49  typedef boost::function<void ()> Callback;
50 
53  : public DtObjectDebugger<CallbackInterface>
54  {
55  public:
56  typedef boost::shared_ptr<CallbackInterface> Ptr;
57 
58  class Wrapper;
59  class RefWrapper;
60 
62  virtual ~CallbackInterface();
63 
65  virtual std::string label() const;
66 
68  virtual void run() = 0;
69 
71  virtual void cancel();
72  };
73 
76  explicit DtCallbackQueue(
77  const std::string& label,
78  unsigned maxTasks);
79 
81  virtual ~DtCallbackQueue();
82 
84  virtual std::string label() const;
85 
87  static void cleanup();
88 
90  virtual void setWarnTime(double seconds);
91 
93  virtual void printInfo(std::ostream&, size_t width=0) const;
94 
96  virtual size_t totalNumberOfCallbacks() const;
97 
99  virtual unsigned size() const;
100 
102  virtual size_t numExecuting() const;
103 
105  virtual unsigned maxTasks() const;
106 
109  virtual void setMaxTasks(unsigned tasks, bool wait=true);
110 
113  virtual void shutdown(bool wait = true);
114 
116  virtual void clear(bool wait = true);
117 
119  virtual void add_back(const Callback&, const std::string& label = "");
120 
122  virtual void add_front(const Callback&, const std::string& label = "");
123 
125  virtual void add_back(CallbackInterface*);
126 
128  virtual void add_front(CallbackInterface*);
129 
131  virtual void add_back(const CallbackInterface::Ptr&);
132 
134  virtual void add_front(const CallbackInterface::Ptr&);
135 
138  virtual void add_back(CallbackInterface&);
139 
142  virtual void add_front(CallbackInterface&);
143 
148  virtual bool runOne(bool block=false, ThreadInfo* info=0);
149 
153  virtual void runAll(bool useThisThread=false);
154 
155 private:
160  virtual bool pop(
161  CallbackInterface::Ptr&, bool block=true, ThreadInfo* info=0);
162 
164  void execute(ThreadInfo*);
165 
167  struct Record : public DtObjectDebugger<Record>
168  {
169  typedef std::list<Record> List;
170 
172  const tbb::tick_count timeAdded;
173 
174  explicit Record(CallbackInterface*);
175  explicit Record(const CallbackInterface::Ptr&);
176  };
177 
178  typedef tbb::mutex Mutex;
179  typedef tbb::interface5::condition_variable Condition;
180  typedef tbb::interface5::unique_lock<Mutex> Lock;
181 
182  mutable Mutex myMutex;
185 
188 
191 
192  tbb::atomic<size_t> myNumExecuting;
193 
195 
196  const std::string myLabel;
197 
198  double myWarnTime;
201 };
202 
205 {
206 public:
208  ~Wrapper() { delete myCallback; }
209  std::string label() const { return myCallback->label(); }
210  void run() { myCallback->run(); }
211  void cancel() { myCallback->cancel(); }
212 
213 protected:
215 };
216 
219 {
220 public:
222  std::string label() const { return myCallback.label(); }
223  void run() { myCallback.run(); }
224  void cancel() { myCallback.cancel(); }
225 
226 protected:
228 };
229 

Document ID: Generated on Wed Jul 29 00:55:00 EDT 2015 from SVN revision 155257
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)