safe_refcount.hpp 8.0 KB

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