spinlock_std_atomic.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <boost/config.hpp>
  16. #include <atomic>
  17. namespace boost
  18. {
  19. namespace detail
  20. {
  21. class spinlock
  22. {
  23. public:
  24. std::atomic_flag v_;
  25. public:
  26. bool try_lock() BOOST_NOEXCEPT
  27. {
  28. return !v_.test_and_set( std::memory_order_acquire );
  29. }
  30. void lock() BOOST_NOEXCEPT
  31. {
  32. for( unsigned k = 0; !try_lock(); ++k )
  33. {
  34. boost::detail::yield( k );
  35. }
  36. }
  37. void unlock() BOOST_NOEXCEPT
  38. {
  39. v_ .clear( std::memory_order_release );
  40. }
  41. public:
  42. class scoped_lock
  43. {
  44. private:
  45. spinlock & sp_;
  46. scoped_lock( scoped_lock const & );
  47. scoped_lock & operator=( scoped_lock const & );
  48. public:
  49. explicit scoped_lock( spinlock & sp ) BOOST_NOEXCEPT: sp_( sp )
  50. {
  51. sp.lock();
  52. }
  53. ~scoped_lock() /*BOOST_NOEXCEPT*/
  54. {
  55. sp_.unlock();
  56. }
  57. };
  58. };
  59. } // namespace detail
  60. } // namespace boost
  61. #define BOOST_DETAIL_SPINLOCK_INIT { ATOMIC_FLAG_INIT }
  62. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED