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

My Project

dgThread.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 #ifndef __DG_THREAD_API_H__
23 #define __DG_THREAD_API_H__
24
25 // by default newton is run on a separate thread, optionally concurrent with the calling thread, it also uses a thread job pool for multi core systems.
26 // define DG_USE_THREAD_EMULATION on the command line for platform that do not support hardware multi threading or if multi threading is not stable
27 //#define DG_USE_THREAD_EMULATION
28
29 //
30 //#define DG_USE_MUTEX_CRITICAL_SECTION
31
32 class dgThread
33 {
34 public:
35 class dgCriticalSection
36 {
37 public:
38 dgCriticalSection();
39 ~dgCriticalSection();
40 void Lock();
41 void Unlock();
42
43 private:
44 #if (defined (DG_USE_THREAD_EMULATION) || !defined (DG_USE_MUTEX_CRITICAL_SECTION))
45 dgInt32 m_mutex;
46 #else
47 pthread_mutex_t m_mutex;
48 #endif
49 };
50
51 class dgSemaphore
52 {
53 public:
54 dgSemaphore ();
55 ~dgSemaphore ();
56 void Wait();
57 void Release();
58
59 private:
60 #ifdef DG_USE_THREAD_EMULATION
61 dgInt32 m_sem;
62 #else
63 #if defined (_MACOSX_VER) || defined (IOS) || defined (__APPLE__)
64 sem_t* m_sem;
65 dgInt32 m_nameId;
66 #else
67 sem_t m_sem;
68 #endif
69 #endif
70 friend class dgThread;
71 };
72
73 dgThread ();
74 dgThread (const char* const name, dgInt32 id);
75 virtual ~dgThread ();
76
77 virtual void Execute (dgInt32 threadId) = 0;
78
79 bool StillBusy() const;
80 void SuspendExecution (dgSemaphore& mutex);
81 void SuspendExecution (dgInt32 count, dgSemaphore* const mutexes);
82
83 dgInt32 GetPriority() const;
84 void SetPriority(int priority);
85
86 protected:
87 void Init ();
88 void Init (const char* const name, dgInt32 id);
89 void Close ();
90
91 static void* dgThreadSystemCallback(void* threadData);
92
93 pthread_t m_handle;
94 dgInt32 m_id;
95 dgInt32 m_terminate;
96 dgInt32 m_threadRunning;
97
98 char m_name[32];
99 };
100
101
102 DG_INLINE void dgThread::dgCriticalSection::Lock()
103 {
104 #ifndef DG_USE_THREAD_EMULATION
105 #ifdef DG_USE_MUTEX_CRITICAL_SECTION
106 pthread_mutex_lock(&m_mutex);
107 #else
108 while (dgInterlockedExchange(&m_mutex, 1)) {
109 dgThreadYield();
110 }
111
112 #endif
113 #endif
114 }
115
116 DG_INLINE void dgThread::dgCriticalSection::Unlock()
117 {
118 #ifndef DG_USE_THREAD_EMULATION
119 #ifdef DG_USE_MUTEX_CRITICAL_SECTION
120 pthread_mutex_unlock(&m_mutex);
121 #else
122 dgInterlockedExchange(&m_mutex, 0);
123 #endif
124 #endif
125 }
126
127
128 #endif
dgThread::m_handle
pthread_t m_handle
Definition: dgThread.h:93
pthread_mutex_lock
PTW32_DLLPORT int PTW32_CDECL pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: pthread_mutex_lock.c:44
dgThread::m_id
dgInt32 m_id
Definition: dgThread.h:94
dgThread::m_name
char m_name[32]
Definition: dgThread.h:98
dgThread::GetPriority
dgInt32 GetPriority() const
Definition: dgThread.cpp:210
dgThread::StillBusy
bool StillBusy() const
Definition: dgThread.cpp:51
DG_INLINE
#define DG_INLINE
Definition: dgTypes.h:178
dgInt32
int dgInt32
Definition: dgTypes.h:218
dgThread::dgThread
dgThread()
Definition: dgThread.cpp:26
dgThread
Definition: dgThread.h:32
pthread_mutex_t_
Definition: implement.h:222
dgInterlockedExchange
DG_INLINE dgInt32 dgInterlockedExchange(dgInt32 *const ptr, dgInt32 value)
Definition: dgTypes.h:796
dgThread::dgCriticalSection::Lock
void Lock()
Definition: dgThread.h:102
mutex
pthread_mutex_t mutex
Definition: condvar2.c:82
dgThread::m_threadRunning
dgInt32 m_threadRunning
Definition: dgThread.h:96
ptw32_handle_t
Definition: pthread.h:580
dgThread::dgCriticalSection::~dgCriticalSection
~dgCriticalSection()
Definition: dgThread.cpp:186
dgThread::Close
void Close()
Definition: dgThread.cpp:204
dgThread::SetPriority
void SetPriority(int priority)
Definition: dgThread.cpp:218
dgThread::dgThreadSystemCallback
static void * dgThreadSystemCallback(void *threadData)
Definition: dgThread.cpp:244
dgThread::dgSemaphore
Definition: dgThread.h:51
pthread_mutex_unlock
PTW32_DLLPORT int PTW32_CDECL pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: pthread_mutex_unlock.c:42
dgThreadYield
DG_INLINE void dgThreadYield()
Definition: dgTypes.h:814
dgThread::dgCriticalSection::dgCriticalSection
dgCriticalSection()
Definition: dgThread.cpp:177
dgThread::m_terminate
dgInt32 m_terminate
Definition: dgThread.h:95
dgThread::dgCriticalSection::Unlock
void Unlock()
Definition: dgThread.h:116
dgThread::Execute
virtual void Execute(dgInt32 threadId)=0
dgThread::~dgThread
virtual ~dgThread()
Definition: dgThread.cpp:195
dgThread::Init
void Init()
Definition: dgThread.cpp:199
sem_t_
Definition: implement.h:209
dgThread::SuspendExecution
void SuspendExecution(dgSemaphore &mutex)
Definition: dgThread.cpp:230
dgThread::dgCriticalSection
Definition: dgThread.h:35