VR-Forces Development_Version Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parallelTickManager.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2014 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
9 
12 
13 #pragma once
14 
15 #include "vrfcore/vrfcoreDefines.h"
16 
18 #include "vrfutil/profiler.h"
19 
20 #include <boost/noncopyable.hpp>
21 #include <boost/unordered/unordered_map.hpp>
22 
23 #include <tbb/mutex.h>
24 #include <tbb/tbb_thread.h>
25 #include <tbb/compat/condition_variable>
26 #include <tbb/enumerable_thread_specific.h>
27 
28 #include <vector>
29 
34 class DtCriticalSection;
35 class DtSimManager;
36 
58 class DT_DLL_vrfcore DtParallelTickManager : boost::noncopyable
59 {
60 public:
62  {
65  UNLOCKED
66  };
67 
71  {
72  friend class DtParallelTickManager;
73 
75  void run();
76 
77  public:
78  virtual ~CallbackInterface();
79 
81  virtual void runCallback() = 0;
82  };
83 
84  typedef boost::unordered_map<std::string, bool> EnabledMap;
85 
87  virtual ~DtParallelTickManager();
88 
90  virtual void setCallbackQueue(DtVrfCallbackQueue*);
91 
93  virtual DtVrfCallbackQueue* queue() const;
94 
101 
102  virtual void flushQueue(bool useThisThread);
103  virtual void flushQueue();
105 
107 
108  virtual bool mainThreadWait() const;
109  virtual void setMainThreadWait(bool);
111 
114 
115  virtual bool active() const;
116  virtual void setActive(bool);
118 
127  virtual bool enabled(const std::string&, bool def) const;
128 
130  virtual bool executeCommand(const std::string&, bool on);
131 
133  virtual void printInfo(std::ostream&) const;
134 
142 
143  virtual void addCallback(CallbackInterface*, bool runNow=false);
144  virtual void addCallback(CallbackInterface&, bool runNow=false);
145 
146  virtual void addCallback(CallbackInterface*, const std::string& tag, bool def=true);
147  virtual void addCallback(CallbackInterface&, const std::string& tag, bool def=true);
148 
149  void addCallback(CallbackInterface*, const char* tag, bool def=true);
150  void addCallback(CallbackInterface&, const char* tag, bool def=true);
151 
152  template <typename Container>
153  void addCallbacks(Container& cont, bool runNow=false)
154  {
155  typedef typename Container::iterator iterator;
156  for (iterator i=cont.begin(), end=cont.end(); i != end; ++i)
157  addCallback(*i, runNow);
158  }
159 
160  template <typename Container>
161  void addCallbacks(Container& cont, const std::string& tag, bool def=true)
162  {
163  bool runNow = !enabled(tag, def);
164  typedef typename Container::iterator iterator;
165  for (iterator i=cont.begin(), end=cont.end(); i != end; ++i)
166  addCallback(*i, runNow);
167  }
168 
169  template <typename Container>
170  void addCallbacks(Container& cont, const char* tag, bool def=true)
171  {
172  addCallbacks(cont, std::string(tag), def);
173  }
175 
177 
178  virtual void lock();
179  virtual void unlock();
180  virtual void readLock();
181  virtual void readUnlock();
182  virtual void upgradeToWriter();
183  virtual void downgradeToReader();
184 
185  virtual void changeAccess(AccessType from, AccessType to);
187 
188  class ThreadLocalInfo;
189 
190  virtual ThreadLocalInfo& threadLocalInfo();
191 
192 private:
194  virtual void createCallbackQueue(bool force=false);
195 
196  typedef tbb::mutex Mutex;
197  typedef tbb::interface5::condition_variable ConditionVariable;
198  typedef tbb::interface5::unique_lock<Mutex> ScopedLock;
199 
200  mutable Mutex myMutex;
204  unsigned myNumWriters;
205  unsigned myNumReaders;
207 
208  typedef tbb::cache_aligned_allocator<ThreadLocalInfo*> ThreadLocalAllocator;
209  //typedef tbb::enumerable_thread_specific<
211  typedef tbb::enumerable_thread_specific<
212  ThreadLocalInfo*,
214  tbb::ets_key_per_instance> ThreadLocalContainer;
215 
217 
220 
223 
226 };
227 
228 class DT_DLL_vrfcore DtCriticalSection : boost::noncopyable
229 {
230 public:
232 
234  explicit DtCriticalSection(DtSimManager*);
237 
239 
240  void reset(AccessType access);
241 
242  void unlock();
243  void lockShared();
244  void lock();
245 
246  void release()
247  {
248  reset(from);
249  }
250 
251 protected:
252  explicit DtCriticalSection(DtParallelTickManager*, AccessType to);
254 
255  friend class DtParallelTickManager;
256 
259 
262 };
263 
264 class DT_DLL_vrfcore DtParallelTickManager::ThreadLocalInfo : boost::noncopyable
265 {
266 public:
267  explicit ThreadLocalInfo(DtParallelTickManager* m);
268 
269  DtParallelTickManager::AccessType state() const;
270 
271 private:
272  friend class DtCriticalSection;
273 
274  void push(DtCriticalSection& cs);
275  void pop(DtCriticalSection& cs);
276 
277  typedef std::vector<DtCriticalSection*> Stack;
279 };

Document ID: Generated on Mon Jul 4 01:00:18 EDT 2016 from SVN revision 166489
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)