AtomicCounter.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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 "Mutex.hpp"
  30. #include "NonCopyable.hpp"
  31. namespace ZeroTier {
  32. /**
  33. * Simple atomic counter supporting increment and decrement
  34. */
  35. class AtomicCounter : NonCopyable
  36. {
  37. public:
  38. /**
  39. * Initialize counter at zero
  40. */
  41. AtomicCounter()
  42. throw() :
  43. _v(0)
  44. {
  45. }
  46. inline int operator*() const
  47. throw()
  48. {
  49. #ifdef __GNUC__
  50. return __sync_or_and_fetch(const_cast <int *> (&_v),0);
  51. #else
  52. _l.lock();
  53. int v = _v;
  54. _l.unlock();
  55. return v;
  56. #endif
  57. }
  58. inline int operator++()
  59. throw()
  60. {
  61. #ifdef __GNUC__
  62. return __sync_add_and_fetch(&_v,1);
  63. #else
  64. _l.lock();
  65. int v = ++_v;
  66. _l.unlock();
  67. return v;
  68. #endif
  69. }
  70. inline int operator--()
  71. throw()
  72. {
  73. #ifdef __GNUC__
  74. return __sync_sub_and_fetch(&_v,1);
  75. #else
  76. _l.lock();
  77. int v = --_v;
  78. _l.unlock();
  79. return v;
  80. #endif
  81. }
  82. inline bool operator==(const AtomicCounter &i) const throw() { return (**this == *i); }
  83. inline bool operator!=(const AtomicCounter &i) const throw() { return (**this != *i); }
  84. inline bool operator>(const AtomicCounter &i) const throw() { return (**this > *i); }
  85. inline bool operator<(const AtomicCounter &i) const throw() { return (**this < *i); }
  86. inline bool operator>=(const AtomicCounter &i) const throw() { return (**this >= *i); }
  87. inline bool operator<=(const AtomicCounter &i) const throw() { return (**this <= *i); }
  88. inline bool operator==(const int i) const throw() { return (**this == i); }
  89. inline bool operator!=(const int i) const throw() { return (**this != i); }
  90. inline bool operator>(const int i) const throw() { return (**this > i); }
  91. inline bool operator<(const int i) const throw() { return (**this < i); }
  92. inline bool operator>=(const int i) const throw() { return (**this >= i); }
  93. inline bool operator<=(const int i) const throw() { return (**this <= i); }
  94. private:
  95. int _v;
  96. #ifndef __GNUC__
  97. Mutex _l;
  98. #endif
  99. };
  100. } // namespace ZeroTier
  101. #endif