summaryrefslogtreecommitdiff
path: root/node/AtomicCounter.hpp
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@zerotier.com>2014-02-28 09:15:29 -0800
committerAdam Ierymenko <adam.ierymenko@zerotier.com>2014-02-28 09:15:29 -0800
commitd27251ec4ec16e1b87e4e05fd34ea8398936d4d6 (patch)
tree26f5be378c7516271961ac99e83801f1a6940cac /node/AtomicCounter.hpp
parent9ae6403346b03b59d302d27206c93ba696d585cb (diff)
downloadinfinitytier-d27251ec4ec16e1b87e4e05fd34ea8398936d4d6.tar.gz
infinitytier-d27251ec4ec16e1b87e4e05fd34ea8398936d4d6.zip
Make AtomicCounter use <atomic> on Windows (eventually this will replace it on other platforms), and some installer work.
Diffstat (limited to 'node/AtomicCounter.hpp')
-rw-r--r--node/AtomicCounter.hpp44
1 files changed, 26 insertions, 18 deletions
diff --git a/node/AtomicCounter.hpp b/node/AtomicCounter.hpp
index c801e56e..9dc5fd05 100644
--- a/node/AtomicCounter.hpp
+++ b/node/AtomicCounter.hpp
@@ -28,9 +28,15 @@
#ifndef ZT_ATOMICCOUNTER_HPP
#define ZT_ATOMICCOUNTER_HPP
+#include "Constants.hpp"
#include "Mutex.hpp"
#include "NonCopyable.hpp"
+#ifdef __WINDOWS__
+// <atomic> will replace this whole class eventually once it's ubiquitous
+#include <atomic>
+#endif
+
namespace ZeroTier {
/**
@@ -43,22 +49,26 @@ public:
* Initialize counter at zero
*/
AtomicCounter()
- throw() :
- _v(0)
+ throw()
{
+ _v = 0;
}
- inline int operator*() const
+ inline operator int() const
throw()
{
#ifdef __GNUC__
- return __sync_or_and_fetch(const_cast <int *> (&_v),0);
+ return __sync_or_and_fetch(&_v,0);
+#else
+#ifdef __WINDOWS__
+ return (int)_v;
#else
_l.lock();
int v = _v;
_l.unlock();
return v;
#endif
+#endif
}
inline int operator++()
@@ -67,11 +77,15 @@ public:
#ifdef __GNUC__
return __sync_add_and_fetch(&_v,1);
#else
+#ifdef __WINDOWS__
+ return ++_v;
+#else
_l.lock();
int v = ++_v;
_l.unlock();
return v;
#endif
+#endif
}
inline int operator--()
@@ -80,32 +94,26 @@ public:
#ifdef __GNUC__
return __sync_sub_and_fetch(&_v,1);
#else
+#ifdef __WINDOWS__
+ return --_v;
+#else
_l.lock();
int v = --_v;
_l.unlock();
return v;
#endif
+#endif
}
- inline bool operator==(const AtomicCounter &i) const throw() { return (**this == *i); }
- inline bool operator!=(const AtomicCounter &i) const throw() { return (**this != *i); }
- inline bool operator>(const AtomicCounter &i) const throw() { return (**this > *i); }
- inline bool operator<(const AtomicCounter &i) const throw() { return (**this < *i); }
- inline bool operator>=(const AtomicCounter &i) const throw() { return (**this >= *i); }
- inline bool operator<=(const AtomicCounter &i) const throw() { return (**this <= *i); }
-
- inline bool operator==(const int i) const throw() { return (**this == i); }
- inline bool operator!=(const int i) const throw() { return (**this != i); }
- inline bool operator>(const int i) const throw() { return (**this > i); }
- inline bool operator<(const int i) const throw() { return (**this < i); }
- inline bool operator>=(const int i) const throw() { return (**this >= i); }
- inline bool operator<=(const int i) const throw() { return (**this <= i); }
-
private:
+#ifdef __WINDOWS__
+ std::atomic_int _v;
+#else
int _v;
#ifndef __GNUC__
Mutex _l;
#endif
+#endif
};
} // namespace ZeroTier