From b80c229d873ca51bf679ff9df90c8360dca5d0d7 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 31 Jul 2014 14:09:32 -0700 Subject: Tons of code cleanup, refactor Network to use EthernetTapFactory, probably also fix GitHub issue #90 --- node/Thread.hpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'node/Thread.hpp') diff --git a/node/Thread.hpp b/node/Thread.hpp index 6426d5bb..a796af33 100644 --- a/node/Thread.hpp +++ b/node/Thread.hpp @@ -87,6 +87,8 @@ public: CancelSynchronousIo(t._th); } + inline operator bool() const throw() { return (_th != NULL); } + private: HANDLE _th; DWORD _tid; @@ -123,18 +125,21 @@ public: throw() { memset(&_tid,0,sizeof(_tid)); + _started = false; } Thread(const Thread &t) throw() { memcpy(&_tid,&(t._tid),sizeof(_tid)); + _started = t._started; } inline Thread &operator=(const Thread &t) throw() { memcpy(&_tid,&(t._tid),sizeof(_tid)); + _started = t._started; return *this; } @@ -151,19 +156,21 @@ public: throw(std::runtime_error) { Thread t; + t._started = true; if (pthread_create(&t._tid,(const pthread_attr_t *)0,&___zt_threadMain,instance)) throw std::runtime_error("pthread_create() failed, unable to create thread"); return t; } /** - * Join to a thread, waiting for it to terminate + * Join to a thread, waiting for it to terminate (does nothing on null Thread values) * * @param t Thread to join */ static inline void join(const Thread &t) { - pthread_join(t._tid,(void **)0); + if (t._started) + pthread_join(t._tid,(void **)0); } /** @@ -171,13 +178,13 @@ public: * * @param ms Number of milliseconds to sleep */ - static inline void sleep(unsigned long ms) - { - usleep(ms * 1000); - } + static inline void sleep(unsigned long ms) { usleep(ms * 1000); } + + inline operator bool() const throw() { return (_started); } private: pthread_t _tid; + volatile bool _started; }; } // namespace ZeroTier -- cgit v1.2.3