summaryrefslogtreecommitdiff
path: root/node/Thread.hpp
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@zerotier.com>2013-08-12 16:18:35 -0400
committerAdam Ierymenko <adam.ierymenko@zerotier.com>2013-08-12 16:18:35 -0400
commit36af3d92ecb4148a74c14896f5b6a9dcea0c1700 (patch)
tree0474c2add280ea6a33f25eff9bdf7ada7fe0c70d /node/Thread.hpp
parent2ad80063ec6e7d78d0d5b492f6065563792de05c (diff)
downloadinfinitytier-36af3d92ecb4148a74c14896f5b6a9dcea0c1700.tar.gz
infinitytier-36af3d92ecb4148a74c14896f5b6a9dcea0c1700.zip
Windows build work: condition, mutex, thread, udp socket...
Diffstat (limited to 'node/Thread.hpp')
-rw-r--r--node/Thread.hpp52
1 files changed, 51 insertions, 1 deletions
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 <Windows.h>
+#include <string.h>
+
+namespace ZeroTier {
+
+template<typename C>
+static DWORD WINAPI ___zt_threadMain(LPVOID lpParam)
+{
+ try {
+ ((C *)lpParam)->threadMain();
+ } catch ( ... ) {}
+ return 0;
+}
+
+class Thread
+{
+public:
+ Thread()
+ throw()
+ {
+ _th = NULL;
+ }
+
+ template<typename C>
+ static inline Thread start(C *instance)
+ throw(std::runtime_error)
+ {
+ 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)
+ {
+ 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