atomic_count_sync.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_SYNC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_SYNC_HPP_INCLUDED
  3. //
  4. // boost/detail/atomic_count_sync.hpp
  5. //
  6. // atomic_count for g++ 4.1+
  7. //
  8. // http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html
  9. //
  10. // Copyright 2007 Peter Dimov
  11. //
  12. // Distributed under the Boost Software License, Version 1.0. (See
  13. // accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. //
  16. #include <boost/cstdint.hpp>
  17. #if defined( __ia64__ ) && defined( __INTEL_COMPILER )
  18. # include <ia64intrin.h>
  19. #endif
  20. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  21. #include <boost/config/pragma_message.hpp>
  22. BOOST_PRAGMA_MESSAGE("Using __sync atomic_count")
  23. #endif
  24. namespace boost
  25. {
  26. namespace detail
  27. {
  28. class atomic_count
  29. {
  30. public:
  31. explicit atomic_count( long v ): value_( static_cast< boost::int_least32_t >( v ) )
  32. {
  33. }
  34. long operator++()
  35. {
  36. return __sync_add_and_fetch( &value_, 1 );
  37. }
  38. long operator--()
  39. {
  40. return __sync_add_and_fetch( &value_, -1 );
  41. }
  42. operator long() const
  43. {
  44. return __sync_fetch_and_add( &value_, 0 );
  45. }
  46. private:
  47. atomic_count(atomic_count const &);
  48. atomic_count & operator=(atomic_count const &);
  49. mutable boost::int_least32_t value_;
  50. };
  51. } // namespace detail
  52. } // namespace boost
  53. #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_SYNC_HPP_INCLUDED