AtomicCounter.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_ATOMICCOUNTER_HPP
  28. #define ZT_ATOMICCOUNTER_HPP
  29. #include "Constants.hpp"
  30. #include "Mutex.hpp"
  31. #include "NonCopyable.hpp"
  32. #ifdef __WINDOWS__
  33. // <atomic> will replace this whole class eventually once it's ubiquitous
  34. #include <atomic>
  35. #endif
  36. namespace ZeroTier {
  37. /**
  38. * Simple atomic counter supporting increment and decrement
  39. */
  40. class AtomicCounter : NonCopyable
  41. {
  42. public:
  43. /**
  44. * Initialize counter at zero
  45. */
  46. AtomicCounter()
  47. throw()
  48. {
  49. _v = 0;
  50. }
  51. inline operator int() const
  52. throw()
  53. {
  54. #ifdef __GNUC__
  55. return __sync_or_and_fetch(&_v,0);
  56. #else
  57. #ifdef __WINDOWS__
  58. return (int)_v;
  59. #else
  60. _l.lock();
  61. int v = _v;
  62. _l.unlock();
  63. return v;
  64. #endif
  65. #endif
  66. }
  67. inline int operator++()
  68. throw()
  69. {
  70. #ifdef __GNUC__
  71. return __sync_add_and_fetch(&_v,1);
  72. #else
  73. #ifdef __WINDOWS__
  74. return ++_v;
  75. #else
  76. _l.lock();
  77. int v = ++_v;
  78. _l.unlock();
  79. return v;
  80. #endif
  81. #endif
  82. }
  83. inline int operator--()
  84. throw()
  85. {
  86. #ifdef __GNUC__
  87. return __sync_sub_and_fetch(&_v,1);
  88. #else
  89. #ifdef __WINDOWS__
  90. return --_v;
  91. #else
  92. _l.lock();
  93. int v = --_v;
  94. _l.unlock();
  95. return v;
  96. #endif
  97. #endif
  98. }
  99. private:
  100. #ifdef __WINDOWS__
  101. std::atomic_int _v;
  102. #else
  103. int _v;
  104. #ifndef __GNUC__
  105. Mutex _l;
  106. #endif
  107. #endif
  108. };
  109. } // namespace ZeroTier
  110. #endif