SpuSync.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2007 Starbreeze Studios
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. Written by: Marten Svanfeldt
  13. */
  14. #ifndef BT_SPU_SYNC_H
  15. #define BT_SPU_SYNC_H
  16. #include "PlatformDefinitions.h"
  17. #if defined(WIN32)
  18. #define WIN32_LEAN_AND_MEAN
  19. #ifdef _XBOX
  20. #include <Xtl.h>
  21. #else
  22. #include <Windows.h>
  23. #endif
  24. ///The btSpinlock is a structure to allow multi-platform synchronization. This allows to port the SPU tasks to other platforms.
  25. class btSpinlock
  26. {
  27. public:
  28. //typedef volatile LONG SpinVariable;
  29. typedef CRITICAL_SECTION SpinVariable;
  30. btSpinlock (SpinVariable* var)
  31. : spinVariable (var)
  32. {}
  33. void Init ()
  34. {
  35. //*spinVariable = 0;
  36. InitializeCriticalSection(spinVariable);
  37. }
  38. void Lock ()
  39. {
  40. EnterCriticalSection(spinVariable);
  41. }
  42. void Unlock ()
  43. {
  44. LeaveCriticalSection(spinVariable);
  45. }
  46. private:
  47. SpinVariable* spinVariable;
  48. };
  49. #elif defined (__CELLOS_LV2__)
  50. //#include <cell/atomic.h>
  51. #include <cell/sync/mutex.h>
  52. ///The btSpinlock is a structure to allow multi-platform synchronization. This allows to port the SPU tasks to other platforms.
  53. class btSpinlock
  54. {
  55. public:
  56. typedef CellSyncMutex SpinVariable;
  57. btSpinlock (SpinVariable* var)
  58. : spinVariable (var)
  59. {}
  60. void Init ()
  61. {
  62. #ifndef __SPU__
  63. //*spinVariable = 1;
  64. cellSyncMutexInitialize(spinVariable);
  65. #endif
  66. }
  67. void Lock ()
  68. {
  69. #ifdef __SPU__
  70. // lock semaphore
  71. /*while (cellAtomicTestAndDecr32(atomic_buf, (uint64_t)spinVariable) == 0)
  72. {
  73. };*/
  74. cellSyncMutexLock((uint64_t)spinVariable);
  75. #endif
  76. }
  77. void Unlock ()
  78. {
  79. #ifdef __SPU__
  80. //cellAtomicIncr32(atomic_buf, (uint64_t)spinVariable);
  81. cellSyncMutexUnlock((uint64_t)spinVariable);
  82. #endif
  83. }
  84. private:
  85. SpinVariable* spinVariable;
  86. ATTRIBUTE_ALIGNED128(uint32_t atomic_buf[32]);
  87. };
  88. #else
  89. //create a dummy implementation (without any locking) useful for serial processing
  90. class btSpinlock
  91. {
  92. public:
  93. typedef int SpinVariable;
  94. btSpinlock (SpinVariable* var)
  95. : spinVariable (var)
  96. {}
  97. void Init ()
  98. {
  99. }
  100. void Lock ()
  101. {
  102. }
  103. void Unlock ()
  104. {
  105. }
  106. private:
  107. SpinVariable* spinVariable;
  108. };
  109. #endif
  110. #endif //BT_SPU_SYNC_H