safe_refcount.h 8.4 KB

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