CFx SDK Documentation  2022 SP0
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_
false
Definition: DimVarDefs.h:165
#define NULL
Definition: GsProperties.h:177
int OdRefCounter
Definition: OdMutex.h:436
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
FIRSTDLL_EXPORT ThreadsCounter & odThreadsCounter()
FIRSTDLL_EXPORT void odThreadYield()
void(* ExecuteMainThreadFunc)(MainThreadFunc, void *)
FIRSTDLL_EXPORT bool odExecuteMainThreadAction(MainThreadFunc mtFunc, void *pArg, bool bExecST=true)
void(* MainThreadFunc)(void *)
FIRSTDLL_EXPORT unsigned odGetCurrentThreadId()
void lock()
Definition: OdMutex.h:69
void unlock()
Definition: OdMutex.h:76
void set(bool bEnable)
OdMutexPtrAutoLock(OdMutex *mutex)
void clear()
OdMutex * get()
void increaseProc(unsigned nThreads, const unsigned *aThreads, unsigned nThreadAttributes)
void startThreadProc()
OdRxThreadPoolService * getThreadPoolService()
void setThreadPoolService(OdRxThreadPoolService *pService)
bool removeReactor(ThreadsCounterReactor *pReactor)
OdRefCounter m_count
ExecuteMainThreadFunc m_mainThreadFunc
bool hasThreadProc(unsigned nThreadId, unsigned *pThreadAttributes)
ExecuteMainThreadFunc getMainThreadFunc()
void stopThreadProc()
void setMainThreadFunc(ExecuteMainThreadFunc func)
void decreaseProc(unsigned nThreads, const unsigned *aThreads)
OdRxThreadPoolService * m_threadPool
bool hasReactor(ThreadsCounterReactor *pReactor) const
bool hasThread(unsigned nThreadId, unsigned *pThreadAttributes)
void decrease(unsigned nThreads, const unsigned *aThreads)
int nReactors() const
void increase(unsigned nThreads, const unsigned *aThreads, unsigned nThreadAttributes=ThreadsCounter::kNoAttributes)
bool addReactor(ThreadsCounterReactor *pReactor)
virtual ~ThreadsCounterReactor()
virtual void decrease(unsigned, const unsigned *, unsigned)
virtual void startThread(unsigned, unsigned)
virtual void increase(unsigned, const unsigned *, unsigned)
virtual void stopThread(unsigned, unsigned)
GLenum func
Definition: gles2_ext.h:262
typedef void(APIENTRYP PFNGLACTIVETEXTUREPROC)(GLenum texture)