diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-06-20 16:51:51 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-06-20 16:51:51 (GMT) |
| commit | 0594aa5d07d7bf63885eee966d102e3e7354c925 (patch) | |
| tree | 5700cb10ab0e63b6684eaeba225d578bf2f1dfa6 /src/tasks/Task.cpp | |
| parent | 088b2f678d5baedcf3555a5afce76710e6900a9e (diff) | |
| download | powder-0594aa5d07d7bf63885eee966d102e3e7354c925.zip powder-0594aa5d07d7bf63885eee966d102e3e7354c925.tar.gz | |
More in the way of an autoupdater
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); } |
