AtomicCounter.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_ATOMICCOUNTER_HPP
  14. #define ZT_ATOMICCOUNTER_HPP
  15. #include "Constants.hpp"
  16. #ifndef __GNUC__
  17. #include <atomic>
  18. #endif
  19. namespace ZeroTier {
  20. /**
  21. * Simple atomic counter supporting increment and decrement
  22. *
  23. * This is used as the reference counter in reference counted objects that
  24. * work with SharedPtr<>.
  25. */
  26. class AtomicCounter
  27. {
  28. public:
  29. ZT_ALWAYS_INLINE AtomicCounter() : _v(0) {}
  30. ZT_ALWAYS_INLINE int load() const
  31. {
  32. #ifdef __GNUC__
  33. return _v;
  34. #else
  35. return _v.load();
  36. #endif
  37. }
  38. ZT_ALWAYS_INLINE void zero() { _v = 0; }
  39. ZT_ALWAYS_INLINE int operator++()
  40. {
  41. #ifdef __GNUC__
  42. return __sync_add_and_fetch((int *)&_v,1);
  43. #else
  44. return ++_v;
  45. #endif
  46. }
  47. ZT_ALWAYS_INLINE int operator--()
  48. {
  49. #ifdef __GNUC__
  50. return __sync_sub_and_fetch((int *)&_v,1);
  51. #else
  52. return --_v;
  53. #endif
  54. }
  55. private:
  56. ZT_ALWAYS_INLINE AtomicCounter(const AtomicCounter &) {}
  57. ZT_ALWAYS_INLINE const AtomicCounter &operator=(const AtomicCounter &) { return *this; }
  58. #ifdef __GNUC__
  59. volatile int _v;
  60. #else
  61. std::atomic_int _v;
  62. #endif
  63. };
  64. } // namespace ZeroTier
  65. #endif