safe_refcount.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*************************************************************************/
  2. /* safe_refcount.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/typedefs.h"
  33. #if !defined(NO_THREADS)
  34. #include <atomic>
  35. #include <type_traits>
  36. // Design goals for these classes:
  37. // - No automatic conversions or arithmetic operators,
  38. // to keep explicit the use of atomics everywhere.
  39. // - Using acquire-release semantics, even to set the first value.
  40. // The first value may be set relaxedly in many cases, but adding the distinction
  41. // between relaxed and unrelaxed operation to the interface would make it needlessly
  42. // flexible. There's negligible waste in having release semantics for the initial
  43. // value and, as an important benefit, you can be sure the value is properly synchronized
  44. // even with threads that are already running.
  45. // This is used in very specific areas of the engine where it's critical that these guarantees are held
  46. #define SAFE_NUMERIC_TYPE_PUN_GUARANTEES(m_type) \
  47. static_assert(sizeof(SafeNumeric<m_type>) == sizeof(m_type)); \
  48. static_assert(alignof(SafeNumeric<m_type>) == alignof(m_type)); \
  49. static_assert(std::is_trivially_destructible<std::atomic<m_type>>::value);
  50. template <class T>
  51. class SafeNumeric {
  52. std::atomic<T> value;
  53. static_assert(std::atomic<T>::is_always_lock_free);
  54. public:
  55. _ALWAYS_INLINE_ void set(T p_value) {
  56. value.store(p_value, std::memory_order_release);
  57. }
  58. _ALWAYS_INLINE_ T get() const {
  59. return value.load(std::memory_order_acquire);
  60. }
  61. _ALWAYS_INLINE_ T increment() {
  62. return value.fetch_add(1, std::memory_order_acq_rel) + 1;
  63. }
  64. // Returns the original value instead of the new one
  65. _ALWAYS_INLINE_ T postincrement() {
  66. return value.fetch_add(1, std::memory_order_acq_rel);
  67. }
  68. _ALWAYS_INLINE_ T decrement() {
  69. return value.fetch_sub(1, std::memory_order_acq_rel) - 1;
  70. }
  71. // Returns the original value instead of the new one
  72. _ALWAYS_INLINE_ T postdecrement() {
  73. return value.fetch_sub(1, std::memory_order_acq_rel);
  74. }
  75. _ALWAYS_INLINE_ T add(T p_value) {
  76. return value.fetch_add(p_value, std::memory_order_acq_rel) + p_value;
  77. }
  78. // Returns the original value instead of the new one
  79. _ALWAYS_INLINE_ T postadd(T p_value) {
  80. return value.fetch_add(p_value, std::memory_order_acq_rel);
  81. }
  82. _ALWAYS_INLINE_ T sub(T p_value) {
  83. return value.fetch_sub(p_value, std::memory_order_acq_rel) - p_value;
  84. }
  85. // Returns the original value instead of the new one
  86. _ALWAYS_INLINE_ T postsub(T p_value) {
  87. return value.fetch_sub(p_value, std::memory_order_acq_rel);
  88. }
  89. _ALWAYS_INLINE_ T exchange_if_greater(T p_value) {
  90. while (true) {
  91. T tmp = value.load(std::memory_order_acquire);
  92. if (tmp >= p_value) {
  93. return tmp; // already greater, or equal
  94. }
  95. if (value.compare_exchange_weak(tmp, p_value, std::memory_order_release)) {
  96. return p_value;
  97. }
  98. }
  99. }
  100. _ALWAYS_INLINE_ T conditional_increment() {
  101. while (true) {
  102. T c = value.load(std::memory_order_acquire);
  103. if (c == 0) {
  104. return 0;
  105. }
  106. if (value.compare_exchange_weak(c, c + 1, std::memory_order_release)) {
  107. return c + 1;
  108. }
  109. }
  110. }
  111. _ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) {
  112. set(p_value);
  113. }
  114. };
  115. class SafeFlag {
  116. std::atomic_bool flag;
  117. static_assert(std::atomic_bool::is_always_lock_free);
  118. public:
  119. _ALWAYS_INLINE_ bool is_set() const {
  120. return flag.load(std::memory_order_acquire);
  121. }
  122. _ALWAYS_INLINE_ void set() {
  123. flag.store(true, std::memory_order_release);
  124. }
  125. _ALWAYS_INLINE_ void clear() {
  126. flag.store(false, std::memory_order_release);
  127. }
  128. _ALWAYS_INLINE_ void set_to(bool p_value) {
  129. flag.store(p_value, std::memory_order_release);
  130. }
  131. _ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) {
  132. set_to(p_value);
  133. }
  134. };
  135. class SafeRefCount {
  136. SafeNumeric<uint32_t> count;
  137. public:
  138. _ALWAYS_INLINE_ bool ref() { // true on success
  139. return count.conditional_increment() != 0;
  140. }
  141. _ALWAYS_INLINE_ uint32_t refval() { // none-zero on success
  142. return count.conditional_increment();
  143. }
  144. _ALWAYS_INLINE_ bool unref() { // true if must be disposed of
  145. return count.decrement() == 0;
  146. }
  147. _ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
  148. return count.decrement();
  149. }
  150. _ALWAYS_INLINE_ uint32_t get() const {
  151. return count.get();
  152. }
  153. _ALWAYS_INLINE_ void init(uint32_t p_value = 1) {
  154. count.set(p_value);
  155. }
  156. };
  157. #else
  158. template <class T>
  159. class SafeNumeric {
  160. protected:
  161. T value;
  162. public:
  163. _ALWAYS_INLINE_ void set(T p_value) {
  164. value = p_value;
  165. }
  166. _ALWAYS_INLINE_ T get() const {
  167. return value;
  168. }
  169. _ALWAYS_INLINE_ T increment() {
  170. return ++value;
  171. }
  172. _ALWAYS_INLINE_ T postincrement() {
  173. return value++;
  174. }
  175. _ALWAYS_INLINE_ T decrement() {
  176. return --value;
  177. }
  178. _ALWAYS_INLINE_ T postdecrement() {
  179. return value--;
  180. }
  181. _ALWAYS_INLINE_ T add(T p_value) {
  182. return value += p_value;
  183. }
  184. _ALWAYS_INLINE_ T postadd(T p_value) {
  185. T old = value;
  186. value += p_value;
  187. return old;
  188. }
  189. _ALWAYS_INLINE_ T sub(T p_value) {
  190. return value -= p_value;
  191. }
  192. _ALWAYS_INLINE_ T postsub(T p_value) {
  193. T old = value;
  194. value -= p_value;
  195. return old;
  196. }
  197. _ALWAYS_INLINE_ T exchange_if_greater(T p_value) {
  198. if (value < p_value) {
  199. value = p_value;
  200. }
  201. return value;
  202. }
  203. _ALWAYS_INLINE_ T conditional_increment() {
  204. if (value == 0) {
  205. return 0;
  206. } else {
  207. return ++value;
  208. }
  209. }
  210. _ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) :
  211. value(p_value) {
  212. }
  213. };
  214. class SafeFlag {
  215. protected:
  216. bool flag;
  217. public:
  218. _ALWAYS_INLINE_ bool is_set() const {
  219. return flag;
  220. }
  221. _ALWAYS_INLINE_ void set() {
  222. flag = true;
  223. }
  224. _ALWAYS_INLINE_ void clear() {
  225. flag = false;
  226. }
  227. _ALWAYS_INLINE_ void set_to(bool p_value) {
  228. flag = p_value;
  229. }
  230. _ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) :
  231. flag(p_value) {}
  232. };
  233. class SafeRefCount {
  234. uint32_t count = 0;
  235. public:
  236. _ALWAYS_INLINE_ bool ref() { // true on success
  237. if (count != 0) {
  238. ++count;
  239. return true;
  240. } else {
  241. return false;
  242. }
  243. }
  244. _ALWAYS_INLINE_ uint32_t refval() { // none-zero on success
  245. if (count != 0) {
  246. return ++count;
  247. } else {
  248. return 0;
  249. }
  250. }
  251. _ALWAYS_INLINE_ bool unref() { // true if must be disposed of
  252. return --count == 0;
  253. }
  254. _ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
  255. return --count;
  256. }
  257. _ALWAYS_INLINE_ uint32_t get() const {
  258. return count;
  259. }
  260. _ALWAYS_INLINE_ void init(uint32_t p_value = 1) {
  261. count = p_value;
  262. }
  263. };
  264. #endif
  265. #endif // SAFE_REFCOUNT_H