Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:43

0001 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ATOMIC_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ATOMIC_HPP_INCLUDED
0003 
0004 // MS compatible compilers support #pragma once
0005 
0006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0007 # pragma once
0008 #endif
0009 
0010 // Copyright 2008, 2020 Peter Dimov
0011 // Distributed under the Boost Software License, Version 1.0.
0012 // https://www.boost.org/LICENSE_1_0.txt
0013 
0014 #include <boost/smart_ptr/detail/yield_k.hpp>
0015 
0016 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0017 
0018 #include <boost/config/pragma_message.hpp>
0019 BOOST_PRAGMA_MESSAGE("Using __atomic spinlock")
0020 
0021 #endif
0022 
0023 namespace boost
0024 {
0025 
0026 namespace detail
0027 {
0028 
0029 class spinlock
0030 {
0031 public:
0032 
0033     // `bool` alignment is required for Apple PPC32
0034     // https://github.com/boostorg/smart_ptr/issues/105
0035     // https://github.com/PurpleI2P/i2pd/issues/1726
0036     // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107590
0037 
0038     union
0039     {
0040         unsigned char v_;
0041         bool align_;
0042     };
0043 
0044 public:
0045 
0046     bool try_lock()
0047     {
0048         return __atomic_test_and_set( &v_, __ATOMIC_ACQUIRE ) == 0;
0049     }
0050 
0051     void lock()
0052     {
0053         for( unsigned k = 0; !try_lock(); ++k )
0054         {
0055             boost::detail::yield( k );
0056         }
0057     }
0058 
0059     void unlock()
0060     {
0061         __atomic_clear( &v_, __ATOMIC_RELEASE );
0062     }
0063 
0064 public:
0065 
0066     class scoped_lock
0067     {
0068     private:
0069 
0070         spinlock & sp_;
0071 
0072         scoped_lock( scoped_lock const & );
0073         scoped_lock & operator=( scoped_lock const & );
0074 
0075     public:
0076 
0077         explicit scoped_lock( spinlock & sp ): sp_( sp )
0078         {
0079             sp.lock();
0080         }
0081 
0082         ~scoped_lock()
0083         {
0084             sp_.unlock();
0085         }
0086     };
0087 };
0088 
0089 } // namespace detail
0090 } // namespace boost
0091 
0092 #define BOOST_DETAIL_SPINLOCK_INIT {{0}}
0093 
0094 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ATOMIC_HPP_INCLUDED