VR-Forces 4.7 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 #include <boost/ptr_container/ptr_vector.hpp>
23 
24 #include <tbb/mutex.h>
25 #include <tbb/tbb_thread.h>
26 #include <tbb/compat/condition_variable>
27 #include <tbb/enumerable_thread_specific.h>
28 #include <tbb/atomic.h>
29 #include <tbb/concurrent_queue.h>
30 
31 #include <vector>
32 
33 #include "vrfutil/itt.h"
34 
39 class DtCriticalSection;
40 class DtSimManager;
41 
65 class DT_DLL_vrfcore DtParallelTickManager : boost::noncopyable
66 {
67 public:
69  {
73  DEFERRED
74  };
75 
79  {
80  friend class DtParallelTickManager;
81 
83  void run();
84 
85  public:
86  virtual ~CallbackInterface();
87 
89  virtual void runCallback() = 0;
90  };
91 
92  typedef boost::unordered_map<std::string, bool> EnabledMap;
93 
95  virtual ~DtParallelTickManager();
96 
98  virtual void setCallbackQueue(DtVrfCallbackQueue*);
99 
101  virtual DtVrfCallbackQueue* queue() const;
102 
109 
110  virtual void flushQueue(bool useThisThread);
111  virtual void flushQueue();
113 
115  virtual void clearDeferredJobs();
116 
118 
119  virtual bool mainThreadWait() const;
120  virtual void setMainThreadWait(bool);
122 
126 
127  virtual unsigned maxSpinCount() const;
128  virtual void setMaxSpinCount(unsigned);
130 
133 
134  virtual bool active() const;
135  virtual void setActive(bool);
137 
146  virtual bool enabled(const std::string&, bool def) const;
147 
149  virtual bool executeCommand(const std::string&, bool on);
150 
152  virtual void printInfo(std::ostream&) const;
153 
161 
162  virtual void addCallback(CallbackInterface*);
163  virtual void addCallback(CallbackInterface&);
164 
165  virtual void addCallback(CallbackInterface*, const std::string& tag, bool def=true);
166  virtual void addCallback(CallbackInterface&, const std::string& tag, bool def=true);
167 
168  void addCallback(CallbackInterface*, const char* tag, bool def=true);
169  void addCallback(CallbackInterface&, const char* tag, bool def=true);
170 
171  template <typename Container>
172  void addCallbacks(Container& cont)
173  {
174  typedef typename Container::iterator iterator;
175  for (iterator i=cont.begin(), end=cont.end(); i != end; ++i)
176  addCallback(*i);
177  }
178 
179  template <typename Container>
180  void addCallbacks(Container& cont, const std::string& tag, bool def=true)
181  {
182  bool runNow = !enabled(tag, def);
183  typedef typename Container::iterator iterator;
184  if (runNow)
185  {
186  for (iterator i = cont.begin(), end = cont.end(); i != end; ++i)
187  {
188  RunJob(*i);
189  }
190  }
191  else
192  {
193  for (iterator i = cont.begin(), end = cont.end(); i != end; ++i)
194  {
195  addCallback(*i);
196  }
197  }
198  }
199 
200  template <typename Container>
201  void addCallbacks(Container& cont, const char* tag, bool def=true)
202  {
203  addCallbacks(cont, std::string(tag), def);
204  }
206 
211 
212  virtual bool lock();
213  virtual bool unlock();
214  virtual bool readLock();
215  virtual bool readUnlock();
216  virtual bool upgradeToWriter();
217  virtual bool downgradeToReader();
218 
219  virtual bool changeAccess(AccessType from, AccessType to);
221 
233 
234 
237  enum JobClass
238  {
241  };
242 
247  virtual void configureDeferredJobClass(
248  JobClass jobclass,
249  AccessType access,
250  unsigned maxConcurrent);
251 
253  virtual void addDeferredJob(JobClass jobclass, const boost::function<void ()>& callback);
254 
256 
257  virtual void addVRLinkDeferredJob(const boost::function<void()>& callback);
259 
263  virtual void setDeferJobs(bool);
265 
266  class ThreadLocalInfo;
267 
268  virtual ThreadLocalInfo& threadLocalInfo();
269 
270 private:
271  static void RunJob(CallbackInterface*);
272  static void RunJob(CallbackInterface&);
273 
274  virtual void executeDeferredJobs(AccessType access);
275  virtual void flushDeferredJobs();
276 
277  tbb::atomic<int> myAccessCount;
278 
279  typedef tbb::cache_aligned_allocator<ThreadLocalInfo*> ThreadLocalAllocator;
280  //typedef tbb::enumerable_thread_specific<
282  typedef tbb::enumerable_thread_specific<
283  ThreadLocalInfo*,
285  tbb::ets_key_per_instance> ThreadLocalContainer;
286 
288 
291 
294  unsigned myMaxSpinCount;
295 
296  struct ClassInfo
297  {
298  typedef tbb::concurrent_queue< boost::function<void()> > JobQueue;
299 
301  tbb::atomic<unsigned> jobCount;
302  tbb::atomic<unsigned> runCount;
303  unsigned max;
305 
306  explicit ClassInfo(
307  AccessType access = EXCLUSIVE,
308  unsigned max = 1);
309  };
310 
312  boost::ptr_vector<ClassInfo> myDeferredJobs;
313  tbb::atomic<unsigned> myDeferredJobCount;
314 
317 
319 
325 };
326 
335 class DT_DLL_vrfcore DtCriticalSection : boost::noncopyable
336 {
337 public:
339 
341  explicit DtCriticalSection(DtSimManager*, AccessType initState);
342 
344  explicit DtCriticalSection(DtSimManager*);
345 
348 
352 
354 
356  void reset(AccessType access);
357 
359  void unlock();
360 
362  void lockShared();
363 
365  void lock();
366 
368  void release()
369  {
370  reset(from);
371  }
372 
373 protected:
374  explicit DtCriticalSection(DtParallelTickManager*, AccessType to);
376 
377  friend class DtParallelTickManager;
378 
381 
384 };
385 
386 class DT_DLL_vrfcore DtParallelTickManager::ThreadLocalInfo : boost::noncopyable
387 {
388 public:
389  explicit ThreadLocalInfo(DtParallelTickManager* m);
390 
391  DtParallelTickManager::AccessType state() const;
392 
393 private:
394  friend class DtCriticalSection;
395 
396  void push(DtCriticalSection& cs);
397  void pop(DtCriticalSection& cs);
398 
399  typedef std::vector<DtCriticalSection*> Stack;
401 };

Document ID: Generated on Fri Apr 26 21:53:14 EDT 2019 from SVN revision 197883
Copyright © 2005-2019 VT MAK. All Rights Reserved (www.mak.com)