File indexing completed on 2025-09-13 08:51:02
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 <atomic>
0020
0021 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0022
0023 #include <boost/config/pragma_message.hpp>
0024 BOOST_PRAGMA_MESSAGE("Using std::atomic spinlock")
0025
0026 #endif
0027
0028 namespace boost
0029 {
0030
0031 namespace detail
0032 {
0033
0034 class spinlock
0035 {
0036 public:
0037
0038 std::atomic_flag v_;
0039
0040 public:
0041
0042 bool try_lock() noexcept
0043 {
0044 return !v_.test_and_set( std::memory_order_acquire );
0045 }
0046
0047 void lock() noexcept
0048 {
0049 for( unsigned k = 0; !try_lock(); ++k )
0050 {
0051 boost::detail::yield( k );
0052 }
0053 }
0054
0055 void unlock() noexcept
0056 {
0057 v_ .clear( std::memory_order_release );
0058 }
0059
0060 public:
0061
0062 class scoped_lock
0063 {
0064 private:
0065
0066 spinlock & sp_;
0067
0068 scoped_lock( scoped_lock const & );
0069 scoped_lock & operator=( scoped_lock const & );
0070
0071 public:
0072
0073 explicit scoped_lock( spinlock & sp ) noexcept: sp_( sp )
0074 {
0075 sp.lock();
0076 }
0077
0078 ~scoped_lock()
0079 {
0080 sp_.unlock();
0081 }
0082 };
0083 };
0084
0085 }
0086 }
0087
0088 #define BOOST_DETAIL_SPINLOCK_INIT { ATOMIC_FLAG_INIT }
0089
0090 #endif