thread_local.cpp 3.5 KB

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