lock_pool.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2011 Helge Bahmann
  7. * Copyright (c) 2013-2014, 2020 Andrey Semashev
  8. */
  9. /*!
  10. * \file atomic/detail/lock_pool.hpp
  11. *
  12. * This header contains declaration of the lock pool used to emulate atomic ops.
  13. */
  14. #ifndef BOOST_ATOMIC_DETAIL_LOCK_POOL_HPP_INCLUDED_
  15. #define BOOST_ATOMIC_DETAIL_LOCK_POOL_HPP_INCLUDED_
  16. #include <cstddef>
  17. #include <boost/atomic/detail/config.hpp>
  18. #include <boost/atomic/detail/link.hpp>
  19. #include <boost/atomic/detail/intptr.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. namespace atomics {
  25. namespace detail {
  26. namespace lock_pool {
  27. BOOST_ATOMIC_DECL void* lock(atomics::detail::uintptr_t ptr) BOOST_NOEXCEPT;
  28. BOOST_ATOMIC_DECL void unlock(void* p) BOOST_NOEXCEPT;
  29. BOOST_ATOMIC_DECL void thread_fence() BOOST_NOEXCEPT;
  30. BOOST_ATOMIC_DECL void signal_fence() BOOST_NOEXCEPT;
  31. template< std::size_t Alignment >
  32. class scoped_lock
  33. {
  34. private:
  35. void* m_lock;
  36. public:
  37. explicit scoped_lock(const volatile void* addr) BOOST_NOEXCEPT :
  38. m_lock(lock_pool::lock(((atomics::detail::uintptr_t)addr) / Alignment))
  39. {
  40. }
  41. ~scoped_lock() BOOST_NOEXCEPT
  42. {
  43. lock_pool::unlock(m_lock);
  44. }
  45. BOOST_DELETED_FUNCTION(scoped_lock(scoped_lock const&))
  46. BOOST_DELETED_FUNCTION(scoped_lock& operator=(scoped_lock const&))
  47. };
  48. } // namespace lock_pool
  49. } // namespace detail
  50. } // namespace atomics
  51. } // namespace boost
  52. #endif // BOOST_ATOMIC_DETAIL_LOCK_POOL_HPP_INCLUDED_