task (Mac OS X):

- Now uses a bool flag to check the thread state instead of checking the thread variable directly. This should make the code more compatible between FreeBSD-based OSes and Linux-based OSes.
- Changes the thread variable visibility from public to private.
This commit is contained in:
rogerman 2011-11-14 23:11:21 +00:00
parent c87c49c17d
commit ffcca74387
1 changed files with 13 additions and 10 deletions

View File

@ -160,6 +160,10 @@ void* Task::Impl::finish()
#elif defined(__APPLE__) #elif defined(__APPLE__)
class Task::Impl { class Task::Impl {
private:
pthread_t _thread;
bool _isThreadRunning;
public: public:
Impl(); Impl();
~Impl(); ~Impl();
@ -169,10 +173,8 @@ public:
void* finish(); void* finish();
void shutdown(); void shutdown();
pthread_t thread;
pthread_mutex_t mutex; pthread_mutex_t mutex;
pthread_cond_t condWork; pthread_cond_t condWork;
TWork work; TWork work;
void *param; void *param;
void *ret; void *ret;
@ -208,7 +210,7 @@ void* taskProc(void *arg)
Task::Impl::Impl() Task::Impl::Impl()
{ {
thread = -1; _isThreadRunning = false;
work = NULL; work = NULL;
param = NULL; param = NULL;
ret = NULL; ret = NULL;
@ -229,7 +231,7 @@ void Task::Impl::start(bool spinlock)
{ {
pthread_mutex_lock(&this->mutex); pthread_mutex_lock(&this->mutex);
if (this->thread != -1) { if (this->_isThreadRunning) {
pthread_mutex_unlock(&this->mutex); pthread_mutex_unlock(&this->mutex);
return; return;
} }
@ -238,7 +240,8 @@ void Task::Impl::start(bool spinlock)
this->param = NULL; this->param = NULL;
this->ret = NULL; this->ret = NULL;
this->exitThread = false; this->exitThread = false;
pthread_create(&this->thread, NULL, &taskProc, this); pthread_create(&this->_thread, NULL, &taskProc, this);
this->_isThreadRunning = true;
pthread_mutex_unlock(&this->mutex); pthread_mutex_unlock(&this->mutex);
} }
@ -247,7 +250,7 @@ void Task::Impl::execute(const TWork &work, void *param)
{ {
pthread_mutex_lock(&this->mutex); pthread_mutex_lock(&this->mutex);
if (work == NULL || this->thread == -1) { if (work == NULL || !this->_isThreadRunning) {
pthread_mutex_unlock(&this->mutex); pthread_mutex_unlock(&this->mutex);
return; return;
} }
@ -265,7 +268,7 @@ void* Task::Impl::finish()
pthread_mutex_lock(&this->mutex); pthread_mutex_lock(&this->mutex);
if (this->thread == -1) { if (!this->_isThreadRunning) {
pthread_mutex_unlock(&this->mutex); pthread_mutex_unlock(&this->mutex);
return returnValue; return returnValue;
} }
@ -285,7 +288,7 @@ void Task::Impl::shutdown()
{ {
pthread_mutex_lock(&this->mutex); pthread_mutex_lock(&this->mutex);
if (this->thread == -1) { if (!this->_isThreadRunning) {
pthread_mutex_unlock(&this->mutex); pthread_mutex_unlock(&this->mutex);
return; return;
} }
@ -296,10 +299,10 @@ void Task::Impl::shutdown()
pthread_mutex_unlock(&this->mutex); pthread_mutex_unlock(&this->mutex);
pthread_join(this->thread, NULL); pthread_join(this->_thread, NULL);
pthread_mutex_lock(&this->mutex); pthread_mutex_lock(&this->mutex);
this->thread = -1; this->_isThreadRunning = false;
pthread_mutex_unlock(&this->mutex); pthread_mutex_unlock(&this->mutex);
} }