as_atomic.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 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. //
  24. // as_gc.cpp
  25. //
  26. // The implementation of the garbage collector
  27. //
  28. #include "as_atomic.h"
  29. BEGIN_AS_NAMESPACE
  30. asCAtomic::asCAtomic()
  31. {
  32. value = 0;
  33. }
  34. asDWORD asCAtomic::get() const
  35. {
  36. return value;
  37. }
  38. void asCAtomic::set(asDWORD val)
  39. {
  40. value = val;
  41. }
  42. //
  43. // The following code implements the atomicInc and atomicDec on different platforms
  44. //
  45. #ifdef AS_NO_THREADS
  46. asDWORD asCAtomic::atomicInc()
  47. {
  48. return ++value;
  49. }
  50. asDWORD asCAtomic::atomicDec()
  51. {
  52. return --value;
  53. }
  54. #elif defined(AS_NO_ATOMIC)
  55. asDWORD asCAtomic::atomicInc()
  56. {
  57. asDWORD v;
  58. ENTERCRITICALSECTION(cs);
  59. v = ++value;
  60. LEAVECRITICALSECTION(cs);
  61. return v;
  62. }
  63. asDWORD asCAtomic::atomicDec()
  64. {
  65. asDWORD v;
  66. ENTERCRITICALSECTION(cs);
  67. v = --value;
  68. LEAVECRITICALSECTION(cs);
  69. return v;
  70. }
  71. #elif defined(AS_XENON) /// XBox360
  72. END_AS_NAMESPACE
  73. #include <xtl.h>
  74. BEGIN_AS_NAMESPACE
  75. asDWORD asCAtomic::atomicInc()
  76. {
  77. return InterlockedIncrement((LONG*)&value);
  78. }
  79. asDWORD asCAtomic::atomicDec()
  80. {
  81. return InterlockedDecrement((LONG*)&value);
  82. }
  83. #elif defined(AS_WIN)
  84. END_AS_NAMESPACE
  85. #define WIN32_MEAN_AND_LEAN
  86. #include <windows.h>
  87. BEGIN_AS_NAMESPACE
  88. asDWORD asCAtomic::atomicInc()
  89. {
  90. return InterlockedIncrement((LONG*)&value);
  91. }
  92. asDWORD asCAtomic::atomicDec()
  93. {
  94. asASSERT(value > 0);
  95. return InterlockedDecrement((LONG*)&value);
  96. }
  97. #elif defined(AS_LINUX) || defined(AS_BSD) || defined(AS_ILLUMOS)
  98. //
  99. // atomic_inc_and_test() and atomic_dec_and_test() from asm/atomic.h is not meant
  100. // to be used outside the Linux kernel. Instead we should use the GNUC provided
  101. // __sync_add_and_fetch() and __sync_sub_and_fetch() functions.
  102. //
  103. // Reference: http://golubenco.org/blog/atomic-operations/
  104. //
  105. // These are only available in GCC 4.1 and above, so for older versions we
  106. // use the critical sections, though it is a lot slower.
  107. //
  108. asDWORD asCAtomic::atomicInc()
  109. {
  110. return __sync_add_and_fetch(&value, 1);
  111. }
  112. asDWORD asCAtomic::atomicDec()
  113. {
  114. return __sync_sub_and_fetch(&value, 1);
  115. }
  116. #elif defined(AS_MAC) || defined(AS_IPHONE)
  117. END_AS_NAMESPACE
  118. #include <libkern/OSAtomic.h>
  119. BEGIN_AS_NAMESPACE
  120. asDWORD asCAtomic::atomicInc()
  121. {
  122. return OSAtomicIncrement32((int32_t*)&value);
  123. }
  124. asDWORD asCAtomic::atomicDec()
  125. {
  126. return OSAtomicDecrement32((int32_t*)&value);
  127. }
  128. #else
  129. // If we get here, then the configuration in as_config.h
  130. // is wrong for the compiler/platform combination.
  131. int ERROR_PleaseFixTheConfig[-1];
  132. #endif
  133. END_AS_NAMESPACE