mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-07 13:03:45 +02:00
.
This commit is contained in:
parent
2f7d3e655a
commit
63775723c1
2 changed files with 2 additions and 43 deletions
|
@ -24,45 +24,6 @@
|
||||||
|
|
||||||
namespace ZeroTier {
|
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
|
// libpthread based mutex lock
|
||||||
class Mutex
|
class Mutex
|
||||||
{
|
{
|
||||||
|
@ -89,11 +50,9 @@ private:
|
||||||
pthread_mutex_t _mh;
|
pthread_mutex_t _mh;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} // namespace ZeroTier
|
} // namespace ZeroTier
|
||||||
|
|
||||||
#endif // Apple / Linux
|
#endif
|
||||||
|
|
||||||
#ifdef __WINDOWS__
|
#ifdef __WINDOWS__
|
||||||
|
|
||||||
|
|
|
@ -938,7 +938,7 @@ uint64_t Packet::nextPacketId()
|
||||||
static uint64_t ctr = 0;
|
static uint64_t ctr = 0;
|
||||||
static Mutex lock;
|
static Mutex lock;
|
||||||
lock.lock();
|
lock.lock();
|
||||||
if (unlikely(ctr == 0))
|
while (unlikely(ctr == 0))
|
||||||
Utils::getSecureRandom(&ctr,sizeof(ctr));
|
Utils::getSecureRandom(&ctr,sizeof(ctr));
|
||||||
const uint64_t i = ctr++;
|
const uint64_t i = ctr++;
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
|
Loading…
Add table
Reference in a new issue