2013-07-04 16:56:19 -04:00
|
|
|
/*
|
2019-08-23 09:23:39 -07:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2013-07-04 16:56:19 -04:00
|
|
|
*
|
2019-08-23 09:23:39 -07:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2013-07-04 16:56:19 -04:00
|
|
|
*
|
2020-08-20 12:51:39 -07:00
|
|
|
* Change Date: 2025-01-01
|
2013-07-04 16:56:19 -04:00
|
|
|
*
|
2019-08-23 09:23:39 -07:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2013-07-04 16:56:19 -04:00
|
|
|
*/
|
2019-08-23 09:23:39 -07:00
|
|
|
/****/
|
2013-07-04 16:56:19 -04:00
|
|
|
|
2013-12-06 16:49:20 -08:00
|
|
|
#ifndef ZT_MUTEX_HPP
|
|
|
|
#define ZT_MUTEX_HPP
|
2013-07-04 16:56:19 -04:00
|
|
|
|
2013-08-12 16:18:35 -04:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 16:56:19 -04:00
|
|
|
|
2013-08-12 16:18:35 -04:00
|
|
|
#ifdef __UNIX_LIKE__
|
2013-07-04 16:56:19 -04:00
|
|
|
|
2017-08-23 18:52:32 -07:00
|
|
|
#include <stdint.h>
|
2013-07-04 16:56:19 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2017-08-23 18:52:32 -07:00
|
|
|
// libpthread based mutex lock
|
2018-01-26 21:34:56 -05:00
|
|
|
class Mutex
|
2017-08-23 18:52:32 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Mutex()
|
|
|
|
{
|
|
|
|
pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Mutex()
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy(&_mh);
|
2013-07-04 16:56:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void lock() const
|
|
|
|
{
|
2017-08-31 20:47:44 -04:00
|
|
|
pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh));
|
2013-07-04 16:56:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void unlock() const
|
|
|
|
{
|
2017-08-31 20:47:44 -04:00
|
|
|
pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh));
|
2013-07-04 16:56:19 -04:00
|
|
|
}
|
|
|
|
|
2018-01-26 21:34:56 -05:00
|
|
|
class Lock
|
2013-07-04 16:56:19 -04:00
|
|
|
{
|
|
|
|
public:
|
2017-07-17 14:21:09 -07:00
|
|
|
Lock(Mutex &m) :
|
2013-07-04 16:56:19 -04:00
|
|
|
_m(&m)
|
|
|
|
{
|
|
|
|
m.lock();
|
|
|
|
}
|
|
|
|
|
2017-07-17 14:21:09 -07:00
|
|
|
Lock(const Mutex &m) :
|
2013-07-04 16:56:19 -04:00
|
|
|
_m(const_cast<Mutex *>(&m))
|
|
|
|
{
|
|
|
|
_m->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Lock()
|
|
|
|
{
|
|
|
|
_m->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex *const _m;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2018-01-26 21:34:56 -05:00
|
|
|
Mutex(const Mutex &) {}
|
|
|
|
const Mutex &operator=(const Mutex &) { return *this; }
|
|
|
|
|
2013-07-04 16:56:19 -04:00
|
|
|
pthread_mutex_t _mh;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
2021-11-12 16:44:54 -05:00
|
|
|
#endif
|
2013-07-04 16:56:19 -04:00
|
|
|
|
2013-08-12 16:18:35 -04:00
|
|
|
#ifdef __WINDOWS__
|
2013-07-04 16:56:19 -04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2021-12-27 14:07:35 -05:00
|
|
|
#include <windows.h>
|
2013-07-04 16:56:19 -04:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2017-08-23 18:52:32 -07:00
|
|
|
// Windows critical section based lock
|
2018-01-26 21:34:56 -05:00
|
|
|
class Mutex
|
2013-07-04 16:56:19 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Mutex()
|
|
|
|
{
|
|
|
|
InitializeCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Mutex()
|
|
|
|
{
|
|
|
|
DeleteCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
2017-09-27 15:05:13 -07:00
|
|
|
inline void lock()
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void unlock()
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&_cs);
|
|
|
|
}
|
|
|
|
|
2013-07-04 16:56:19 -04:00
|
|
|
inline void lock() const
|
|
|
|
{
|
|
|
|
(const_cast <Mutex *> (this))->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void unlock() const
|
|
|
|
{
|
|
|
|
(const_cast <Mutex *> (this))->unlock();
|
|
|
|
}
|
|
|
|
|
2018-01-26 21:34:56 -05:00
|
|
|
class Lock
|
2013-07-04 16:56:19 -04:00
|
|
|
{
|
|
|
|
public:
|
2017-07-17 14:21:09 -07:00
|
|
|
Lock(Mutex &m) :
|
2013-07-04 16:56:19 -04:00
|
|
|
_m(&m)
|
|
|
|
{
|
|
|
|
m.lock();
|
|
|
|
}
|
|
|
|
|
2017-07-17 14:21:09 -07:00
|
|
|
Lock(const Mutex &m) :
|
2013-07-04 16:56:19 -04:00
|
|
|
_m(const_cast<Mutex *>(&m))
|
|
|
|
{
|
|
|
|
_m->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Lock()
|
|
|
|
{
|
|
|
|
_m->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex *const _m;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2018-01-26 21:34:56 -05:00
|
|
|
Mutex(const Mutex &) {}
|
|
|
|
const Mutex &operator=(const Mutex &) { return *this; }
|
|
|
|
|
2013-07-04 16:56:19 -04:00
|
|
|
CRITICAL_SECTION _cs;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
#endif
|