diff options
Diffstat (limited to 'lib/thread.c')
-rw-r--r-- | lib/thread.c | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/lib/thread.c b/lib/thread.c index 4f2cbfe..7728db6 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -10,6 +10,8 @@ #include <sys/syscall.h> #include <sys/wait.h> #include <sys/time.h> +#include <unistd.h> +#include <stdarg.h> #include "thread.h" @@ -61,12 +63,14 @@ cThread::cThread(const char* Description, bool LowPriority) SetDescription("%s", Description); lowPriority = LowPriority; + pthread_attr_init(&attr); } cThread::~cThread() { Cancel(); // just in case the derived class didn't call it free(description); + pthread_attr_destroy(&attr); } void cThread::SetDescription(const char *Description, ...) @@ -135,7 +139,7 @@ void cThread::SetIOPriority(int priority) #define THREAD_STOP_TIMEOUT 3000 // ms to wait for a thread to stop before newly starting it #define THREAD_STOP_SLEEP 30 // ms to sleep while waiting for a thread to stop -bool cThread::Start(int s) +bool cThread::Start(int s, int stackSize) { silent = s; @@ -153,9 +157,21 @@ bool cThread::Start(int s) } if (!active) { + int res; + active = running = true; - if (pthread_create(&childTid, NULL, (void *(*) (void *))&StartThread, (void *)this) == 0) + if (stackSize == na) + { + res = pthread_create(&childTid, 0, (void*(*)(void*))&StartThread, (void*)this); + } + else + { + pthread_attr_setstacksize(&attr, stackSize); + res = pthread_create(&childTid, &attr, (void*(*)(void*))&StartThread, (void*)this); + } + + if (res == 0) { pthread_detach(childTid); // auto-reap } @@ -195,6 +211,8 @@ void cThread::Cancel(int WaitSeconds) { running = false; + waitCondition.Broadcast(); + if (active && WaitSeconds > -1) { if (WaitSeconds > 0) @@ -241,7 +259,7 @@ cCondWait::~cCondWait() void cCondWait::SleepMs(int TimeoutMs) { cCondWait w; - w.Wait(max(TimeoutMs, 3)); // making sure the time is >2ms to avoid a possible busy wait + w.Wait(std::max(TimeoutMs, 3)); // making sure the time is >2ms to avoid a possible busy wait } bool cCondWait::Wait(int TimeoutMs) |