as_atomic.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 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_atomic.cpp
  25. //
  26. // The implementation of the atomic class for thread safe reference counting
  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. // A very high ref count is highly unlikely. It most likely a problem with
  37. // memory that has been overwritten or is being accessed after it was deleted.
  38. asASSERT(value < 1000000);
  39. return value;
  40. }
  41. void asCAtomic::set(asDWORD val)
  42. {
  43. // A very high ref count is highly unlikely. It most likely a problem with
  44. // memory that has been overwritten or is being accessed after it was deleted.
  45. asASSERT(value < 1000000);
  46. value = val;
  47. }
  48. asDWORD asCAtomic::atomicInc()
  49. {
  50. // A very high ref count is highly unlikely. It most likely a problem with
  51. // memory that has been overwritten or is being accessed after it was deleted.
  52. asASSERT(value < 1000000);
  53. return asAtomicInc((int&)value);
  54. }
  55. asDWORD asCAtomic::atomicDec()
  56. {
  57. // A very high ref count is highly unlikely. It most likely a problem with
  58. // memory that has been overwritten or is being accessed after it was deleted.
  59. asASSERT(value < 1000000);
  60. return asAtomicDec((int&)value);
  61. }
  62. //
  63. // The following code implements the atomicInc and atomicDec on different platforms
  64. //
  65. #if defined(AS_NO_THREADS) || defined(AS_NO_ATOMIC)
  66. int asAtomicInc(int &value)
  67. {
  68. return ++value;
  69. }
  70. int asAtomicDec(int &value)
  71. {
  72. return --value;
  73. }
  74. #elif defined(AS_XENON) /// XBox360
  75. END_AS_NAMESPACE
  76. #include <xtl.h>
  77. BEGIN_AS_NAMESPACE
  78. int asAtomicInc(int &value)
  79. {
  80. return InterlockedIncrement((LONG*)&value);
  81. }
  82. int asAtomicDec(int &value)
  83. {
  84. return InterlockedDecrement((LONG*)&value);
  85. }
  86. #elif defined(AS_WIN)
  87. END_AS_NAMESPACE
  88. #define WIN32_MEAN_AND_LEAN
  89. #include <windows.h>
  90. BEGIN_AS_NAMESPACE
  91. int asAtomicInc(int &value)
  92. {
  93. return InterlockedIncrement((LONG*)&value);
  94. }
  95. int asAtomicDec(int &value)
  96. {
  97. asASSERT(value > 0);
  98. return InterlockedDecrement((LONG*)&value);
  99. }
  100. #elif defined(AS_LINUX) || defined(AS_BSD) || defined(AS_ILLUMOS) || defined(AS_ANDROID)
  101. //
  102. // atomic_inc_and_test() and atomic_dec_and_test() from asm/atomic.h is not meant
  103. // to be used outside the Linux kernel. Instead we should use the GNUC provided
  104. // __sync_add_and_fetch() and __sync_sub_and_fetch() functions.
  105. //
  106. // Reference: http://golubenco.org/blog/atomic-operations/
  107. //
  108. // These are only available in GCC 4.1 and above, so for older versions we
  109. // use the critical sections, though it is a lot slower.
  110. //
  111. int asAtomicInc(int &value)
  112. {
  113. return __sync_add_and_fetch(&value, 1);
  114. }
  115. int asAtomicDec(int &value)
  116. {
  117. return __sync_sub_and_fetch(&value, 1);
  118. }
  119. #elif defined(AS_MAC) || defined(AS_IPHONE)
  120. END_AS_NAMESPACE
  121. #include <libkern/OSAtomic.h>
  122. BEGIN_AS_NAMESPACE
  123. int asAtomicInc(int &value)
  124. {
  125. return OSAtomicIncrement32((int32_t*)&value);
  126. }
  127. int asAtomicDec(int &value)
  128. {
  129. return OSAtomicDecrement32((int32_t*)&value);
  130. }
  131. #else
  132. // If we get here, then the configuration in as_config.h
  133. // is wrong for the compiler/platform combination.
  134. int ERROR_PleaseFixTheConfig[-1];
  135. #endif
  136. END_AS_NAMESPACE