tls_cpp11.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef BOOST_LEAF_CONFIG_TLS_CPP11_HPP_INCLUDED
  2. #define BOOST_LEAF_CONFIG_TLS_CPP11_HPP_INCLUDED
  3. // Copyright 2018-2023 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 leaf_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. template <class Tag>
  54. void uint_increment() noexcept
  55. {
  56. ++tagged_uint<Tag>::x;
  57. }
  58. template <class Tag>
  59. void uint_decrement() noexcept
  60. {
  61. --tagged_uint<Tag>::x;
  62. }
  63. }
  64. } }
  65. #endif