safe_refcount.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*************************************************************************/
  2. /* safe_refcount.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "safe_refcount.h"
  31. #if defined(_MSC_VER)
  32. /* Implementation for MSVC-Windows */
  33. // don't pollute my namespace!
  34. #include <windows.h>
  35. #define ATOMIC_CONDITIONAL_INCREMENT_BODY(m_pw, m_win_type, m_win_cmpxchg, m_cpp_type) \
  36. /* try to increment until it actually works */ \
  37. /* taken from boost */ \
  38. while (true) { \
  39. m_cpp_type tmp = static_cast<m_cpp_type const volatile &>(*(m_pw)); \
  40. if (tmp == 0) \
  41. return 0; /* if zero, can't add to it anymore */ \
  42. if (m_win_cmpxchg((m_win_type volatile *)(m_pw), tmp + 1, tmp) == tmp) \
  43. return tmp + 1; \
  44. }
  45. #define ATOMIC_EXCHANGE_IF_GREATER_BODY(m_pw, m_val, m_win_type, m_win_cmpxchg, m_cpp_type) \
  46. while (true) { \
  47. m_cpp_type tmp = static_cast<m_cpp_type const volatile &>(*(m_pw)); \
  48. if (tmp >= m_val) \
  49. return tmp; /* already greater, or equal */ \
  50. if (m_win_cmpxchg((m_win_type volatile *)(m_pw), m_val, tmp) == tmp) \
  51. return m_val; \
  52. }
  53. _ALWAYS_INLINE_ uint32_t _atomic_conditional_increment_impl(volatile uint32_t *pw){
  54. ATOMIC_CONDITIONAL_INCREMENT_BODY(pw, LONG, InterlockedCompareExchange, uint32_t)
  55. }
  56. _ALWAYS_INLINE_ uint32_t _atomic_decrement_impl(volatile uint32_t *pw) {
  57. return InterlockedDecrement((LONG volatile *)pw);
  58. }
  59. _ALWAYS_INLINE_ uint32_t _atomic_increment_impl(volatile uint32_t *pw) {
  60. return InterlockedIncrement((LONG volatile *)pw);
  61. }
  62. _ALWAYS_INLINE_ uint32_t _atomic_sub_impl(volatile uint32_t *pw, volatile uint32_t val) {
  63. return InterlockedExchangeAdd((LONG volatile *)pw, -(int32_t)val) - val;
  64. }
  65. _ALWAYS_INLINE_ uint32_t _atomic_add_impl(volatile uint32_t *pw, volatile uint32_t val) {
  66. return InterlockedAdd((LONG volatile *)pw, val);
  67. }
  68. _ALWAYS_INLINE_ uint32_t _atomic_exchange_if_greater_impl(volatile uint32_t *pw, volatile uint32_t val){
  69. ATOMIC_EXCHANGE_IF_GREATER_BODY(pw, val, LONG, InterlockedCompareExchange, uint32_t)
  70. }
  71. _ALWAYS_INLINE_ uint64_t _atomic_conditional_increment_impl(volatile uint64_t *pw){
  72. ATOMIC_CONDITIONAL_INCREMENT_BODY(pw, LONGLONG, InterlockedCompareExchange64, uint64_t)
  73. }
  74. _ALWAYS_INLINE_ uint64_t _atomic_decrement_impl(volatile uint64_t *pw) {
  75. return InterlockedDecrement64((LONGLONG volatile *)pw);
  76. }
  77. _ALWAYS_INLINE_ uint64_t _atomic_increment_impl(volatile uint64_t *pw) {
  78. return InterlockedIncrement64((LONGLONG volatile *)pw);
  79. }
  80. _ALWAYS_INLINE_ uint64_t _atomic_sub_impl(volatile uint64_t *pw, volatile uint64_t val) {
  81. return InterlockedExchangeAdd64((LONGLONG volatile *)pw, -(int64_t)val) - val;
  82. }
  83. _ALWAYS_INLINE_ uint64_t _atomic_add_impl(volatile uint64_t *pw, volatile uint64_t val) {
  84. return InterlockedAdd64((LONGLONG volatile *)pw, val);
  85. }
  86. _ALWAYS_INLINE_ uint64_t _atomic_exchange_if_greater_impl(volatile uint64_t *pw, volatile uint64_t val){
  87. ATOMIC_EXCHANGE_IF_GREATER_BODY(pw, val, LONGLONG, InterlockedCompareExchange64, uint64_t)
  88. }
  89. // The actual advertised functions; they'll call the right implementation
  90. uint32_t atomic_conditional_increment(volatile uint32_t *pw) {
  91. return _atomic_conditional_increment_impl(pw);
  92. }
  93. uint32_t atomic_decrement(volatile uint32_t *pw) {
  94. return _atomic_decrement_impl(pw);
  95. }
  96. uint32_t atomic_increment(volatile uint32_t *pw) {
  97. return _atomic_increment_impl(pw);
  98. }
  99. uint32_t atomic_sub(volatile uint32_t *pw, volatile uint32_t val) {
  100. return _atomic_sub_impl(pw, val);
  101. }
  102. uint32_t atomic_add(volatile uint32_t *pw, volatile uint32_t val) {
  103. return _atomic_add_impl(pw, val);
  104. }
  105. uint32_t atomic_exchange_if_greater(volatile uint32_t *pw, volatile uint32_t val) {
  106. return _atomic_exchange_if_greater_impl(pw, val);
  107. }
  108. uint64_t atomic_conditional_increment(volatile uint64_t *pw) {
  109. return _atomic_conditional_increment_impl(pw);
  110. }
  111. uint64_t atomic_decrement(volatile uint64_t *pw) {
  112. return _atomic_decrement_impl(pw);
  113. }
  114. uint64_t atomic_increment(volatile uint64_t *pw) {
  115. return _atomic_increment_impl(pw);
  116. }
  117. uint64_t atomic_sub(volatile uint64_t *pw, volatile uint64_t val) {
  118. return _atomic_sub_impl(pw, val);
  119. }
  120. uint64_t atomic_add(volatile uint64_t *pw, volatile uint64_t val) {
  121. return _atomic_add_impl(pw, val);
  122. }
  123. uint64_t atomic_exchange_if_greater(volatile uint64_t *pw, volatile uint64_t val) {
  124. return _atomic_exchange_if_greater_impl(pw, val);
  125. }
  126. #endif