CFx SDK Documentation  2020SP3
ThreadsCounter.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 #ifndef _THREADS_COUNTER_H_
25 #define _THREADS_COUNTER_H_
26 
27 #include "TD_PackPush.h"
28 #include "OdMutex.h"
29 
38 {
39 public:
40  OdMutexPtr(): m_ptr(0){}
42  OdMutex* get() { if(!m_ptr) create(); return m_ptr; }
43  void clear();
44 private:
45  void create();
46 
47 private:
48  static OdMutex s_mt;
49  OdMutex* m_ptr;
50 };
51 
65 {
66 public:
69  OdMutexPtrAutoLock(OdMutex* mutex) : m_mutex(mutex), m_bEnable(false)
70  {
71  lock();
72  }
73 
75  {
76  unlock();
77  }
78 
79  void lock() { set(true); }
80  void unlock() { set(false); }
81 
82 protected:
84  void set(bool bEnable)
85  {
86  if(m_mutex && (m_bEnable != bEnable))
87  {
88  m_bEnable = bEnable;
89  if(bEnable)
90  m_mutex->lock();
91  else
92  m_mutex->unlock();
93  }
94  }
95 protected:
97 private:
98  bool m_bEnable;
99 };
100 
108 {
109  public:
112 
119  virtual void increase(unsigned /*nThreads*/, const unsigned* /*aThreads*/, unsigned /*nThreadAttributes*/) { }
126  virtual void decrease(unsigned /*nThreads*/, const unsigned* /*aThreads*/, unsigned /*nThreadAttributes*/) { }
127 
133  virtual void startThread(unsigned /*nThreadId*/, unsigned /*nThreadAttributes*/) { }
139  virtual void stopThread(unsigned /*nThreadId*/, unsigned /*nThreadAttributes*/) { }
140 };
141 
142 // Function for execute in the main thread
143 typedef void (*MainThreadFunc)(void*);
144 // Function for initiate execution in the main thread by external process
146 
148 
156 {
157 public:
159  {
160  kNoAttributes = 0, // Simple Mt-process which doesn't require any special initializations
161 
162  kMtLoadingAttributes = (1 << 0), // Actual for both MtLoading and loading of DB's in multiple threads
163  kMtRegenAttributes = (1 << 1), // Multithread regeneration
164  kStRegenAttributes = (1 << 2), // Regeneration w/o cache (local heaps not required)
165  kMtDisplayAttributes = (1 << 3), // Multithread display
166  kMtModelerAttributes = (1 << 4), // Modeling operations in parallel threads
167  kAllAttributes = 0xFFFFFFFF // Applicable for Mt functionality tests
168  };
169 protected:
171  : m_count(1)
172  , m_mainThreadFunc(NULL)
173  , m_threadPool(NULL) { }
175 public:
179  operator bool() const { return m_count > 1; }
180 
199  bool hasReactor(ThreadsCounterReactor *pReactor) const;
203  int nReactors() const;
204 
205  /* Following methods are intended for internal usage by Teigha libraries and not recommended for usage outside. */
206 
211  void setThreadPoolService(OdRxThreadPoolService *pService) { m_threadPool = pService; }
215  OdRxThreadPoolService *getThreadPoolService() { return m_threadPool; }
216 
223  void increase(unsigned nThreads, const unsigned* aThreads, unsigned nThreadAttributes = ThreadsCounter::kNoAttributes)
224  { ++m_count; increaseProc(nThreads, aThreads, nThreadAttributes); }
230  void decrease(unsigned nThreads, const unsigned* aThreads)
231  { --m_count; decreaseProc(nThreads, aThreads); }
232 
236  void startThread() { startThreadProc(); }
240  void stopThread() { stopThreadProc(); }
241 
247  bool hasThread(unsigned nThreadId, unsigned *pThreadAttributes)
248  { return hasThreadProc(nThreadId, pThreadAttributes); }
249 
254  void setMainThreadFunc(ExecuteMainThreadFunc func) { m_mainThreadFunc = func; }
258  ExecuteMainThreadFunc getMainThreadFunc() { return m_mainThreadFunc; }
259 
260 protected:
261  void increaseProc(unsigned nThreads, const unsigned* aThreads, unsigned nThreadAttributes);
262  void decreaseProc(unsigned nThreads, const unsigned* aThreads);
265  bool hasThreadProc(unsigned nThreadId, unsigned *pThreadAttributes);
266 
267 protected:
271 };
272 
274 
275 FIRSTDLL_EXPORT bool odExecuteMainThreadAction(MainThreadFunc mtFunc, void *pArg, bool bExecST = true);
276 
279 
280 #define TD_AUTOLOCK_P(Mutex) OdMutexPtrAutoLock autoLock(Mutex);
281 #define TD_AUTOLOCK_P_DEF(Mutex) OdMutexPtrAutoLock autoLock(odThreadsCounter() ? Mutex.get() : NULL);
282 
283 #include "TD_PackPop.h"
284 
285 #endif //_THREADS_COUNTER_H_
FIRSTDLL_EXPORT
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
odGetCurrentThreadId
FIRSTDLL_EXPORT unsigned odGetCurrentThreadId()
OdMutex::unlock
void unlock()
Definition: OdMutex.h:76
ThreadsCounter::hasThread
bool hasThread(unsigned nThreadId, unsigned *pThreadAttributes)
Definition: ThreadsCounter.h:247
ThreadsCounter::increase
void increase(unsigned nThreads, const unsigned *aThreads, unsigned nThreadAttributes=ThreadsCounter::kNoAttributes)
Definition: ThreadsCounter.h:223
NULL
#define NULL
Definition: GsProperties.h:177
ThreadsCounter::increaseProc
void increaseProc(unsigned nThreads, const unsigned *aThreads, unsigned nThreadAttributes)
OdMutexPtrAutoLock
Definition: ThreadsCounter.h:65
ThreadsCounter::startThread
void startThread()
Definition: ThreadsCounter.h:236
odThreadYield
FIRSTDLL_EXPORT void odThreadYield()
MainThreadFunc
void(* MainThreadFunc)(void *)
Definition: ThreadsCounter.h:143
ThreadsCounterReactor::decrease
virtual void decrease(unsigned, const unsigned *, unsigned)
Definition: ThreadsCounter.h:126
ThreadsCounterReactor::increase
virtual void increase(unsigned, const unsigned *, unsigned)
Definition: ThreadsCounter.h:119
ThreadsCounter::removeReactor
bool removeReactor(ThreadsCounterReactor *pReactor)
false
false
Definition: DimVarDefs.h:165
OdMutexPtrAutoLock::m_mutex
OdMutex * m_mutex
Definition: ThreadsCounter.h:96
ThreadsCounterReactor::stopThread
virtual void stopThread(unsigned, unsigned)
Definition: ThreadsCounter.h:139
OdRxThreadPoolService
Definition: RxThreadPoolService.h:373
ThreadsCounter::kNoAttributes
@ kNoAttributes
Definition: ThreadsCounter.h:160
ThreadsCounter::getMainThreadFunc
ExecuteMainThreadFunc getMainThreadFunc()
Definition: ThreadsCounter.h:258
OdRefCounter
int OdRefCounter
Definition: OdMutex.h:436
ThreadsCounter::m_threadPool
OdRxThreadPoolService * m_threadPool
Definition: ThreadsCounter.h:270
OdMutexPtr::~OdMutexPtr
~OdMutexPtr()
TD_PackPop.h
ThreadsCounter::stopThreadProc
void stopThreadProc()
ThreadsCounter::m_mainThreadFunc
ExecuteMainThreadFunc m_mainThreadFunc
Definition: ThreadsCounter.h:269
ThreadsCounterReactor::~ThreadsCounterReactor
virtual ~ThreadsCounterReactor()
Definition: ThreadsCounter.h:111
OdMutexPtrAutoLock::lock
void lock()
Definition: ThreadsCounter.h:79
OdMutexPtrAutoLock::unlock
void unlock()
Definition: ThreadsCounter.h:80
odExecuteMainThreadAction
FIRSTDLL_EXPORT bool odExecuteMainThreadAction(MainThreadFunc mtFunc, void *pArg, bool bExecST=true)
ThreadsCounter::startThreadProc
void startThreadProc()
ThreadsCounterReactor
Definition: ThreadsCounter.h:108
OdMutexPtrAutoLock::OdMutexPtrAutoLock
OdMutexPtrAutoLock()
Definition: ThreadsCounter.h:83
ThreadsCounter::decrease
void decrease(unsigned nThreads, const unsigned *aThreads)
Definition: ThreadsCounter.h:230
func
GLenum func
Definition: gles2_ext.h:262
OdMutexPtr::get
OdMutex * get()
Definition: ThreadsCounter.h:42
OdMutex.h
ExecuteMainThreadFunc
void(* ExecuteMainThreadFunc)(MainThreadFunc, void *)
Definition: ThreadsCounter.h:145
OdMutexPtr::clear
void clear()
TD_PackPush.h
ThreadsCounter::stopThread
void stopThread()
Definition: ThreadsCounter.h:240
ThreadsCounter::m_count
OdRefCounter m_count
Definition: ThreadsCounter.h:268
ThreadsCounter::setMainThreadFunc
void setMainThreadFunc(ExecuteMainThreadFunc func)
Definition: ThreadsCounter.h:254
ThreadsCounter::nReactors
int nReactors() const
ThreadsCounter::~ThreadsCounter
~ThreadsCounter()
Definition: ThreadsCounter.h:174
OdMutex::lock
void lock()
Definition: OdMutex.h:69
ThreadsCounter::getThreadPoolService
OdRxThreadPoolService * getThreadPoolService()
Definition: ThreadsCounter.h:215
ThreadsCounter::setThreadPoolService
void setThreadPoolService(OdRxThreadPoolService *pService)
Definition: ThreadsCounter.h:211
OdMutexPtrAutoLock::set
void set(bool bEnable)
Definition: ThreadsCounter.h:84
ThreadsCounter::hasReactor
bool hasReactor(ThreadsCounterReactor *pReactor) const
ThreadsCounter::ThreadsCounter
ThreadsCounter()
Definition: ThreadsCounter.h:170
OdMutexPtrAutoLock::OdMutexPtrAutoLock
OdMutexPtrAutoLock(OdMutex *mutex)
Definition: ThreadsCounter.h:69
ThreadsCounter::hasThreadProc
bool hasThreadProc(unsigned nThreadId, unsigned *pThreadAttributes)
OdMutexPtr
Definition: ThreadsCounter.h:38
odThreadsCounter
FIRSTDLL_EXPORT ThreadsCounter & odThreadsCounter()
ThreadsCounterReactor::startThread
virtual void startThread(unsigned, unsigned)
Definition: ThreadsCounter.h:133
ThreadsCounter::decreaseProc
void decreaseProc(unsigned nThreads, const unsigned *aThreads)
ThreadsCounterReactor::ThreadsCounterReactor
ThreadsCounterReactor()
Definition: ThreadsCounter.h:110
ThreadsCounter::addReactor
bool addReactor(ThreadsCounterReactor *pReactor)
void
typedef void(APIENTRYP PFNGLACTIVETEXTUREPROC)(GLenum texture)
ThreadsCounter::ThreadAttributes
ThreadAttributes
Definition: ThreadsCounter.h:159
ThreadsCounter
Definition: ThreadsCounter.h:156
OdMutex
Definition: OdMutex.h:50
OdMutexPtrAutoLock::~OdMutexPtrAutoLock
~OdMutexPtrAutoLock()
Definition: ThreadsCounter.h:74
OdMutexPtr::OdMutexPtr
OdMutexPtr()
Definition: ThreadsCounter.h:40