atomic_count_gcc.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <larsbj@lyx.org>
  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. namespace boost
  24. {
  25. namespace detail
  26. {
  27. #if defined(__GLIBCXX__) // g++ 3.4+
  28. using __gnu_cxx::__atomic_add;
  29. using __gnu_cxx::__exchange_and_add;
  30. #endif
  31. class atomic_count
  32. {
  33. public:
  34. explicit atomic_count( long v ) : value_( v ) {}
  35. long operator++()
  36. {
  37. return __exchange_and_add( &value_, +1 ) + 1;
  38. }
  39. long operator--()
  40. {
  41. return __exchange_and_add( &value_, -1 ) - 1;
  42. }
  43. operator long() const
  44. {
  45. return __exchange_and_add( &value_, 0 );
  46. }
  47. private:
  48. atomic_count(atomic_count const &);
  49. atomic_count & operator=(atomic_count const &);
  50. mutable _Atomic_word value_;
  51. };
  52. } // namespace detail
  53. } // namespace boost
  54. #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED