safe_refcount.h 7.9 KB

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