atomic_count_gcc_x86.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_X86_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_X86_HPP_INCLUDED
  3. //
  4. // boost/detail/atomic_count_gcc_x86.hpp
  5. //
  6. // atomic_count for g++ on 486+/AMD64
  7. //
  8. // Copyright 2007 Peter Dimov
  9. //
  10. // Distributed under the Boost Software License, Version 1.0. (See
  11. // 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/sp_obsolete.hpp>
  15. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  16. #include <boost/config/pragma_message.hpp>
  17. BOOST_PRAGMA_MESSAGE("Using g++/x86 atomic_count")
  18. #endif
  19. BOOST_SP_OBSOLETE()
  20. namespace boost
  21. {
  22. namespace detail
  23. {
  24. class atomic_count
  25. {
  26. public:
  27. explicit atomic_count( long v ) : value_( static_cast< int >( v ) ) {}
  28. long operator++()
  29. {
  30. return atomic_exchange_and_add( &value_, +1 ) + 1;
  31. }
  32. long operator--()
  33. {
  34. return atomic_exchange_and_add( &value_, -1 ) - 1;
  35. }
  36. operator long() const
  37. {
  38. return atomic_exchange_and_add( &value_, 0 );
  39. }
  40. private:
  41. atomic_count(atomic_count const &);
  42. atomic_count & operator=(atomic_count const &);
  43. mutable int value_;
  44. private:
  45. static int atomic_exchange_and_add( int * pw, int dv )
  46. {
  47. // int r = *pw;
  48. // *pw += dv;
  49. // return r;
  50. int r;
  51. __asm__ __volatile__
  52. (
  53. "lock\n\t"
  54. "xadd %1, %0":
  55. "+m"( *pw ), "=r"( r ): // outputs (%0, %1)
  56. "1"( dv ): // inputs (%2 == %1)
  57. "memory", "cc" // clobbers
  58. );
  59. return r;
  60. }
  61. };
  62. } // namespace detail
  63. } // namespace boost
  64. #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_X86_HPP_INCLUDED