2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2019-01-14 18:25:53 +00:00
|
|
|
* Copyright (C) 2011-2019 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
|
2019-01-14 18:25:53 +00:00
|
|
|
* 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_THREAD_HPP
|
|
|
|
#define ZT_THREAD_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-05 20:06:16 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2015-04-09 02:03:30 +00:00
|
|
|
#include "../node/Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-05 20:06:16 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
|
2014-08-22 00:49:05 +00:00
|
|
|
#include <WinSock2.h>
|
2013-08-12 20:18:35 +00:00
|
|
|
#include <Windows.h>
|
|
|
|
#include <string.h>
|
2017-01-30 16:01:36 +00:00
|
|
|
|
2015-04-24 22:05:28 +00:00
|
|
|
#include "../node/Mutex.hpp"
|
2013-08-12 20:18:35 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
template<typename C>
|
|
|
|
static DWORD WINAPI ___zt_threadMain(LPVOID lpParam)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
((C *)lpParam)->threadMain();
|
|
|
|
} catch ( ... ) {}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Thread()
|
|
|
|
{
|
|
|
|
_th = NULL;
|
2014-08-22 00:49:05 +00:00
|
|
|
_tid = 0;
|
2013-08-12 20:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename C>
|
|
|
|
static inline Thread start(C *instance)
|
|
|
|
{
|
|
|
|
Thread t;
|
|
|
|
t._th = CreateThread(NULL,0,&___zt_threadMain<C>,(LPVOID)instance,0,&t._tid);
|
|
|
|
if (t._th == NULL)
|
|
|
|
throw std::runtime_error("CreateThread() failed");
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void join(const Thread &t)
|
|
|
|
{
|
2014-08-22 00:49:05 +00:00
|
|
|
if (t._th != NULL) {
|
|
|
|
for(;;) {
|
|
|
|
DWORD ec = STILL_ACTIVE;
|
|
|
|
GetExitCodeThread(t._th,&ec);
|
|
|
|
if (ec == STILL_ACTIVE)
|
|
|
|
WaitForSingleObject(t._th,1000);
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 20:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void sleep(unsigned long ms)
|
|
|
|
{
|
|
|
|
Sleep((DWORD)ms);
|
|
|
|
}
|
|
|
|
|
2014-03-26 00:31:03 +00:00
|
|
|
// Not available on *nix platforms
|
|
|
|
static inline void cancelIO(const Thread &t)
|
|
|
|
{
|
2017-10-10 19:21:52 +00:00
|
|
|
#if !defined(__MINGW32__) && !defined(__MINGW64__) // CancelSynchronousIo not available in MSYS2
|
2014-03-26 00:31:03 +00:00
|
|
|
if (t._th != NULL)
|
|
|
|
CancelSynchronousIo(t._th);
|
2017-10-10 19:21:52 +00:00
|
|
|
#endif
|
2014-03-26 00:31:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 03:51:02 +00:00
|
|
|
inline operator bool() const { return (_th != NULL); }
|
2014-07-31 21:09:32 +00:00
|
|
|
|
2013-08-12 20:18:35 +00:00
|
|
|
private:
|
|
|
|
HANDLE _th;
|
|
|
|
DWORD _tid;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
2013-08-05 20:06:16 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2013-08-05 20:06:16 +00:00
|
|
|
template<typename C>
|
|
|
|
static void *___zt_threadMain(void *instance)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
((C *)instance)->threadMain();
|
|
|
|
} catch ( ... ) {}
|
|
|
|
return (void *)0;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2013-08-08 13:19:36 +00:00
|
|
|
* A thread identifier, and static methods to start and join threads
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-08-05 20:06:16 +00:00
|
|
|
class Thread
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-08-05 20:06:16 +00:00
|
|
|
Thread()
|
|
|
|
{
|
2017-04-25 03:51:02 +00:00
|
|
|
memset(this,0,sizeof(Thread));
|
2016-07-28 17:58:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 20:06:16 +00:00
|
|
|
Thread(const Thread &t)
|
|
|
|
{
|
2017-04-25 03:51:02 +00:00
|
|
|
memcpy(this,&t,sizeof(Thread));
|
2013-08-05 20:06:16 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-05 20:06:16 +00:00
|
|
|
inline Thread &operator=(const Thread &t)
|
|
|
|
{
|
2017-04-25 03:51:02 +00:00
|
|
|
memcpy(this,&t,sizeof(Thread));
|
2013-08-05 20:06:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2013-08-05 20:06:16 +00:00
|
|
|
* Start a new thread
|
|
|
|
*
|
|
|
|
* @param instance Instance whose threadMain() method gets called by new thread
|
|
|
|
* @return Thread identifier
|
|
|
|
* @throws std::runtime_error Unable to create thread
|
2013-08-08 13:19:36 +00:00
|
|
|
* @tparam C Class containing threadMain()
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-08-08 13:19:36 +00:00
|
|
|
template<typename C>
|
2013-08-05 20:06:16 +00:00
|
|
|
static inline Thread start(C *instance)
|
|
|
|
{
|
|
|
|
Thread t;
|
2017-04-25 03:51:02 +00:00
|
|
|
pthread_attr_t tattr;
|
|
|
|
pthread_attr_init(&tattr);
|
|
|
|
// This corrects for systems with abnormally small defaults (musl) and also
|
|
|
|
// shrinks the stack on systems with large defaults to save a bit of memory.
|
|
|
|
pthread_attr_setstacksize(&tattr,ZT_THREAD_MIN_STACK_SIZE);
|
|
|
|
if (pthread_create(&t._tid,&tattr,&___zt_threadMain<C>,instance)) {
|
|
|
|
pthread_attr_destroy(&tattr);
|
2013-08-05 20:06:16 +00:00
|
|
|
throw std::runtime_error("pthread_create() failed, unable to create thread");
|
2017-04-25 03:51:02 +00:00
|
|
|
} else {
|
|
|
|
t._started = true;
|
|
|
|
pthread_attr_destroy(&tattr);
|
|
|
|
}
|
2013-08-05 20:06:16 +00:00
|
|
|
return t;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2014-07-31 21:09:32 +00:00
|
|
|
* Join to a thread, waiting for it to terminate (does nothing on null Thread values)
|
2013-08-05 20:06:16 +00:00
|
|
|
*
|
|
|
|
* @param t Thread to join
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-08-05 20:06:16 +00:00
|
|
|
static inline void join(const Thread &t)
|
|
|
|
{
|
2014-07-31 21:09:32 +00:00
|
|
|
if (t._started)
|
|
|
|
pthread_join(t._tid,(void **)0);
|
2013-08-05 20:06:16 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sleep the current thread
|
|
|
|
*
|
2013-08-05 20:06:16 +00:00
|
|
|
* @param ms Number of milliseconds to sleep
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2014-07-31 21:09:32 +00:00
|
|
|
static inline void sleep(unsigned long ms) { usleep(ms * 1000); }
|
|
|
|
|
2017-04-25 03:51:02 +00:00
|
|
|
inline operator bool() const { return (_started); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
private:
|
2013-08-05 20:06:16 +00:00
|
|
|
pthread_t _tid;
|
2014-07-31 21:09:32 +00:00
|
|
|
volatile bool _started;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
2013-08-05 20:06:16 +00:00
|
|
|
#endif // __WINDOWS__ / !__WINDOWS__
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#endif
|