VR-Forces Development_Version 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 #include <map>
27 #include <iosfwd>
28 
30 
32 class DT_DLL_vrfutil DtCallbackQueue : 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(DtCallbackQueue&);
40 
41  bool quitFlag;
42  tbb::tbb_thread thread;
43 
44  protected:
45  explicit ThreadInfo(DtCallbackQueue&);
46  };
47 
48  friend struct ThreadInfo;
49 
50 public:
51  typedef boost::function<void ()> Callback;
52 
55  : public DtObjectDebugger<CallbackInterface>
56  {
57  public:
58  typedef boost::shared_ptr<CallbackInterface> Ptr;
59 
60  class Wrapper;
61  class RefWrapper;
62 
64  virtual ~CallbackInterface();
65 
67  virtual std::string label() const;
68 
70  virtual void run() = 0;
71 
73  virtual void cancel();
74 
76  virtual bool valid() const;
77  };
78 
81  explicit DtCallbackQueue(
82  const std::string& label,
83  unsigned maxTasks);
84 
86  virtual ~DtCallbackQueue();
87 
89  virtual std::string label() const;
90 
92  static void cleanup();
93 
95  virtual void setWarnTime(double seconds);
96  virtual void setWarnTime(double seconds, std::ostream& output);
97 
99  virtual void printInfo(std::ostream&, size_t width=0) const;
100 
102  virtual size_t totalNumberOfCallbacks() const;
103 
105  virtual unsigned size() const;
106 
108  virtual size_t numExecuting() const;
109 
111  virtual unsigned maxTasks() const;
112 
115  virtual void setMaxTasks(unsigned tasks, bool wait=true);
116 
120  void shutdown(bool wait = true);
121 
123  virtual void clear(bool wait = true);
124 
126  virtual void add_back(const Callback&, const std::string& label = "");
127 
129  virtual void add_front(const Callback&, const std::string& label = "");
130 
132  virtual void add_back(CallbackInterface*);
133 
135  virtual void add_front(CallbackInterface*);
136 
138  virtual void add_back(const CallbackInterface::Ptr&);
139 
141  virtual void add_front(const CallbackInterface::Ptr&);
142 
145  virtual void add_back(CallbackInterface&);
146 
149  virtual void add_front(CallbackInterface&);
150 
155  virtual bool runOne(bool block=false, ThreadInfo* info=0);
156 
160  virtual void runAll(bool useThisThread=false);
161 
162 private:
167  virtual bool pop(
168  CallbackInterface::Ptr&, bool block=true, ThreadInfo* info=0);
169 
171  void execute(ThreadInfo*);
172 
174  struct Record : public DtObjectDebugger<Record>
175  {
176  typedef std::list<Record> List;
177 
179  const tbb::tick_count timeAdded;
180 
181  explicit Record(CallbackInterface*);
182  explicit Record(const CallbackInterface::Ptr&);
183  };
184 
185  typedef tbb::mutex Mutex;
186  typedef tbb::interface5::condition_variable Condition;
187  typedef tbb::interface5::unique_lock<Mutex> Lock;
188 
189  mutable Mutex myMutex;
192 
195 
198 
199  tbb::atomic<size_t> myNumExecuting;
200 
202 
203  std::string myLabel;
204 
205  typedef std::multimap<double, std::ostream*, std::greater<double> > StreamMap;
207 };
208 
211 {
212 public:
214  ~Wrapper() { delete myCallback; }
215  std::string label() const { return myCallback->label(); }
216  void run() { myCallback->run(); }
217  void cancel() { myCallback->cancel(); }
218 
219 protected:
221 };
222 
225 {
226 public:
228  std::string label() const { return myCallback.label(); }
229  void run() { myCallback.run(); }
230  void cancel() { myCallback.cancel(); }
231 
232 protected:
234 };
235 

Document ID: Generated on Tue Mar 8 22:13:38 EST 2016 from SVN revision 162938
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)