tls_array.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. public:
  51. static BOOST_LEAF_CFG_TLS_INDEX_TYPE next()
  52. {
  53. int idx = ++c_;
  54. BOOST_LEAF_ASSERT(idx > (BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX));
  55. BOOST_LEAF_ASSERT(idx < (BOOST_LEAF_CFG_TLS_ARRAY_SIZE));
  56. return idx;
  57. }
  58. };
  59. template <class T>
  60. struct BOOST_LEAF_SYMBOL_VISIBLE tls_index
  61. {
  62. static BOOST_LEAF_CFG_TLS_INDEX_TYPE idx;
  63. };
  64. template <class T>
  65. struct BOOST_LEAF_SYMBOL_VISIBLE alloc_tls_index
  66. {
  67. static BOOST_LEAF_CFG_TLS_INDEX_TYPE const idx;
  68. };
  69. template <class T>
  70. int index_counter<T>::c_ = BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX;
  71. template <class T>
  72. BOOST_LEAF_CFG_TLS_INDEX_TYPE tls_index<T>::idx = BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX;
  73. template <class T>
  74. BOOST_LEAF_CFG_TLS_INDEX_TYPE const alloc_tls_index<T>::idx = tls_index<T>::idx = index_counter<>::next();
  75. ////////////////////////////////////////
  76. template <class T>
  77. T * read_ptr() noexcept
  78. {
  79. int tls_idx = tls_index<T>::idx;
  80. if( tls_idx == (BOOST_LEAF_CFG_TLS_ARRAY_START_INDEX) )
  81. return nullptr;
  82. --tls_idx;
  83. return reinterpret_cast<T *>(read_void_ptr(tls_idx));
  84. }
  85. template <class T>
  86. void write_ptr( T * p ) noexcept
  87. {
  88. int tls_idx = alloc_tls_index<T>::idx;
  89. --tls_idx;
  90. write_void_ptr(tls_idx, p);
  91. BOOST_LEAF_ASSERT(read_void_ptr(tls_idx) == p);
  92. }
  93. ////////////////////////////////////////
  94. template <class Tag>
  95. unsigned read_uint() noexcept
  96. {
  97. static_assert(sizeof(std::intptr_t) >= sizeof(unsigned), "Incompatible tls_array implementation");
  98. return (unsigned) (std::intptr_t) (void *) read_ptr<Tag>();
  99. }
  100. template <class Tag>
  101. void write_uint( unsigned x ) noexcept
  102. {
  103. static_assert(sizeof(std::intptr_t) >= sizeof(unsigned), "Incompatible tls_array implementation");
  104. write_ptr<Tag>((Tag *) (void *) (std::intptr_t) x);
  105. }
  106. template <class Tag>
  107. void uint_increment() noexcept
  108. {
  109. write_uint<Tag>(read_uint<Tag>() + 1);
  110. }
  111. template <class Tag>
  112. void uint_decrement() noexcept
  113. {
  114. write_uint<Tag>(read_uint<Tag>() - 1);
  115. }
  116. }
  117. } }
  118. #endif