X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fpthreadwrapper%2Fthread.cpp;h=07cb6898714642846cdcaef9560b7c09cc72d9dc;hb=9b22dd53fe62e312c1647310b7ec43aa127090af;hp=7a4e972906769a880af6886506bca0e9d1064ee4;hpb=b9c3763a932cebaa015a27fe111017f6f34dfbaa;p=fms.git diff --git a/src/pthreadwrapper/thread.cpp b/src/pthreadwrapper/thread.cpp index 7a4e972..07cb689 100644 --- a/src/pthreadwrapper/thread.cpp +++ b/src/pthreadwrapper/thread.cpp @@ -9,12 +9,36 @@ namespace PThread { +void Sleep(const long ms) +{ + pthread_cond_t c; + pthread_mutex_t m; + timespec t; + timeb tb; + + pthread_mutex_init(&m,NULL); + pthread_cond_init(&c,NULL); + + ftime(&tb); + + t.tv_sec=tb.time+(ms/1000); + t.tv_nsec=((1000000L)*(long)tb.millitm)+((1000000L)*(ms%1000)); + + pthread_mutex_lock(&m); + pthread_cond_timedwait(&c,&m,&t); + pthread_mutex_unlock(&m); + + pthread_cond_destroy(&c); + pthread_mutex_destroy(&m); +} + Thread::Thread() { m_running=false; m_cancelled=false; m_runnable=0; m_threadnum=0; + m_threadcleaned=true; } Thread::Thread(Runnable *runnable) @@ -23,6 +47,7 @@ Thread::Thread(Runnable *runnable) m_cancelled=false; m_runnable=runnable; m_threadnum=0; + m_threadcleaned=true; if(m_runnable) { m_runnable->m_thread=this; @@ -34,6 +59,10 @@ Thread::~Thread() { Cancel(); Join(); + if(m_threadcleaned==false) + { + pthread_detach(m_thread); + } if(m_runnable) { delete m_runnable; @@ -65,6 +94,7 @@ void Thread::Join() if(m_running) { pthread_join(m_thread,NULL); + m_threadcleaned=true; } } @@ -88,12 +118,16 @@ void Thread::Sleep(const long ms) pthread_mutex_lock(&m); pthread_cond_timedwait(&c,&m,&t); pthread_mutex_unlock(&m); + + pthread_cond_destroy(&c); + pthread_mutex_destroy(&m); } } void Thread::Start() { m_running=true; + m_threadcleaned=false; m_threadnum=pthread_create(&m_thread,NULL,Thread::EntryPoint,this); }