My Project: dynamics.mod/NewtonDynamics/source/core/dgThreadHive.h Source File

My Project

dgThreadHive.h
Go to the documentation of this file.
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21
22
23 #ifndef __DG_THREAD_HIVE_H__
24 #define __DG_THREAD_HIVE_H__
25
26 #include "dgThread.h"
27 #include "dgMemory.h"
28 #include "dgFastQueue.h"
29
30
31
32 //#define DG_THREAD_POOL_JOB_SIZE (512)
33 #define DG_THREAD_POOL_JOB_SIZE (1024 * 8)
34
35 typedef void (*dgWorkerThreadTaskCallback) (void* const context0, void* const context1, dgInt32 threadID);
36
37 class dgThreadHive
38 {
39 public:
40
41 class dgThreadJob
42 {
43 public:
44 dgThreadJob()
45 {
46 }
47
48 dgThreadJob (void* const context0, void* const context1, dgWorkerThreadTaskCallback callback)
49 :m_context0(context0)
50 ,m_context1(context1)
51 ,m_callback(callback)
52 {
53 }
54 void* m_context0;
55 void* m_context1;
56 dgWorkerThreadTaskCallback m_callback;
57 };
58
59
60 class dgThreadBee: public dgThread
61 {
62 public:
63 DG_CLASS_ALLOCATOR(allocator)
64
65 dgThreadBee();
66 ~dgThreadBee();
67
68 bool IsBusy() const;
69 void SetPerfomanceCounter(OnGetPerformanceCountCallback callback);
70 void SetUp(dgMemoryAllocator* const allocator, const char* const name, dgInt32 id, dgThreadHive* const hive);
71 virtual void Execute (dgInt32 threadId);
72
73 void RunNextJobInQueue(dgInt32 threadId);
74
75 dgInt32 m_isBusy;
76
77 dgUnsigned32 m_ticks;
78 dgSemaphore m_myMutex;
79 dgThreadHive* m_hive;
80 dgMemoryAllocator* m_allocator;
81 OnGetPerformanceCountCallback m_getPerformanceCount;
82 };
83
84 dgThreadHive(dgMemoryAllocator* const allocator);
85 virtual ~dgThreadHive();
86
87 virtual void OnBeginWorkerThread (dgInt32 threadId);
88 virtual void OnEndWorkerThread (dgInt32 threadId);
89
90 void SetMatertThread (dgThread* const mastertThread);
91
92 void GlobalLock() const;
93 void GlobalUnlock() const;
94
95 void GetIndirectLock (dgThread::dgCriticalSection* const criticalSectionLock) const;
96 void ReleaseIndirectLock (dgThread::dgCriticalSection* const criticalSectionLock) const;
97
98 void ClearTimers();
99 dgInt32 GetThreadCount() const;
100
101 dgInt32 GetMaxThreadCount() const;
102 void SetThreadsCount (dgInt32 count);
103
104 void QueueJob (dgWorkerThreadTaskCallback callback, void* const context0, void* const context1);
105 void SynchronizationBarrier ();
106
107 void SetPerfomanceCounter(OnGetPerformanceCountCallback callback);
108 dgUnsigned32 GetPerfomanceTicks (dgUnsigned32 threadIndex) const;
109
110 private:
111 void DestroyThreads();
112
113 dgInt32 m_beesCount;
114 dgInt32 m_currentIdleBee;
115 dgThreadBee* m_workerBees;
116 dgThread* m_myMasterThread;
117 dgMemoryAllocator* m_allocator;
118 dgThread::dgCriticalSection m_jobsCriticalSection;
119 mutable dgThread::dgCriticalSection m_globalCriticalSection;
120 dgThread::dgSemaphore m_myMutex[DG_MAX_THREADS_HIVE_COUNT];
121 dgFastQueue<dgThreadJob, DG_THREAD_POOL_JOB_SIZE> m_jobsPool;
122 };
123
124
125 inline void dgThreadHive::GlobalLock() const
126 {
127 GetIndirectLock(&m_globalCriticalSection);
128 }
129
130 inline void dgThreadHive::GlobalUnlock() const
131 {
132 ReleaseIndirectLock(&m_globalCriticalSection);
133 }
134
135
136 inline void dgThreadHive::GetIndirectLock (dgThread::dgCriticalSection* const criticalSectionLock) const
137 {
138 if (m_beesCount) {
139 criticalSectionLock->Lock();
140 }
141 }
142
143 inline void dgThreadHive::ReleaseIndirectLock (dgThread::dgCriticalSection* const criticalSectionLock) const
144 {
145 if (m_beesCount) {
146 criticalSectionLock->Unlock();
147 }
148 }
149
150 class dgThreadHiveScopeLock
151 {
152 public:
153 dgThreadHiveScopeLock(const dgThreadHive* const me, dgThread::dgCriticalSection* const criticalSectionLock)
154 :m_me(me)
155 ,m_lock (criticalSectionLock)
156 {
157 me->GetIndirectLock (criticalSectionLock);
158 }
159
160 ~dgThreadHiveScopeLock()
161 {
162 m_me->ReleaseIndirectLock (m_lock);
163 }
164
165 const dgThreadHive* m_me;
166 dgThread::dgCriticalSection* m_lock;
167 };
168
169
170
171 #endif
OnGetPerformanceCountCallback
dgUnsigned32(dgApi * OnGetPerformanceCountCallback)()
Definition: dgTypes.h:708
dgThreadHive::dgThreadJob::m_context1
void * m_context1
Definition: dgThreadHive.h:55
dgMemory.h
dgThreadHive::dgThreadJob::dgThreadJob
dgThreadJob()
Definition: dgThreadHive.h:44
dgThreadHive::QueueJob
void QueueJob(dgWorkerThreadTaskCallback callback, void *const context0, void *const context1)
Definition: dgThreadHive.cpp:204
dgThreadHive::ClearTimers
void ClearTimers()
Definition: dgThreadHive.cpp:170
dgThreadHiveScopeLock::dgThreadHiveScopeLock
dgThreadHiveScopeLock(const dgThreadHive *const me, dgThread::dgCriticalSection *const criticalSectionLock)
Definition: dgThreadHive.h:153
dgThreadHive::GetIndirectLock
void GetIndirectLock(dgThread::dgCriticalSection *const criticalSectionLock) const
Definition: dgThreadHive.h:136
dgThreadHive::dgThreadJob
Definition: dgThreadHive.h:41
dgThreadHive::SynchronizationBarrier
void SynchronizationBarrier()
Definition: dgThreadHive.cpp:231
dgThreadHive::dgThreadBee::m_allocator
dgMemoryAllocator * m_allocator
Definition: dgThreadHive.h:80
dgThreadHive
Definition: dgThreadHive.h:37
dgInt32
int dgInt32
Definition: dgTypes.h:218
void
typedef void(dgApi *ConstraintsForceFeeback)(const dgConstraint &me
dgThread
Definition: dgThread.h:32
dgFastQueue.h
dgThreadHive::dgThreadBee::m_hive
dgThreadHive * m_hive
Definition: dgThreadHive.h:79
dgThread::dgCriticalSection::Lock
void Lock()
Definition: dgThread.h:102
dgThreadHive::GlobalUnlock
void GlobalUnlock() const
Definition: dgThreadHive.h:130
dgThreadHive::dgThreadJob::dgThreadJob
dgThreadJob(void *const context0, void *const context1, dgWorkerThreadTaskCallback callback)
Definition: dgThreadHive.h:48
DG_CLASS_ALLOCATOR
#define DG_CLASS_ALLOCATOR(allocator)
Definition: dgMemory.h:60
dgThreadHiveScopeLock::m_me
const dgThreadHive * m_me
Definition: dgThreadHive.h:165
dgThreadHive::GetPerfomanceTicks
dgUnsigned32 GetPerfomanceTicks(dgUnsigned32 threadIndex) const
Definition: dgThreadHive.cpp:157
dgThreadHive::dgThreadBee::m_isBusy
dgInt32 m_isBusy
Definition: dgThreadHive.h:75
dgThreadHive::OnBeginWorkerThread
virtual void OnBeginWorkerThread(dgInt32 threadId)
Definition: dgThreadHive.cpp:222
dgThreadHiveScopeLock::~dgThreadHiveScopeLock
~dgThreadHiveScopeLock()
Definition: dgThreadHive.h:160
dgThreadHive::GetMaxThreadCount
dgInt32 GetMaxThreadCount() const
Definition: dgThreadHive.cpp:177
dgThread::dgSemaphore
Definition: dgThread.h:51
dgThreadHive::SetThreadsCount
void SetThreadsCount(dgInt32 count)
Definition: dgThreadHive.cpp:183
DG_MAX_THREADS_HIVE_COUNT
#define DG_MAX_THREADS_HIVE_COUNT
Definition: dgTypes.h:164
dgThreadHive::ReleaseIndirectLock
void ReleaseIndirectLock(dgThread::dgCriticalSection *const criticalSectionLock) const
Definition: dgThreadHive.h:143
dgThreadHive::dgThreadJob::m_context0
void * m_context0
Definition: dgThreadHive.h:54
dgThreadHive::GetThreadCount
dgInt32 GetThreadCount() const
Definition: dgThreadHive.cpp:165
dgThread::dgCriticalSection::Unlock
void Unlock()
Definition: dgThread.h:116
dgUnsigned32
unsigned dgUnsigned32
Definition: dgTypes.h:219
dgThreadHiveScopeLock
Definition: dgThreadHive.h:150
dgThreadHive::SetPerfomanceCounter
void SetPerfomanceCounter(OnGetPerformanceCountCallback callback)
Definition: dgThreadHive.cpp:149
dgWorkerThreadTaskCallback
void(* dgWorkerThreadTaskCallback)(void *const context0, void *const context1, dgInt32 threadID)
Definition: dgThreadHive.h:35
threadIndex
dgFloat32 dgInt32 threadIndex
Definition: dgConstraint.h:44
dgThreadHive::dgThreadJob::m_callback
dgWorkerThreadTaskCallback m_callback
Definition: dgThreadHive.h:56
dgThreadHive::dgThreadBee
Definition: dgThreadHive.h:60
dgThreadHive::dgThreadHive
dgThreadHive(dgMemoryAllocator *const allocator)
Definition: dgThreadHive.cpp:117
dgThreadHive::~dgThreadHive
virtual ~dgThreadHive()
Definition: dgThreadHive.cpp:129
dgThreadHiveScopeLock::m_lock
dgThread::dgCriticalSection * m_lock
Definition: dgThreadHive.h:166
dgMemoryAllocator
Definition: dgMemory.h:73
dgThreadHive::dgThreadBee::m_myMutex
dgSemaphore m_myMutex
Definition: dgThreadHive.h:78
dgThread.h
dgThreadHive::SetMatertThread
void SetMatertThread(dgThread *const mastertThread)
Definition: dgThreadHive.cpp:134
dgThreadHive::dgThreadBee::m_ticks
dgUnsigned32 m_ticks
Definition: dgThreadHive.h:77
dgThread::dgCriticalSection
Definition: dgThread.h:35
dgThreadHive::OnEndWorkerThread
virtual void OnEndWorkerThread(dgInt32 threadId)
Definition: dgThreadHive.cpp:226
dgThreadHive::GlobalLock
void GlobalLock() const
Definition: dgThreadHive.h:125
dgFastQueue
Definition: dgFastQueue.h:33
dgThreadHive::dgThreadBee::m_getPerformanceCount
OnGetPerformanceCountCallback m_getPerformanceCount
Definition: dgThreadHive.h:81