safe_refcount.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*************************************************************************/
  2. /* safe_refcount.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #ifndef SAFE_REFCOUNT_H
  31. #define SAFE_REFCOUNT_H
  32. #include "core/os/mutex.h"
  33. #include "core/typedefs.h"
  34. #include "platform_config.h"
  35. // Atomic functions, these are used for multithread safe reference counters!
  36. #ifdef NO_THREADS
  37. /* Bogus implementation unaware of multiprocessing */
  38. template <class T>
  39. static _ALWAYS_INLINE_ T atomic_conditional_increment(volatile T *pw) {
  40. if (*pw == 0)
  41. return 0;
  42. (*pw)++;
  43. return *pw;
  44. }
  45. template <class T>
  46. static _ALWAYS_INLINE_ T atomic_decrement(volatile T *pw) {
  47. (*pw)--;
  48. return *pw;
  49. }
  50. template <class T>
  51. static _ALWAYS_INLINE_ T atomic_increment(volatile T *pw) {
  52. (*pw)++;
  53. return *pw;
  54. }
  55. template <class T, class V>
  56. static _ALWAYS_INLINE_ T atomic_sub(volatile T *pw, volatile V val) {
  57. (*pw) -= val;
  58. return *pw;
  59. }
  60. template <class T, class V>
  61. static _ALWAYS_INLINE_ T atomic_add(volatile T *pw, volatile V val) {
  62. (*pw) += val;
  63. return *pw;
  64. }
  65. template <class T, class V>
  66. static _ALWAYS_INLINE_ T atomic_exchange_if_greater(volatile T *pw, volatile V val) {
  67. if (val > *pw)
  68. *pw = val;
  69. return *pw;
  70. }
  71. #elif defined(__GNUC__)
  72. /* Implementation for GCC & Clang */
  73. // GCC guarantees atomic intrinsics for sizes of 1, 2, 4 and 8 bytes.
  74. // Clang states it supports GCC atomic builtins.
  75. template <class T>
  76. static _ALWAYS_INLINE_ T atomic_conditional_increment(volatile T *pw) {
  77. while (true) {
  78. T tmp = static_cast<T const volatile &>(*pw);
  79. if (tmp == 0)
  80. return 0; // if zero, can't add to it anymore
  81. if (__sync_val_compare_and_swap(pw, tmp, tmp + 1) == tmp)
  82. return tmp + 1;
  83. }
  84. }
  85. template <class T>
  86. static _ALWAYS_INLINE_ T atomic_decrement(volatile T *pw) {
  87. return __sync_sub_and_fetch(pw, 1);
  88. }
  89. template <class T>
  90. static _ALWAYS_INLINE_ T atomic_increment(volatile T *pw) {
  91. return __sync_add_and_fetch(pw, 1);
  92. }
  93. template <class T, class V>
  94. static _ALWAYS_INLINE_ T atomic_sub(volatile T *pw, volatile V val) {
  95. return __sync_sub_and_fetch(pw, val);
  96. }
  97. template <class T, class V>
  98. static _ALWAYS_INLINE_ T atomic_add(volatile T *pw, volatile V val) {
  99. return __sync_add_and_fetch(pw, val);
  100. }
  101. template <class T, class V>
  102. static _ALWAYS_INLINE_ T atomic_exchange_if_greater(volatile T *pw, volatile V val) {
  103. while (true) {
  104. T tmp = static_cast<T const volatile &>(*pw);
  105. if (tmp >= val)
  106. return tmp; // already greater, or equal
  107. if (__sync_val_compare_and_swap(pw, tmp, val) == tmp)
  108. return val;
  109. }
  110. }
  111. #elif defined(_MSC_VER)
  112. // For MSVC use a separate compilation unit to prevent windows.h from polluting
  113. // the global namespace.
  114. uint32_t atomic_conditional_increment(volatile uint32_t *pw);
  115. uint32_t atomic_decrement(volatile uint32_t *pw);
  116. uint32_t atomic_increment(volatile uint32_t *pw);
  117. uint32_t atomic_sub(volatile uint32_t *pw, volatile uint32_t val);
  118. uint32_t atomic_add(volatile uint32_t *pw, volatile uint32_t val);
  119. uint32_t atomic_exchange_if_greater(volatile uint32_t *pw, volatile uint32_t val);
  120. uint64_t atomic_conditional_increment(volatile uint64_t *pw);
  121. uint64_t atomic_decrement(volatile uint64_t *pw);
  122. uint64_t atomic_increment(volatile uint64_t *pw);
  123. uint64_t atomic_sub(volatile uint64_t *pw, volatile uint64_t val);
  124. uint64_t atomic_add(volatile uint64_t *pw, volatile uint64_t val);
  125. uint64_t atomic_exchange_if_greater(volatile uint64_t *pw, volatile uint64_t val);
  126. #else
  127. //no threads supported?
  128. #error Must provide atomic functions for this platform or compiler!
  129. #endif
  130. struct SafeRefCount {
  131. uint32_t count;
  132. public:
  133. // destroy() is called when weak_count_ drops to zero.
  134. _ALWAYS_INLINE_ bool ref() { //true on success
  135. return atomic_conditional_increment(&count) != 0;
  136. }
  137. _ALWAYS_INLINE_ uint32_t refval() { //true on success
  138. return atomic_conditional_increment(&count);
  139. }
  140. _ALWAYS_INLINE_ bool unref() { // true if must be disposed of
  141. if (atomic_decrement(&count) == 0) {
  142. return true;
  143. }
  144. return false;
  145. }
  146. _ALWAYS_INLINE_ uint32_t get() const { // nothrow
  147. return count;
  148. }
  149. _ALWAYS_INLINE_ void init(uint32_t p_value = 1) {
  150. count = p_value;
  151. }
  152. };
  153. #endif