thread_local.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*************************************************************************/
  2. /* thread_local.cpp */
  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. #include "thread_local.h"
  31. #ifdef WINDOWS_ENABLED
  32. #define WIN32_LEAN_AND_MEAN
  33. #include <windows.h>
  34. #else
  35. #include <pthread.h>
  36. #endif
  37. #include "core/os/memory.h"
  38. #include "core/print_string.h"
  39. struct ThreadLocalStorage::Impl {
  40. #ifdef WINDOWS_ENABLED
  41. DWORD dwFlsIndex;
  42. #else
  43. pthread_key_t key;
  44. #endif
  45. void *get_value() const {
  46. #ifdef WINDOWS_ENABLED
  47. return FlsGetValue(dwFlsIndex);
  48. #else
  49. return pthread_getspecific(key);
  50. #endif
  51. }
  52. void set_value(void *p_value) const {
  53. #ifdef WINDOWS_ENABLED
  54. FlsSetValue(dwFlsIndex, p_value);
  55. #else
  56. pthread_setspecific(key, p_value);
  57. #endif
  58. }
  59. #ifdef WINDOWS_ENABLED
  60. #define _CALLBACK_FUNC_ __stdcall
  61. #else
  62. #define _CALLBACK_FUNC_
  63. #endif
  64. Impl(void(_CALLBACK_FUNC_ *p_destr_callback_func)(void *)) {
  65. #ifdef WINDOWS_ENABLED
  66. dwFlsIndex = FlsAlloc(p_destr_callback_func);
  67. ERR_FAIL_COND(dwFlsIndex == FLS_OUT_OF_INDEXES);
  68. #else
  69. pthread_key_create(&key, p_destr_callback_func);
  70. #endif
  71. }
  72. ~Impl() {
  73. #ifdef WINDOWS_ENABLED
  74. FlsFree(dwFlsIndex);
  75. #else
  76. pthread_key_delete(key);
  77. #endif
  78. }
  79. };
  80. void *ThreadLocalStorage::get_value() const {
  81. return pimpl->get_value();
  82. }
  83. void ThreadLocalStorage::set_value(void *p_value) const {
  84. pimpl->set_value(p_value);
  85. }
  86. void ThreadLocalStorage::alloc(void(_CALLBACK_FUNC_ *p_destr_callback)(void *)) {
  87. pimpl = memnew(ThreadLocalStorage::Impl(p_destr_callback));
  88. }
  89. #undef _CALLBACK_FUNC_
  90. void ThreadLocalStorage::free() {
  91. memdelete(pimpl);
  92. pimpl = NULL;
  93. }