atomic_count_gcc.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED
  3. //
  4. // boost/detail/atomic_count_gcc.hpp
  5. //
  6. // atomic_count for GNU libstdc++ v3
  7. //
  8. // http://gcc.gnu.org/onlinedocs/porting/Thread-safety.html
  9. //
  10. // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
  11. // Copyright (c) 2002 Lars Gullik Bjønnes <[email protected]>
  12. // Copyright 2003-2005 Peter Dimov
  13. //
  14. // Distributed under the Boost Software License, Version 1.0. (See
  15. // accompanying file LICENSE_1_0.txt or copy at
  16. // http://www.boost.org/LICENSE_1_0.txt)
  17. //
  18. #if __GNUC__ * 100 + __GNUC_MINOR__ >= 402
  19. # include <ext/atomicity.h>
  20. #else
  21. # include <bits/atomicity.h>
  22. #endif
  23. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  24. #include <boost/config/pragma_message.hpp>
  25. BOOST_PRAGMA_MESSAGE("Using libstdc++ atomic_count")
  26. #endif
  27. namespace boost
  28. {
  29. namespace detail
  30. {
  31. #if defined(__GLIBCXX__) // g++ 3.4+
  32. using __gnu_cxx::__atomic_add;
  33. using __gnu_cxx::__exchange_and_add;
  34. #endif
  35. class atomic_count
  36. {
  37. public:
  38. explicit atomic_count( long v ) : value_( v ) {}
  39. long operator++()
  40. {
  41. return __exchange_and_add( &value_, +1 ) + 1;
  42. }
  43. long operator--()
  44. {
  45. return __exchange_and_add( &value_, -1 ) - 1;
  46. }
  47. operator long() const
  48. {
  49. return __exchange_and_add( &value_, 0 );
  50. }
  51. private:
  52. atomic_count(atomic_count const &);
  53. atomic_count & operator=(atomic_count const &);
  54. mutable _Atomic_word value_;
  55. };
  56. } // namespace detail
  57. } // namespace boost
  58. #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED