tls_globals.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef BOOST_LEAF_CONFIG_TLS_GLOBALS_HPP_INCLUDED
  2. #define BOOST_LEAF_CONFIG_TLS_GLOBALS_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 globals, which is suitable for single thread environments.
  9. #include <cstdint>
  10. namespace boost { namespace leaf {
  11. namespace leaf_detail
  12. {
  13. using atomic_unsigned_int = unsigned int;
  14. }
  15. namespace tls
  16. {
  17. template <class T>
  18. struct BOOST_LEAF_SYMBOL_VISIBLE ptr
  19. {
  20. static T * p;
  21. };
  22. template <class T>
  23. T * ptr<T>::p;
  24. template <class T>
  25. T * read_ptr() noexcept
  26. {
  27. return ptr<T>::p;
  28. }
  29. template <class T>
  30. void write_ptr( T * p ) noexcept
  31. {
  32. ptr<T>::p = p;
  33. }
  34. ////////////////////////////////////////
  35. template <class Tag>
  36. struct BOOST_LEAF_SYMBOL_VISIBLE tagged_uint
  37. {
  38. static unsigned x;
  39. };
  40. template <class Tag>
  41. unsigned tagged_uint<Tag>::x;
  42. template <class Tag>
  43. unsigned read_uint() noexcept
  44. {
  45. return tagged_uint<Tag>::x;
  46. }
  47. template <class Tag>
  48. void write_uint( unsigned x ) noexcept
  49. {
  50. tagged_uint<Tag>::x = x;
  51. }
  52. template <class Tag>
  53. void uint_increment() noexcept
  54. {
  55. ++tagged_uint<Tag>::x;
  56. }
  57. template <class Tag>
  58. void uint_decrement() noexcept
  59. {
  60. --tagged_uint<Tag>::x;
  61. }
  62. }
  63. } }
  64. #endif