diff options
| author | Bryan Hoyle <starfoxprime@gmail.com> | 2012-06-20 18:41:17 (GMT) |
|---|---|---|
| committer | Bryan Hoyle <starfoxprime@gmail.com> | 2012-06-20 18:41:17 (GMT) |
| commit | 9953f4518ec2d98dbe950e13a94a5e845f725f6c (patch) | |
| tree | 67d9b56b7a4772c817f753bef8cdc81ba7833b7b /src/tasks/Task.cpp | |
| parent | b35255b722a5259f34cfc9b7569f58d11658c3b9 (diff) | |
| parent | ad76b293d8fc257a7efe0cbcf6f7fb9380616030 (diff) | |
| download | powder-9953f4518ec2d98dbe950e13a94a5e845f725f6c.zip powder-9953f4518ec2d98dbe950e13a94a5e845f725f6c.tar.gz | |
Merge branch 'master' of github.com:FacialTurd/PowderToypp
Diffstat (limited to 'src/tasks/Task.cpp')
| -rw-r--r-- | src/tasks/Task.cpp | 64 |
1 files changed, 50 insertions, 14 deletions
diff --git a/src/tasks/Task.cpp b/src/tasks/Task.cpp index c20a0ea..ec0ae99 100644 --- a/src/tasks/Task.cpp +++ b/src/tasks/Task.cpp @@ -17,6 +17,8 @@ void Task::SetTaskListener(TaskListener * listener) void Task::Start() { + pthread_mutex_init (&taskMutex, NULL); + pthread_cond_init(&taskCond, NULL); pthread_create(&doWorkThread, 0, &Task::doWork_helper, this); } @@ -35,6 +37,42 @@ bool Task::GetDone() return done; } +void Task::Poll() +{ + int newProgress; + bool newDone; + std::string newStatus; + pthread_mutex_lock(&taskMutex); + newProgress = thProgress; + newDone = thDone; + newStatus = std::string(thStatus); + pthread_cond_signal(&taskCond); + pthread_mutex_unlock(&taskMutex); + + if(newProgress!=progress) { + progress = newProgress; + if(listener) + listener->NotifyProgress(this); + } + if(newStatus!=status) { + status = newStatus; + if(listener) + listener->NotifyStatus(this); + } + if(newDone!=done) + { + done = newDone; + if(listener) + listener->NotifyDone(this); + } + + if(done) + { + pthread_join(doWorkThread, NULL); + pthread_mutex_destroy(&taskMutex); + } +} + Task::~Task() { @@ -59,26 +97,24 @@ void * Task::doWork_helper(void * ref) void Task::notifyProgress(int progress) { - if(this->progress!=progress) { - this->progress = progress; - if(listener) - listener->NotifyProgress(this); - } + pthread_mutex_lock(&taskMutex); + pthread_cond_wait(&taskCond, &taskMutex); + thProgress = progress; + pthread_mutex_unlock(&taskMutex); } void Task::notifyStatus(std::string status) { - if(this->status!=status) { - this->status = status; - if(listener) - listener->NotifyStatus(this); - } + pthread_mutex_lock(&taskMutex); + pthread_cond_wait(&taskCond, &taskMutex); + thStatus = status; + pthread_mutex_unlock(&taskMutex); } void Task::notifyDone() { - if(listener) - { - done = true; listener->NotifyDone(this); - } + pthread_mutex_lock(&taskMutex); + pthread_cond_wait(&taskCond, &taskMutex); + thDone = true; + pthread_mutex_unlock(&taskMutex); } |
