File indexing completed on 2025-01-18 09:51:43
0001 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_STD_ATOMIC_HPP_INCLUDED
0003
0004
0005
0006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0007 # pragma once
0008 #endif
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <boost/smart_ptr/detail/yield_k.hpp>
0019 #include <boost/config.hpp>
0020 #include <atomic>
0021
0022 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0023
0024 #include <boost/config/pragma_message.hpp>
0025 BOOST_PRAGMA_MESSAGE("Using std::atomic spinlock")
0026
0027 #endif
0028
0029 namespace boost
0030 {
0031
0032 namespace detail
0033 {
0034
0035 class spinlock
0036 {
0037 public:
0038
0039 std::atomic_flag v_;
0040
0041 public:
0042
0043 bool try_lock() BOOST_NOEXCEPT
0044 {
0045 return !v_.test_and_set( std::memory_order_acquire );
0046 }
0047
0048 void lock() BOOST_NOEXCEPT
0049 {
0050 for( unsigned k = 0; !try_lock(); ++k )
0051 {
0052 boost::detail::yield( k );
0053 }
0054 }
0055
0056 void unlock() BOOST_NOEXCEPT
0057 {
0058 v_ .clear( std::memory_order_release );
0059 }
0060
0061 public:
0062
0063 class scoped_lock
0064 {
0065 private:
0066
0067 spinlock & sp_;
0068
0069 scoped_lock( scoped_lock const & );
0070 scoped_lock & operator=( scoped_lock const & );
0071
0072 public:
0073
0074 explicit scoped_lock( spinlock & sp ) BOOST_NOEXCEPT: sp_( sp )
0075 {
0076 sp.lock();
0077 }
0078
0079 ~scoped_lock()
0080 {
0081 sp_.unlock();
0082 }
0083 };
0084 };
0085
0086 }
0087 }
0088
0089 #define BOOST_DETAIL_SPINLOCK_INIT { ATOMIC_FLAG_INIT }
0090
0091 #endif