atomic_count_sync.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifndef DAHUA_DETAIL_ATOMIC_COUNT_SYNC_HPP_INCLUDED
  2. #define DAHUA_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. namespace Dahua {
  17. namespace Infra {
  18. namespace Detail {
  19. class atomic_count
  20. {
  21. public:
  22. explicit atomic_count( long v ) : value_( v ) {}
  23. long operator++()
  24. {
  25. return __sync_add_and_fetch( &value_, 1 );
  26. }
  27. long operator--()
  28. {
  29. return __sync_add_and_fetch( &value_, -1 );
  30. }
  31. operator long() const
  32. {
  33. return __sync_fetch_and_add( &value_, 0 );
  34. }
  35. long get() const
  36. {
  37. return __sync_fetch_and_add(&value_, 0);
  38. }
  39. void set(long v)
  40. {
  41. long tmp = get();
  42. __sync_fetch_and_add(&value_, v - tmp);
  43. return;
  44. }
  45. private:
  46. atomic_count(atomic_count const &);
  47. atomic_count & operator=(atomic_count const &);
  48. mutable long value_;
  49. };
  50. } // namespace Detail
  51. } // namespace Infra
  52. } // namespace Dahua
  53. #endif // #ifndef DAHUA_DETAIL_ATOMIC_COUNT_SYNC_HPP_INCLUDED