safe_refcount.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**************************************************************************/
  2. /* safe_refcount.hpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 GODOT_SAFE_REFCOUNT_HPP
  31. #define GODOT_SAFE_REFCOUNT_HPP
  32. #if !defined(NO_THREADS)
  33. #include <atomic>
  34. #include <type_traits>
  35. namespace godot {
  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. // These are 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. #define SAFE_FLAG_TYPE_PUN_GUARANTEES \
  51. static_assert(sizeof(SafeFlag) == sizeof(bool)); \
  52. static_assert(alignof(SafeFlag) == alignof(bool));
  53. template <typename T>
  54. class SafeNumeric {
  55. std::atomic<T> value;
  56. static_assert(std::atomic<T>::is_always_lock_free);
  57. public:
  58. _ALWAYS_INLINE_ void set(T p_value) {
  59. value.store(p_value, std::memory_order_release);
  60. }
  61. _ALWAYS_INLINE_ T get() const {
  62. return value.load(std::memory_order_acquire);
  63. }
  64. _ALWAYS_INLINE_ T increment() {
  65. return value.fetch_add(1, std::memory_order_acq_rel) + 1;
  66. }
  67. // Returns the original value instead of the new one
  68. _ALWAYS_INLINE_ T postincrement() {
  69. return value.fetch_add(1, std::memory_order_acq_rel);
  70. }
  71. _ALWAYS_INLINE_ T decrement() {
  72. return value.fetch_sub(1, std::memory_order_acq_rel) - 1;
  73. }
  74. // Returns the original value instead of the new one
  75. _ALWAYS_INLINE_ T postdecrement() {
  76. return value.fetch_sub(1, std::memory_order_acq_rel);
  77. }
  78. _ALWAYS_INLINE_ T add(T p_value) {
  79. return value.fetch_add(p_value, std::memory_order_acq_rel) + p_value;
  80. }
  81. // Returns the original value instead of the new one
  82. _ALWAYS_INLINE_ T postadd(T p_value) {
  83. return value.fetch_add(p_value, std::memory_order_acq_rel);
  84. }
  85. _ALWAYS_INLINE_ T sub(T p_value) {
  86. return value.fetch_sub(p_value, std::memory_order_acq_rel) - p_value;
  87. }
  88. // Returns the original value instead of the new one
  89. _ALWAYS_INLINE_ T postsub(T p_value) {
  90. return value.fetch_sub(p_value, std::memory_order_acq_rel);
  91. }
  92. _ALWAYS_INLINE_ T exchange_if_greater(T p_value) {
  93. while (true) {
  94. T tmp = value.load(std::memory_order_acquire);
  95. if (tmp >= p_value) {
  96. return tmp; // already greater, or equal
  97. }
  98. if (value.compare_exchange_weak(tmp, p_value, std::memory_order_release)) {
  99. return p_value;
  100. }
  101. }
  102. }
  103. _ALWAYS_INLINE_ T conditional_increment() {
  104. while (true) {
  105. T c = value.load(std::memory_order_acquire);
  106. if (c == 0) {
  107. return 0;
  108. }
  109. if (value.compare_exchange_weak(c, c + 1, std::memory_order_release)) {
  110. return c + 1;
  111. }
  112. }
  113. }
  114. _ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) {
  115. set(p_value);
  116. }
  117. };
  118. class SafeFlag {
  119. std::atomic_bool flag;
  120. static_assert(std::atomic_bool::is_always_lock_free);
  121. public:
  122. _ALWAYS_INLINE_ bool is_set() const {
  123. return flag.load(std::memory_order_acquire);
  124. }
  125. _ALWAYS_INLINE_ void set() {
  126. flag.store(true, std::memory_order_release);
  127. }
  128. _ALWAYS_INLINE_ void clear() {
  129. flag.store(false, std::memory_order_release);
  130. }
  131. _ALWAYS_INLINE_ void set_to(bool p_value) {
  132. flag.store(p_value, std::memory_order_release);
  133. }
  134. _ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) {
  135. set_to(p_value);
  136. }
  137. };
  138. class SafeRefCount {
  139. SafeNumeric<uint32_t> count;
  140. public:
  141. _ALWAYS_INLINE_ bool ref() { // true on success
  142. return count.conditional_increment() != 0;
  143. }
  144. _ALWAYS_INLINE_ uint32_t refval() { // none-zero on success
  145. return count.conditional_increment();
  146. }
  147. _ALWAYS_INLINE_ bool unref() { // true if must be disposed of
  148. return count.decrement() == 0;
  149. }
  150. _ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
  151. return count.decrement();
  152. }
  153. _ALWAYS_INLINE_ uint32_t get() const {
  154. return count.get();
  155. }
  156. _ALWAYS_INLINE_ void init(uint32_t p_value = 1) {
  157. count.set(p_value);
  158. }
  159. };
  160. #else
  161. template <typename T>
  162. class SafeNumeric {
  163. protected:
  164. T value;
  165. public:
  166. _ALWAYS_INLINE_ void set(T p_value) {
  167. value = p_value;
  168. }
  169. _ALWAYS_INLINE_ T get() const {
  170. return value;
  171. }
  172. _ALWAYS_INLINE_ T increment() {
  173. return ++value;
  174. }
  175. _ALWAYS_INLINE_ T postincrement() {
  176. return value++;
  177. }
  178. _ALWAYS_INLINE_ T decrement() {
  179. return --value;
  180. }
  181. _ALWAYS_INLINE_ T postdecrement() {
  182. return value--;
  183. }
  184. _ALWAYS_INLINE_ T add(T p_value) {
  185. return value += p_value;
  186. }
  187. _ALWAYS_INLINE_ T postadd(T p_value) {
  188. T old = value;
  189. value += p_value;
  190. return old;
  191. }
  192. _ALWAYS_INLINE_ T sub(T p_value) {
  193. return value -= p_value;
  194. }
  195. _ALWAYS_INLINE_ T postsub(T p_value) {
  196. T old = value;
  197. value -= p_value;
  198. return old;
  199. }
  200. _ALWAYS_INLINE_ T exchange_if_greater(T p_value) {
  201. if (value < p_value) {
  202. value = p_value;
  203. }
  204. return value;
  205. }
  206. _ALWAYS_INLINE_ T conditional_increment() {
  207. if (value == 0) {
  208. return 0;
  209. } else {
  210. return ++value;
  211. }
  212. }
  213. _ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) :
  214. value(p_value) {
  215. }
  216. };
  217. class SafeFlag {
  218. protected:
  219. bool flag;
  220. public:
  221. _ALWAYS_INLINE_ bool is_set() const {
  222. return flag;
  223. }
  224. _ALWAYS_INLINE_ void set() {
  225. flag = true;
  226. }
  227. _ALWAYS_INLINE_ void clear() {
  228. flag = false;
  229. }
  230. _ALWAYS_INLINE_ void set_to(bool p_value) {
  231. flag = p_value;
  232. }
  233. _ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) :
  234. flag(p_value) {}
  235. };
  236. class SafeRefCount {
  237. uint32_t count = 0;
  238. public:
  239. _ALWAYS_INLINE_ bool ref() { // true on success
  240. if (count != 0) {
  241. ++count;
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. }
  247. _ALWAYS_INLINE_ uint32_t refval() { // none-zero on success
  248. if (count != 0) {
  249. return ++count;
  250. } else {
  251. return 0;
  252. }
  253. }
  254. _ALWAYS_INLINE_ bool unref() { // true if must be disposed of
  255. return --count == 0;
  256. }
  257. _ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
  258. return --count;
  259. }
  260. _ALWAYS_INLINE_ uint32_t get() const {
  261. return count;
  262. }
  263. _ALWAYS_INLINE_ void init(uint32_t p_value = 1) {
  264. count = p_value;
  265. }
  266. };
  267. #endif
  268. } // namespace godot
  269. #endif // GODOT_SAFE_REFCOUNT_HPP