From 36af3d92ecb4148a74c14896f5b6a9dcea0c1700 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 12 Aug 2013 16:18:35 -0400 Subject: Windows build work: condition, mutex, thread, udp socket... --- node/Thread.hpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'node/Thread.hpp') diff --git a/node/Thread.hpp b/node/Thread.hpp index ea75297a..d295fea3 100644 --- a/node/Thread.hpp +++ b/node/Thread.hpp @@ -35,7 +35,57 @@ #ifdef __WINDOWS__ -todo need windows; +#include +#include + +namespace ZeroTier { + +template +static DWORD WINAPI ___zt_threadMain(LPVOID lpParam) +{ + try { + ((C *)lpParam)->threadMain(); + } catch ( ... ) {} + return 0; +} + +class Thread +{ +public: + Thread() + throw() + { + _th = NULL; + } + + template + static inline Thread start(C *instance) + throw(std::runtime_error) + { + Thread t; + t._th = CreateThread(NULL,0,&___zt_threadMain,(LPVOID)instance,0,&t._tid); + if (t._th == NULL) + throw std::runtime_error("CreateThread() failed"); + return t; + } + + static inline void join(const Thread &t) + { + if (t._th != NULL) + WaitForSingleObject(t._th,INFINITE); + } + + static inline void sleep(unsigned long ms) + { + Sleep((DWORD)ms); + } + +private: + HANDLE _th; + DWORD _tid; +}; + +} // namespace ZeroTier #else -- cgit v1.2.3