Implement "weak pointer" behavior on Topology Path canonicalization hash table.

This commit is contained in:
Adam Ierymenko 2016-09-02 12:34:02 -07:00
parent d1101441b3
commit 4931e44998
3 changed files with 49 additions and 57 deletions

View file

@ -20,11 +20,9 @@
#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
#ifndef __GNUC__
#include <atomic>
#endif
@ -36,75 +34,34 @@ namespace ZeroTier {
class AtomicCounter : NonCopyable
{
public:
/**
* Initialize counter at zero
*/
AtomicCounter()
throw()
{
_v = 0;
}
inline operator int() const
throw()
{
#ifdef __GNUC__
return __sync_or_and_fetch(const_cast <volatile int *>(&_v),0);
#else
#ifdef __WINDOWS__
return (int)_v;
#else
_l.lock();
int v = _v;
_l.unlock();
return v;
#endif
#endif
}
inline int operator++()
throw()
{
#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--()
throw()
{
#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
}
private:
#ifdef __WINDOWS__
std::atomic_int _v;
#else
#ifdef __GNUC__
int _v;
#ifndef __GNUC__
#warning Neither __WINDOWS__ nor __GNUC__ so AtomicCounter using Mutex
Mutex _l;
#endif
#else
std::atomic_int _v;
#endif
};