This commit is contained in:
Adam Ierymenko 2019-08-28 14:28:07 -07:00
parent 2f7d3e655a
commit 63775723c1
No known key found for this signature in database
GPG Key ID: C8877CF2D7A5D7F3
2 changed files with 2 additions and 43 deletions

View File

@ -24,45 +24,6 @@
namespace ZeroTier {
#if defined(__GNUC__) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
// Inline ticket lock with yield for x64 systems, provides much better performance when there is no contention.
class Mutex
{
public:
ZT_ALWAYS_INLINE Mutex() : nextTicket(0),nowServing(0) {}
ZT_ALWAYS_INLINE void lock() const
{
const uint16_t myTicket = __sync_fetch_and_add(&(const_cast<Mutex *>(this)->nextTicket),1);
while (nowServing != myTicket) {
__asm__ __volatile__("rep;nop"::);
__asm__ __volatile__("":::"memory");
}
}
ZT_ALWAYS_INLINE void unlock() const { ++(const_cast<Mutex *>(this)->nowServing); }
class Lock
{
public:
ZT_ALWAYS_INLINE Lock(Mutex &m) : _m(&m) { m.lock(); }
ZT_ALWAYS_INLINE Lock(const Mutex &m) : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
ZT_ALWAYS_INLINE ~Lock() { _m->unlock(); }
private:
Mutex *const _m;
};
private:
inline Mutex(const Mutex &) {}
const Mutex &operator=(const Mutex &) { return *this; }
uint16_t nextTicket;
uint16_t nowServing;
};
#else
// libpthread based mutex lock
class Mutex
{
@ -89,11 +50,9 @@ private:
pthread_mutex_t _mh;
};
#endif
} // namespace ZeroTier
#endif // Apple / Linux
#endif
#ifdef __WINDOWS__

View File

@ -938,7 +938,7 @@ uint64_t Packet::nextPacketId()
static uint64_t ctr = 0;
static Mutex lock;
lock.lock();
if (unlikely(ctr == 0))
while (unlikely(ctr == 0))
Utils::getSecureRandom(&ctr,sizeof(ctr));
const uint64_t i = ctr++;
lock.unlock();