From d27251ec4ec16e1b87e4e05fd34ea8398936d4d6 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 28 Feb 2014 09:15:29 -0800 Subject: Make AtomicCounter use on Windows (eventually this will replace it on other platforms), and some installer work. --- node/AtomicCounter.hpp | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'node') 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__ +// will replace this whole class eventually once it's ubiquitous +#include +#endif + namespace ZeroTier { /** @@ -43,21 +49,25 @@ 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 (&_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 } @@ -66,11 +76,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 } @@ -79,33 +93,27 @@ 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 -- cgit v1.2.3