tls_array.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef BOOST_LEAF_CONFIG_TLS_ARRAY_HPP_INCLUDED
  2. #define BOOST_LEAF_CONFIG_TLS_ARRAY_HPP_INCLUDED
  3. // Copyright 2018-2023 Emil Dotchevski and Reverge Studios, Inc.
  4. // Copyright (c) 2022 Khalil Estell
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // LEAF requires thread local storage support for pointers and for uin32_t values.
  8. // This header implements thread local storage for pointers and for unsigned int
  9. // values for platforms that support thread local pointers by index.
  10. namespace boost { namespace leaf {
  11. namespace tls
  12. {
  13. // The TLS support defined in this header requires the following two
  14. // functions to be defined elsewhere:
  15. void * read_void_ptr( int tls_index ) noexcept;
  16. void write_void_ptr( int tls_index, void * ) noexcept;
  17. }
  18. } }
  19. ////////////////////////////////////////
  20. #include <limits>
  21. #include <atomic>
  22. #include <cstdint>
  23. #include <type_traits>
  24. #ifndef BOOST_LEAF_CFG_TLS_INDEX_TYPE
  25. # define BOOST_LEAF_CFG_TLS_INDEX_TYPE unsigned char
  26. #endif
  27. #ifndef BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX
  28. # define BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX 0
  29. #endif
  30. static_assert((BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX) >= 0,
  31. "Bad BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX");
  32. #ifdef BOOST_LEAF_CFG_TLS_ARRAY_SIZE
  33. static_assert((BOOST_LEAF_CFG_TLS_ARRAY_SIZE) > (BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX),
  34. "Bad BOOST_LEAF_CFG_TLS_ARRAY_SIZE");
  35. static_assert((BOOST_LEAF_CFG_TLS_ARRAY_SIZE) - 1 <= std::numeric_limits<BOOST_LEAF_CFG_TLS_INDEX_TYPE>::max(),
  36. "Bad BOOST_LEAF_CFG_TLS_ARRAY_SIZE");
  37. #endif
  38. ////////////////////////////////////////
  39. namespace boost { namespace leaf {
  40. namespace leaf_detail
  41. {
  42. using atomic_unsigned_int = std::atomic<unsigned int>;
  43. }
  44. namespace tls
  45. {
  46. template <class=void>
  47. class BOOST_LEAF_SYMBOL_VISIBLE index_counter
  48. {
  49. static int c_;
  50. static BOOST_LEAF_CFG_TLS_INDEX_TYPE next_() noexcept
  51. {
  52. int idx = ++c_;
  53. BOOST_LEAF_ASSERT(idx > (BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX));
  54. BOOST_LEAF_ASSERT(idx < (BOOST_LEAF_CFG_TLS_ARRAY_SIZE));
  55. return idx;
  56. }
  57. public:
  58. template <class T>
  59. static BOOST_LEAF_CFG_TLS_INDEX_TYPE next() noexcept
  60. {
  61. return next_(); // Set breakpoint here to monitor TLS index allocation for T.
  62. }
  63. };
  64. template <class T>
  65. struct BOOST_LEAF_SYMBOL_VISIBLE tls_index
  66. {
  67. static BOOST_LEAF_CFG_TLS_INDEX_TYPE idx;
  68. };
  69. template <class T>
  70. struct BOOST_LEAF_SYMBOL_VISIBLE alloc_tls_index
  71. {
  72. static BOOST_LEAF_CFG_TLS_INDEX_TYPE const idx;
  73. };
  74. template <class T>
  75. int index_counter<T>::c_ = BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX;
  76. template <class T>
  77. BOOST_LEAF_CFG_TLS_INDEX_TYPE tls_index<T>::idx = BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX;
  78. template <class T>
  79. BOOST_LEAF_CFG_TLS_INDEX_TYPE const alloc_tls_index<T>::idx = tls_index<T>::idx = index_counter<>::next<T>();
  80. ////////////////////////////////////////
  81. template <class T>
  82. T * read_ptr() noexcept
  83. {
  84. int tls_idx = tls_index<T>::idx;
  85. if( tls_idx == (BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX) )
  86. return nullptr;
  87. --tls_idx;
  88. return reinterpret_cast<T *>(read_void_ptr(tls_idx));
  89. }
  90. template <class T>
  91. void write_ptr( T * p ) noexcept
  92. {
  93. int tls_idx = alloc_tls_index<T>::idx;
  94. --tls_idx;
  95. write_void_ptr(tls_idx, p);
  96. BOOST_LEAF_ASSERT(read_void_ptr(tls_idx) == p);
  97. }
  98. ////////////////////////////////////////
  99. template <class Tag>
  100. unsigned read_uint() noexcept
  101. {
  102. static_assert(sizeof(std::intptr_t) >= sizeof(unsigned), "Incompatible tls_array implementation");
  103. return (unsigned) (std::intptr_t) (void *) read_ptr<Tag>();
  104. }
  105. template <class Tag>
  106. void write_uint( unsigned x ) noexcept
  107. {
  108. static_assert(sizeof(std::intptr_t) >= sizeof(unsigned), "Incompatible tls_array implementation");
  109. write_ptr<Tag>((Tag *) (void *) (std::intptr_t) x);
  110. }
  111. template <class Tag>
  112. void uint_increment() noexcept
  113. {
  114. write_uint<Tag>(read_uint<Tag>() + 1);
  115. }
  116. template <class Tag>
  117. void uint_decrement() noexcept
  118. {
  119. write_uint<Tag>(read_uint<Tag>() - 1);
  120. }
  121. }
  122. } }
  123. #endif