Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / Win32ThreadSupport.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2007 Erwin Coumans  http://bulletphysics.com
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #include "LinearMath/btScalar.h"
17 #include "PlatformDefinitions.h"
18
19 #ifdef USE_WIN32_THREADING  //platform specific defines are defined in PlatformDefinitions.h
20
21 #ifndef BT_WIN32_THREAD_SUPPORT_H
22 #define BT_WIN32_THREAD_SUPPORT_H
23
24 #include "LinearMath/btAlignedObjectArray.h"
25
26 #include "btThreadSupportInterface.h"
27
28
29 typedef void (*Win32ThreadFunc)(void* userPtr,void* lsMemory);
30 typedef void* (*Win32lsMemorySetupFunc)();
31
32
33 ///Win32ThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
34 class Win32ThreadSupport : public btThreadSupportInterface 
35 {
36 public:
37         ///placeholder, until libspe2 support is there
38         struct  btSpuStatus
39         {
40                 uint32_t        m_taskId;
41                 uint32_t        m_commandId;
42                 uint32_t        m_status;
43
44                 Win32ThreadFunc m_userThreadFunc;
45                 void*   m_userPtr; //for taskDesc etc
46                 void*   m_lsMemory; //initialized using Win32LocalStoreMemorySetupFunc
47
48                 void*   m_threadHandle; //this one is calling 'Win32ThreadFunc'
49
50                 void*   m_eventStartHandle;
51                 char    m_eventStartHandleName[32];
52
53                 void*   m_eventCompletetHandle;
54                 char    m_eventCompletetHandleName[32];
55                 
56
57         };
58 private:
59
60         btAlignedObjectArray<btSpuStatus>       m_activeSpuStatus;
61         btAlignedObjectArray<void*>                     m_completeHandles;
62         
63         int m_maxNumTasks;
64 public:
65         ///Setup and initialize SPU/CELL/Libspe2
66
67         struct  Win32ThreadConstructionInfo
68         {
69                 Win32ThreadConstructionInfo(const char* uniqueName,
70                                                                         Win32ThreadFunc userThreadFunc,
71                                                                         Win32lsMemorySetupFunc  lsMemoryFunc,
72                                                                         int numThreads=1,
73                                                                         int threadStackSize=65535
74                                                                         )
75                                                                         :m_uniqueName(uniqueName),
76                                                                         m_userThreadFunc(userThreadFunc),
77                                                                         m_lsMemoryFunc(lsMemoryFunc),
78                                                                         m_numThreads(numThreads),
79                                                                         m_threadStackSize(threadStackSize)
80                 {
81
82                 }
83
84                 const char*                             m_uniqueName;
85                 Win32ThreadFunc                 m_userThreadFunc;
86                 Win32lsMemorySetupFunc  m_lsMemoryFunc;
87                 int                                             m_numThreads;
88                 int                                             m_threadStackSize;
89
90         };
91
92
93
94         Win32ThreadSupport(const Win32ThreadConstructionInfo& threadConstructionInfo);
95
96 ///cleanup/shutdown Libspe2
97         virtual ~Win32ThreadSupport();
98
99         void    startThreads(const Win32ThreadConstructionInfo& threadInfo);
100
101
102 ///send messages to SPUs
103         virtual void sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t uiArgument1);
104
105 ///check for messages from SPUs
106         virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
107
108         virtual bool isTaskCompleted(unsigned int *puiArgument0, unsigned int *puiArgument1, int timeOutInMilliseconds);
109
110 ///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
111         virtual void startSPU();
112
113 ///tell the task scheduler we are done with the SPU tasks
114         virtual void stopSPU();
115
116         virtual void    setNumTasks(int numTasks)
117         {
118                 m_maxNumTasks = numTasks;
119         }
120
121         virtual int getNumTasks() const
122         {
123                 return m_maxNumTasks;
124         }
125
126         virtual void*   getThreadLocalMemory(int taskId)
127         {
128                 return m_activeSpuStatus[taskId].m_lsMemory;
129         }
130         virtual btBarrier*      createBarrier();
131
132         virtual btCriticalSection* createCriticalSection();
133
134         virtual void deleteBarrier(btBarrier* barrier);
135
136         virtual void deleteCriticalSection(btCriticalSection* criticalSection);
137 };
138
139 #endif //BT_WIN32_THREAD_SUPPORT_H
140
141 #endif //USE_WIN32_THREADING