Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_X86_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_X86_HPP_INCLUDED
0003 
0004 //
0005 //  boost/detail/atomic_count_gcc_x86.hpp
0006 //
0007 //  atomic_count for g++ on 486+/AMD64
0008 //
0009 //  Copyright 2007 Peter Dimov
0010 //
0011 //  Distributed under the Boost Software License, Version 1.0. (See
0012 //  accompanying file LICENSE_1_0.txt or copy at
0013 //  http://www.boost.org/LICENSE_1_0.txt)
0014 //
0015 
0016 #include <boost/smart_ptr/detail/sp_obsolete.hpp>
0017 
0018 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0019 
0020 #include <boost/config/pragma_message.hpp>
0021 BOOST_PRAGMA_MESSAGE("Using g++/x86 atomic_count")
0022 
0023 #endif
0024 
0025 BOOST_SP_OBSOLETE()
0026 
0027 namespace boost
0028 {
0029 
0030 namespace detail
0031 {
0032 
0033 class atomic_count
0034 {
0035 public:
0036 
0037     explicit atomic_count( long v ) : value_( static_cast< int >( v ) ) {}
0038 
0039     long operator++()
0040     {
0041         return atomic_exchange_and_add( &value_, +1 ) + 1;
0042     }
0043 
0044     long operator--()
0045     {
0046         return atomic_exchange_and_add( &value_, -1 ) - 1;
0047     }
0048 
0049     operator long() const
0050     {
0051         return atomic_exchange_and_add( &value_, 0 );
0052     }
0053 
0054 private:
0055 
0056     atomic_count(atomic_count const &);
0057     atomic_count & operator=(atomic_count const &);
0058 
0059     mutable int value_;
0060 
0061 private:
0062 
0063     static int atomic_exchange_and_add( int * pw, int dv )
0064     {
0065         // int r = *pw;
0066         // *pw += dv;
0067         // return r;
0068 
0069         int r;
0070 
0071         __asm__ __volatile__
0072         (
0073             "lock\n\t"
0074             "xadd %1, %0":
0075             "+m"( *pw ), "=r"( r ): // outputs (%0, %1)
0076             "1"( dv ): // inputs (%2 == %1)
0077             "memory", "cc" // clobbers
0078         );
0079 
0080         return r;
0081     }
0082 };
0083 
0084 } // namespace detail
0085 
0086 } // namespace boost
0087 
0088 #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_X86_HPP_INCLUDED