safe_refcount.h 6.1 KB

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