CFx SDK Documentation 2024 SP0
Loading...
Searching...
No Matches
ThreadsCounter.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2022, 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 Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2022 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{
39public:
40 OdMutexPtr(): m_ptr(0){}
42 OdMutex* get() { if(!m_ptr) create(); return m_ptr; }
43 void clear();
44private:
45 void create();
46
47private:
48 static OdMutex s_mt;
49 OdMutex* m_ptr;
50};
51
65{
66public:
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
82protected:
83 OdMutexPtrAutoLock(): m_mutex(NULL), m_bEnable(false){}
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 }
95protected:
97private:
98 bool m_bEnable;
99};
100
108{
109 public:
111
118 virtual void addRef() = 0;
119
126 virtual void release() = 0;
127
134 virtual void increase(unsigned /*nThreads*/, const unsigned* /*aThreads*/, unsigned /*nThreadAttributes*/) { }
141 virtual void decrease(unsigned /*nThreads*/, const unsigned* /*aThreads*/, unsigned /*nThreadAttributes*/) { }
142
148 virtual void startThread(unsigned /*nThreadId*/, unsigned /*nThreadAttributes*/) { }
154 virtual void stopThread(unsigned /*nThreadId*/, unsigned /*nThreadAttributes*/) { }
155 protected:
157};
158
159// Function for execute in the main thread
160typedef void (*MainThreadFunc)(void*);
161// Function for initiate execution in the main thread by external process
163
165
173{
174public:
176 {
177 kNoAttributes = 0, // Simple Mt-process which doesn't require any special initializations
178
179 kMtLoadingAttributes = (1 << 0), // Actual for both MtLoading and loading of DB's in multiple threads
180 kMtRegenAttributes = (1 << 1), // Multithread regeneration
181 kStRegenAttributes = (1 << 2), // Regeneration w/o cache (local heaps not required)
182 kMtDisplayAttributes = (1 << 3), // Multithread display
183 kMtModelerAttributes = (1 << 4), // Modeling operations in parallel threads
184 kAllAttributes = 0xFFFFFFFF // Applicable for Mt functionality tests
185 };
186protected:
188 : m_count(1)
189 , m_mainThreadFunc(NULL)
190 , m_threadPool(NULL) { }
192public:
196 operator bool() const { return m_count > 1; }
197
216 bool hasReactor(ThreadsCounterReactor *pReactor) const;
220 int nReactors() const;
221
222 /* Following methods are intended for internal usage by ODA Platform libraries and not recommended for usage outside. */
223
228 void setThreadPoolService(OdRxThreadPoolService *pService) { m_threadPool = pService; }
232 OdRxThreadPoolService *getThreadPoolService() { return m_threadPool; }
233
240 void increase(unsigned nThreads, const unsigned* aThreads, unsigned nThreadAttributes = ThreadsCounter::kNoAttributes)
241 { ++m_count; increaseProc(nThreads, aThreads, nThreadAttributes); }
247 void decrease(unsigned nThreads, const unsigned* aThreads)
248 { --m_count; decreaseProc(nThreads, aThreads); }
249
253 void startThread() { startThreadProc(); }
257 void stopThread() { stopThreadProc(); }
258
264 bool hasThread(unsigned nThreadId, unsigned *pThreadAttributes)
265 { return hasThreadProc(nThreadId, pThreadAttributes); }
266
271 void setMainThreadFunc(ExecuteMainThreadFunc func) { m_mainThreadFunc = func; }
275 ExecuteMainThreadFunc getMainThreadFunc() { return m_mainThreadFunc; }
276
277protected:
278 void increaseProc(unsigned nThreads, const unsigned* aThreads, unsigned nThreadAttributes);
279 void decreaseProc(unsigned nThreads, const unsigned* aThreads);
282 bool hasThreadProc(unsigned nThreadId, unsigned *pThreadAttributes);
283
284protected:
288};
289
291
292FIRSTDLL_EXPORT bool odExecuteMainThreadAction(MainThreadFunc mtFunc, void *pArg, bool bExecST = true);
293
296
297#define TD_AUTOLOCK_P(Mutex) OdMutexPtrAutoLock autoLock(Mutex);
298#define TD_AUTOLOCK_P_DEF(Mutex) OdMutexPtrAutoLock autoLock(odThreadsCounter() ? Mutex.get() : NULL);
299
300#include "TD_PackPop.h"
301
302#endif //_THREADS_COUNTER_H_
false
Definition: DimVarDefs.h:165
int OdRefCounter
Definition: OdMutex.h:487
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
FIRSTDLL_EXPORT void odThreadYield()
void(* ExecuteMainThreadFunc)(MainThreadFunc, void *)
FIRSTDLL_EXPORT bool odExecuteMainThreadAction(MainThreadFunc mtFunc, void *pArg, bool bExecST=true)
void(* MainThreadFunc)(void *)
FIRSTDLL_EXPORT ThreadsCounter & odThreadsCounter()
FIRSTDLL_EXPORT unsigned odGetCurrentThreadId()
void lock()
Definition: OdMutex.h:84
void unlock()
Definition: OdMutex.h:91
void set(bool bEnable)
OdMutexPtrAutoLock(OdMutex *mutex)
void clear()
OdMutex * get()
void increaseProc(unsigned nThreads, const unsigned *aThreads, unsigned nThreadAttributes)
void startThreadProc()
void setThreadPoolService(OdRxThreadPoolService *pService)
bool removeReactor(ThreadsCounterReactor *pReactor)
OdRefCounter m_count
ExecuteMainThreadFunc m_mainThreadFunc
bool hasThreadProc(unsigned nThreadId, unsigned *pThreadAttributes)
OdRxThreadPoolService * getThreadPoolService()
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 addRef()=0
virtual void startThread(unsigned, unsigned)
virtual void increase(unsigned, const unsigned *, unsigned)
virtual void stopThread(unsigned, unsigned)
virtual void release()=0
GLenum func
Definition: gles2_ext.h:262
typedef void(APIENTRYP PFNGLACTIVETEXTUREPROC)(GLenum texture)