as_atomic.cpp 3.5 KB

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