2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2018-01-08 22:33:28 +00:00
|
|
|
* Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-28 03:47:25 +00:00
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
* of your own application.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_MUTEX_HPP
|
|
|
|
#define ZT_MUTEX_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-12 20:18:35 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "NonCopyable.hpp"
|
|
|
|
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __UNIX_LIKE__
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2017-08-24 01:52:32 +00:00
|
|
|
#include <stdint.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2018-01-11 22:31:19 +00:00
|
|
|
#if defined(__GNUC__) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
|
2017-08-24 01:52:32 +00:00
|
|
|
|
|
|
|
// Inline ticket lock on x64 systems with GCC and CLANG (Mac, Linux) -- this is really fast as long as locking durations are very short
|
2013-07-04 20:56:19 +00:00
|
|
|
class Mutex : NonCopyable
|
|
|
|
{
|
|
|
|
public:
|
2017-08-24 01:52:32 +00:00
|
|
|
Mutex() :
|
|
|
|
nextTicket(0),
|
|
|
|
nowServing(0)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-24 01:52:32 +00:00
|
|
|
inline void lock() const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2017-08-24 01:52:32 +00:00
|
|
|
const uint16_t myTicket = __sync_fetch_and_add(&(const_cast<Mutex *>(this)->nextTicket),1);
|
|
|
|
while (nowServing != myTicket) {
|
|
|
|
__asm__ __volatile__("rep;nop"::);
|
|
|
|
__asm__ __volatile__("":::"memory");
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 01:52:32 +00:00
|
|
|
inline void unlock() const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2017-08-24 01:52:32 +00:00
|
|
|
++(const_cast<Mutex *>(this)->nowServing);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 01:52:32 +00:00
|
|
|
/**
|
|
|
|
* Uses C++ contexts and constructor/destructor to lock/unlock automatically
|
|
|
|
*/
|
|
|
|
class Lock : NonCopyable
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2017-08-24 01:52:32 +00:00
|
|
|
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:
|
|
|
|
uint16_t nextTicket;
|
|
|
|
uint16_t nowServing;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// libpthread based mutex lock
|
|
|
|
class Mutex : NonCopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Mutex()
|
|
|
|
{
|
|
|
|
pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Mutex()
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&_mh);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void lock() const
|
|
|
|
{
|
2017-09-01 00:47:44 +00:00
|
|
|
pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh));
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void unlock() const
|
|
|
|
{
|
2017-09-01 00:47:44 +00:00
|
|
|
pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh));
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Lock : NonCopyable
|
|
|
|
{
|
|
|
|
public:
|
2017-07-17 21:21:09 +00:00
|
|
|
Lock(Mutex &m) :
|
2013-07-04 20:56:19 +00:00
|
|
|
_m(&m)
|
|
|
|
{
|
|
|
|
m.lock();
|
|
|
|
}
|
|
|
|
|
2017-07-17 21:21:09 +00:00
|
|
|
Lock(const Mutex &m) :
|
2013-07-04 20:56:19 +00:00
|
|
|
_m(const_cast<Mutex *>(&m))
|
|
|
|
{
|
|
|
|
_m->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Lock()
|
|
|
|
{
|
|
|
|
_m->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex *const _m;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_mutex_t _mh;
|
|
|
|
};
|
|
|
|
|
2017-08-24 01:52:32 +00:00
|
|
|
#endif
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif // Apple / Linux
|
|
|
|
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __WINDOWS__
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2017-08-24 01:52:32 +00:00
|
|
|
// Windows critical section based lock
|
2013-07-04 20:56:19 +00:00
|
|
|
class Mutex : NonCopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Mutex()
|
|
|
|
{
|
|
|
|
InitializeCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Mutex()
|
|
|
|
{
|
|
|
|
DeleteCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
2017-09-27 22:05:13 +00:00
|
|
|
inline void lock()
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void unlock()
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
inline void lock() const
|
|
|
|
{
|
|
|
|
(const_cast <Mutex *> (this))->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void unlock() const
|
|
|
|
{
|
|
|
|
(const_cast <Mutex *> (this))->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
class Lock : NonCopyable
|
|
|
|
{
|
|
|
|
public:
|
2017-07-17 21:21:09 +00:00
|
|
|
Lock(Mutex &m) :
|
2013-07-04 20:56:19 +00:00
|
|
|
_m(&m)
|
|
|
|
{
|
|
|
|
m.lock();
|
|
|
|
}
|
|
|
|
|
2017-07-17 21:21:09 +00:00
|
|
|
Lock(const Mutex &m) :
|
2013-07-04 20:56:19 +00:00
|
|
|
_m(const_cast<Mutex *>(&m))
|
|
|
|
{
|
|
|
|
_m->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Lock()
|
|
|
|
{
|
|
|
|
_m->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex *const _m;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
CRITICAL_SECTION _cs;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
#endif
|