spinlock_std_atomic.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // Copyright (c) 2014 Peter Dimov
  9. //
  10. // Distributed under the Boost Software License, Version 1.0.
  11. // See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. //
  14. #include <boost/smart_ptr/detail/yield_k.hpp>
  15. #include <atomic>
  16. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  17. #include <boost/config/pragma_message.hpp>
  18. BOOST_PRAGMA_MESSAGE("Using std::atomic spinlock")
  19. #endif
  20. namespace boost
  21. {
  22. namespace detail
  23. {
  24. class spinlock
  25. {
  26. public:
  27. std::atomic_flag v_;
  28. public:
  29. bool try_lock() noexcept
  30. {
  31. return !v_.test_and_set( std::memory_order_acquire );
  32. }
  33. void lock() noexcept
  34. {
  35. for( unsigned k = 0; !try_lock(); ++k )
  36. {
  37. boost::detail::yield( k );
  38. }
  39. }
  40. void unlock() noexcept
  41. {
  42. v_ .clear( std::memory_order_release );
  43. }
  44. public:
  45. class scoped_lock
  46. {
  47. private:
  48. spinlock & sp_;
  49. scoped_lock( scoped_lock const & );
  50. scoped_lock & operator=( scoped_lock const & );
  51. public:
  52. explicit scoped_lock( spinlock & sp ) noexcept: sp_( sp )
  53. {
  54. sp.lock();
  55. }
  56. ~scoped_lock() /*noexcept*/
  57. {
  58. sp_.unlock();
  59. }
  60. };
  61. };
  62. } // namespace detail
  63. } // namespace boost
  64. #define BOOST_DETAIL_SPINLOCK_INIT { ATOMIC_FLAG_INIT }
  65. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED