atomic_count_gcc.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef DAHUA_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED
  2. #define DAHUA_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. #include <bits/atomicity.h>
  19. namespace Dahua {
  20. namespace Infra {
  21. namespace Detail {
  22. #if defined(__GLIBCXX__) // g++ 3.4+
  23. using __gnu_cxx::__atomic_add;
  24. using __gnu_cxx::__exchange_and_add;
  25. #endif
  26. class atomic_count
  27. {
  28. public:
  29. explicit atomic_count(long v) : value_(v) {}
  30. long operator++()
  31. {
  32. return __exchange_and_add(&value_, 1) + 1;
  33. }
  34. long operator--()
  35. {
  36. return __exchange_and_add(&value_, -1) - 1;
  37. }
  38. operator long() const
  39. {
  40. return __exchange_and_add(&value_, 0);
  41. }
  42. long get() const
  43. {
  44. return __exchange_and_add(&value_, 0);
  45. }
  46. void set(long v)
  47. {
  48. long tmp = get();
  49. __exchange_and_add(&value_, v - tmp);
  50. return;
  51. }
  52. private:
  53. atomic_count(atomic_count const &);
  54. atomic_count & operator=(atomic_count const &);
  55. mutable _Atomic_word value_;
  56. };
  57. } // namespace Detail
  58. } // namespace Infra
  59. } // namespace Dahua
  60. #endif // #ifndef DAHUA_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED