Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / SpuSync.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2007 Starbreeze Studios
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 Written by: Marten Svanfeldt
16 */
17
18 #ifndef BT_SPU_SYNC_H
19 #define BT_SPU_SYNC_H
20
21
22 #include "PlatformDefinitions.h"
23
24
25 #if defined(WIN32)
26
27 #define WIN32_LEAN_AND_MEAN
28 #ifdef _XBOX
29 #include <Xtl.h>
30 #else
31 #include <Windows.h>
32 #endif
33
34 ///The btSpinlock is a structure to allow multi-platform synchronization. This allows to port the SPU tasks to other platforms.
35 class btSpinlock
36 {
37 public:
38         //typedef volatile LONG SpinVariable;
39         typedef CRITICAL_SECTION SpinVariable;
40
41         btSpinlock (SpinVariable* var)
42                 : spinVariable (var)
43         {}
44
45         void Init ()
46         {
47                 //*spinVariable = 0;
48                 InitializeCriticalSection(spinVariable);
49         }
50
51         void Lock ()
52         {
53                 EnterCriticalSection(spinVariable);
54         }
55
56         void Unlock ()
57         {
58                 LeaveCriticalSection(spinVariable);
59         }
60
61 private:
62         SpinVariable* spinVariable;
63 };
64
65
66 #elif defined (__CELLOS_LV2__)
67
68 //#include <cell/atomic.h>
69 #include <cell/sync/mutex.h>
70
71 ///The btSpinlock is a structure to allow multi-platform synchronization. This allows to port the SPU tasks to other platforms.
72 class btSpinlock
73 {
74 public:
75         typedef CellSyncMutex SpinVariable;
76
77         btSpinlock (SpinVariable* var)
78                 : spinVariable (var)
79         {}
80
81         void Init ()
82         {
83 #ifndef __SPU__
84                 //*spinVariable = 1;
85                 cellSyncMutexInitialize(spinVariable);
86 #endif
87         }
88
89
90
91         void Lock ()
92         {
93 #ifdef __SPU__
94                 // lock semaphore
95                 /*while (cellAtomicTestAndDecr32(atomic_buf, (uint64_t)spinVariable) == 0) 
96                 {
97
98                 };*/
99                 cellSyncMutexLock((uint64_t)spinVariable);
100 #endif
101         }
102
103         void Unlock ()
104         {
105 #ifdef __SPU__
106                 //cellAtomicIncr32(atomic_buf, (uint64_t)spinVariable);
107                 cellSyncMutexUnlock((uint64_t)spinVariable);
108 #endif 
109         }
110
111
112 private:
113         SpinVariable*   spinVariable;
114         ATTRIBUTE_ALIGNED128(uint32_t           atomic_buf[32]);
115 };
116
117 #else
118 //create a dummy implementation (without any locking) useful for serial processing
119 class btSpinlock
120 {
121 public:
122         typedef int  SpinVariable;
123
124         btSpinlock (SpinVariable* var)
125                 : spinVariable (var)
126         {}
127
128         void Init ()
129         {
130         }
131
132         void Lock ()
133         {
134         }
135
136         void Unlock ()
137         {
138         }
139
140 private:
141         SpinVariable* spinVariable;
142 };
143
144
145 #endif
146
147
148 #endif //BT_SPU_SYNC_H
149