thread_local.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*************************************************************************/
  2. /* thread_local.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 THREAD_LOCAL_H
  31. #define THREAD_LOCAL_H
  32. #ifdef HAVE_CXX11_THREAD_LOCAL
  33. #define _THREAD_LOCAL_(m_t) thread_local m_t
  34. #else
  35. #if !defined(__GNUC__) && !defined(_MSC_VER)
  36. #error Platform or compiler not supported
  37. #endif
  38. #ifdef __GNUC__
  39. #ifdef HAVE_GCC___THREAD
  40. #define _THREAD_LOCAL_(m_t) __thread m_t
  41. #else
  42. #define USE_CUSTOM_THREAD_LOCAL
  43. #endif
  44. #elif _MSC_VER
  45. #ifdef HAVE_DECLSPEC_THREAD
  46. #define _THREAD_LOCAL_(m_t) __declspec(thread) m_t
  47. #else
  48. #define USE_CUSTOM_THREAD_LOCAL
  49. #endif
  50. #endif // __GNUC__ _MSC_VER
  51. #endif // HAVE_CXX11_THREAD_LOCAL
  52. #ifdef USE_CUSTOM_THREAD_LOCAL
  53. #define _THREAD_LOCAL_(m_t) ThreadLocal<m_t>
  54. #endif
  55. #include "core/typedefs.h"
  56. #ifdef WINDOWS_ENABLED
  57. #define _CALLBACK_FUNC_ __stdcall
  58. #else
  59. #define _CALLBACK_FUNC_
  60. #endif
  61. struct ThreadLocalStorage {
  62. void *get_value() const;
  63. void set_value(void *p_value) const;
  64. void alloc(void(_CALLBACK_FUNC_ *p_dest_callback)(void *));
  65. void free();
  66. private:
  67. struct Impl;
  68. Impl *pimpl;
  69. };
  70. template <typename T>
  71. class ThreadLocal {
  72. ThreadLocalStorage storage;
  73. T init_val;
  74. static void _CALLBACK_FUNC_ destr_callback(void *tls_data) {
  75. memdelete(static_cast<T *>(tls_data));
  76. }
  77. T *_tls_get_value() const {
  78. void *tls_data = storage.get_value();
  79. if (tls_data)
  80. return static_cast<T *>(tls_data);
  81. T *data = memnew(T(init_val));
  82. storage.set_value(data);
  83. return data;
  84. }
  85. void _initialize(const T &p_init_val) {
  86. init_val = p_init_val;
  87. storage.alloc(&destr_callback);
  88. }
  89. public:
  90. ThreadLocal() {
  91. _initialize(T());
  92. }
  93. ThreadLocal(const T &p_init_val) {
  94. _initialize(p_init_val);
  95. }
  96. ThreadLocal(const ThreadLocal &other) {
  97. _initialize(*other._tls_get_value());
  98. }
  99. ~ThreadLocal() {
  100. storage.free();
  101. }
  102. _FORCE_INLINE_ T *operator&() const {
  103. return _tls_get_value();
  104. }
  105. _FORCE_INLINE_ operator T &() const {
  106. return *_tls_get_value();
  107. }
  108. _FORCE_INLINE_ ThreadLocal &operator=(const T &val) {
  109. T *ptr = _tls_get_value();
  110. *ptr = val;
  111. return *this;
  112. }
  113. };
  114. struct FlagScopeGuard {
  115. FlagScopeGuard(bool &p_flag) :
  116. flag(p_flag) {
  117. flag = !flag;
  118. }
  119. ~FlagScopeGuard() {
  120. flag = !flag;
  121. }
  122. private:
  123. bool &flag;
  124. };
  125. #undef _CALLBACK_FUNC_
  126. #define _TLS_RECURSION_GUARD_V_(m_ret) \
  127. static _THREAD_LOCAL_(bool) _recursion_flag_ = false; \
  128. if (_recursion_flag_) \
  129. return m_ret; \
  130. FlagScopeGuard _recursion_guard_(_recursion_flag_);
  131. #define _TLS_RECURSION_GUARD_ \
  132. static _THREAD_LOCAL_(bool) _recursion_flag_ = false; \
  133. if (_recursion_flag_) \
  134. return; \
  135. FlagScopeGuard _recursion_guard_(_recursion_flag_);
  136. #endif // THREAD_LOCAL_H