SpuSync.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2007 Starbreeze Studios
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 
00015 Written by: Marten Svanfeldt
00016 */
00017 
00018 #ifndef BT_SPU_SYNC_H
00019 #define BT_SPU_SYNC_H
00020 
00021 
00022 #include "PlatformDefinitions.h"
00023 
00024 
00025 #if defined(WIN32)
00026 
00027 #define WIN32_LEAN_AND_MEAN
00028 #ifdef _XBOX
00029 #include <Xtl.h>
00030 #else
00031 #include <Windows.h>
00032 #endif
00033 
00035 class btSpinlock
00036 {
00037 public:
00038         //typedef volatile LONG SpinVariable;
00039         typedef CRITICAL_SECTION SpinVariable;
00040 
00041         btSpinlock (SpinVariable* var)
00042                 : spinVariable (var)
00043         {}
00044 
00045         void Init ()
00046         {
00047                 //*spinVariable = 0;
00048                 InitializeCriticalSection(spinVariable);
00049         }
00050 
00051         void Lock ()
00052         {
00053                 EnterCriticalSection(spinVariable);
00054         }
00055 
00056         void Unlock ()
00057         {
00058                 LeaveCriticalSection(spinVariable);
00059         }
00060 
00061 private:
00062         SpinVariable* spinVariable;
00063 };
00064 
00065 
00066 #elif defined (__CELLOS_LV2__)
00067 
00068 //#include <cell/atomic.h>
00069 #include <cell/sync/mutex.h>
00070 
00072 class btSpinlock
00073 {
00074 public:
00075         typedef CellSyncMutex SpinVariable;
00076 
00077         btSpinlock (SpinVariable* var)
00078                 : spinVariable (var)
00079         {}
00080 
00081         void Init ()
00082         {
00083 #ifndef __SPU__
00084                 //*spinVariable = 1;
00085                 cellSyncMutexInitialize(spinVariable);
00086 #endif
00087         }
00088 
00089 
00090 
00091         void Lock ()
00092         {
00093 #ifdef __SPU__
00094                 // lock semaphore
00095                 /*while (cellAtomicTestAndDecr32(atomic_buf, (uint64_t)spinVariable) == 0) 
00096                 {
00097 
00098                 };*/
00099                 cellSyncMutexLock((uint64_t)spinVariable);
00100 #endif
00101         }
00102 
00103         void Unlock ()
00104         {
00105 #ifdef __SPU__
00106                 //cellAtomicIncr32(atomic_buf, (uint64_t)spinVariable);
00107                 cellSyncMutexUnlock((uint64_t)spinVariable);
00108 #endif 
00109         }
00110 
00111 
00112 private:
00113         SpinVariable*   spinVariable;
00114         ATTRIBUTE_ALIGNED128(uint32_t           atomic_buf[32]);
00115 };
00116 
00117 #else
00118 //create a dummy implementation (without any locking) useful for serial processing
00119 class btSpinlock
00120 {
00121 public:
00122         typedef int  SpinVariable;
00123 
00124         btSpinlock (SpinVariable* var)
00125                 : spinVariable (var)
00126         {}
00127 
00128         void Init ()
00129         {
00130         }
00131 
00132         void Lock ()
00133         {
00134         }
00135 
00136         void Unlock ()
00137         {
00138         }
00139 
00140 private:
00141         SpinVariable* spinVariable;
00142 };
00143 
00144 
00145 #endif
00146 
00147 
00148 #endif //BT_SPU_SYNC_H
00149