diff --git a/node/Mutex.hpp b/node/Mutex.hpp index 9c67e7935..2124e10e0 100644 --- a/node/Mutex.hpp +++ b/node/Mutex.hpp @@ -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(this)->nextTicket),1); - while (nowServing != myTicket) { - __asm__ __volatile__("rep;nop"::); - __asm__ __volatile__("":::"memory"); - } - } - - ZT_ALWAYS_INLINE void unlock() const { ++(const_cast(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(&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__ diff --git a/node/Packet.cpp b/node/Packet.cpp index 24994d60e..142b78658 100644 --- a/node/Packet.cpp +++ b/node/Packet.cpp @@ -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();