as_atomic.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2011 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. // Modified by Lasse Öörni for Urho3D
  24. //
  25. // as_gc.cpp
  26. //
  27. // The implementation of the garbage collector
  28. //
  29. #include "as_atomic.h"
  30. BEGIN_AS_NAMESPACE
  31. asCAtomic::asCAtomic()
  32. {
  33. value = 0;
  34. }
  35. asDWORD asCAtomic::get() const
  36. {
  37. return value;
  38. }
  39. void asCAtomic::set(asDWORD val)
  40. {
  41. value = val;
  42. }
  43. //
  44. // The following code implements the atomicInc and atomicDec on different platforms
  45. //
  46. #ifdef AS_NO_THREADS
  47. asDWORD asCAtomic::atomicInc()
  48. {
  49. return ++value;
  50. }
  51. asDWORD asCAtomic::atomicDec()
  52. {
  53. return --value;
  54. }
  55. #elif defined(AS_NO_ATOMIC)
  56. asDWORD asCAtomic::atomicInc()
  57. {
  58. asDWORD v;
  59. ENTERCRITICALSECTION(cs);
  60. v = ++value;
  61. LEAVECRITICALSECTION(cs);
  62. return v;
  63. }
  64. asDWORD asCAtomic::atomicDec()
  65. {
  66. asDWORD v;
  67. ENTERCRITICALSECTION(cs);
  68. v = --value;
  69. LEAVECRITICALSECTION(cs);
  70. return v;
  71. }
  72. #elif defined(AS_XENON) /// XBox360
  73. END_AS_NAMESPACE
  74. #include <xtl.h>
  75. BEGIN_AS_NAMESPACE
  76. asDWORD asCAtomic::atomicInc()
  77. {
  78. return InterlockedIncrement((LONG*)&value);
  79. }
  80. asDWORD asCAtomic::atomicDec()
  81. {
  82. return InterlockedDecrement((LONG*)&value);
  83. }
  84. #elif defined(AS_WIN)
  85. END_AS_NAMESPACE
  86. #define WIN32_MEAN_AND_LEAN
  87. #include <windows.h>
  88. BEGIN_AS_NAMESPACE
  89. asDWORD asCAtomic::atomicInc()
  90. {
  91. return InterlockedIncrement((LONG*)&value);
  92. }
  93. asDWORD asCAtomic::atomicDec()
  94. {
  95. asASSERT(value > 0);
  96. return InterlockedDecrement((LONG*)&value);
  97. }
  98. #elif defined(AS_LINUX) || defined(AS_BSD) || defined(AS_ILLUMOS)
  99. //
  100. // atomic_inc_and_test() and atomic_dec_and_test() from asm/atomic.h is not meant
  101. // to be used outside the Linux kernel. Instead we should use the GNUC provided
  102. // __sync_add_and_fetch() and __sync_sub_and_fetch() functions.
  103. //
  104. // Reference: http://golubenco.org/blog/atomic-operations/
  105. //
  106. // These are only available in GCC 4.1 and above, so for older versions we
  107. // use the critical sections, though it is a lot slower.
  108. //
  109. asDWORD asCAtomic::atomicInc()
  110. {
  111. return __sync_add_and_fetch(&value, 1);
  112. }
  113. asDWORD asCAtomic::atomicDec()
  114. {
  115. return __sync_sub_and_fetch(&value, 1);
  116. }
  117. #elif defined(AS_MAC) || defined(AS_IPHONE)
  118. END_AS_NAMESPACE
  119. #include <libkern/OSAtomic.h>
  120. BEGIN_AS_NAMESPACE
  121. asDWORD asCAtomic::atomicInc()
  122. {
  123. return OSAtomicIncrement32((int32_t*)&value);
  124. }
  125. asDWORD asCAtomic::atomicDec()
  126. {
  127. return OSAtomicDecrement32((int32_t*)&value);
  128. }
  129. #else
  130. // If we get here, then the configuration in as_config.h
  131. // is wrong for the compiler/platform combination.
  132. int ERROR_PleaseFixTheConfig[-1];
  133. #endif
  134. END_AS_NAMESPACE