tls_cpp11.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef BOOST_LEAF_CONFIG_TLS_CPP11_HPP_INCLUDED
  2. #define BOOST_LEAF_CONFIG_TLS_CPP11_HPP_INCLUDED
  3. // Copyright 2018-2024 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // LEAF requires thread local storage support for pointers and for uin32_t values.
  7. // This header implements thread local storage for pointers and for unsigned int
  8. // values using the C++11 built-in thread_local storage class specifier.
  9. #include <cstdint>
  10. #include <atomic>
  11. namespace boost { namespace leaf {
  12. namespace detail
  13. {
  14. using atomic_unsigned_int = std::atomic<unsigned int>;
  15. }
  16. namespace tls
  17. {
  18. template <class T>
  19. struct BOOST_LEAF_SYMBOL_VISIBLE ptr
  20. {
  21. static thread_local T * p;
  22. };
  23. template <class T>
  24. thread_local T * ptr<T>::p;
  25. template <class T>
  26. T * read_ptr() noexcept
  27. {
  28. return ptr<T>::p;
  29. }
  30. template <class T>
  31. void write_ptr( T * p ) noexcept
  32. {
  33. ptr<T>::p = p;
  34. }
  35. ////////////////////////////////////////
  36. template <class Tag>
  37. struct BOOST_LEAF_SYMBOL_VISIBLE tagged_uint
  38. {
  39. static thread_local unsigned x;
  40. };
  41. template <class Tag>
  42. thread_local unsigned tagged_uint<Tag>::x;
  43. template <class Tag>
  44. unsigned read_uint() noexcept
  45. {
  46. return tagged_uint<Tag>::x;
  47. }
  48. template <class Tag>
  49. void write_uint( unsigned x ) noexcept
  50. {
  51. tagged_uint<Tag>::x = x;
  52. }
  53. }
  54. } }
  55. #endif // BOOST_LEAF_CONFIG_TLS_CPP11_HPP_INCLUDED