|
@@ -24,69 +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 on x64 systems with GCC and CLANG (Mac, Linux) -- this is really fast as long as locking durations are very short
|
|
|
|
-class Mutex
|
|
|
|
-{
|
|
|
|
-public:
|
|
|
|
- Mutex() :
|
|
|
|
- nextTicket(0),
|
|
|
|
- nowServing(0)
|
|
|
|
- {
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- inline void unlock() const
|
|
|
|
- {
|
|
|
|
- ++(const_cast<Mutex *>(this)->nowServing);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Uses C++ contexts and constructor/destructor to lock/unlock automatically
|
|
|
|
- */
|
|
|
|
- class Lock
|
|
|
|
- {
|
|
|
|
- public:
|
|
|
|
- Lock(Mutex &m) :
|
|
|
|
- _m(&m)
|
|
|
|
- {
|
|
|
|
- m.lock();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Lock(const Mutex &m) :
|
|
|
|
- _m(const_cast<Mutex *>(&m))
|
|
|
|
- {
|
|
|
|
- _m->lock();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- ~Lock()
|
|
|
|
- {
|
|
|
|
- _m->unlock();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private:
|
|
|
|
- Mutex *const _m;
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
-private:
|
|
|
|
- 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
|
|
{
|
|
{
|
|
@@ -142,11 +79,9 @@ private:
|
|
pthread_mutex_t _mh;
|
|
pthread_mutex_t _mh;
|
|
};
|
|
};
|
|
|
|
|
|
-#endif
|
|
|
|
-
|
|
|
|
} // namespace ZeroTier
|
|
} // namespace ZeroTier
|
|
|
|
|
|
-#endif // Apple / Linux
|
|
|
|
|
|
+#endif
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
#ifdef __WINDOWS__
|
|
|
|
|