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