summaryrefslogtreecommitdiff
path: root/src/tasks/Task.cpp
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-11-17 19:44:09 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-11-17 19:44:09 (GMT)
commit058a2edd75debbd0297f92572316daa704bd379f (patch)
treead303f091f9a08b209b91eb34a9fcad996a3de69 /src/tasks/Task.cpp
parente3594aba9e05c6865d396418c028049cda92c2f3 (diff)
parent7a21ae192fe19868539956f3fe28e62b2c7c4429 (diff)
downloadpowder-058a2edd75debbd0297f92572316daa704bd379f.zip
powder-058a2edd75debbd0297f92572316daa704bd379f.tar.gz
Merge branch 'master' of github.com:FacialTurd/PowderToypp
Diffstat (limited to 'src/tasks/Task.cpp')
-rw-r--r--src/tasks/Task.cpp185
1 files changed, 185 insertions, 0 deletions
diff --git a/src/tasks/Task.cpp b/src/tasks/Task.cpp
new file mode 100644
index 0000000..d29fe04
--- /dev/null
+++ b/src/tasks/Task.cpp
@@ -0,0 +1,185 @@
+/*
+ * Task.cpp
+ *
+ * Created on: Apr 6, 2012
+ * Author: Simon
+ */
+
+#include "Task.h"
+#include "TaskListener.h"
+
+void Task::AddTaskListener(TaskListener * listener)
+{
+ this->listener = listener;
+ notifyProgressMain();
+ notifyStatusMain();
+}
+
+void Task::Start()
+{
+ thDone = false;
+ done = false;
+ progress = 0;
+ status = "";
+ //taskMutex = PTHREAD_MUTEX_INITIALIZER;
+ before();
+ pthread_mutex_init (&taskMutex, NULL);
+ pthread_create(&doWorkThread, 0, &Task::doWork_helper, this);
+}
+
+int Task::GetProgress()
+{
+ return progress;
+}
+
+std::string Task::GetStatus()
+{
+ return status;
+}
+
+std::string Task::GetError()
+{
+ return error;
+}
+
+bool Task::GetDone()
+{
+ return done;
+}
+
+bool Task::GetSuccess()
+{
+ return success;
+}
+
+void Task::Poll()
+{
+ if(!done)
+ {
+ int newProgress;
+ bool newDone = false;
+ bool newSuccess = false;
+ std::string newStatus;
+ std::string newError;
+ pthread_mutex_lock(&taskMutex);
+ newProgress = thProgress;
+ newDone = thDone;
+ newSuccess = thSuccess;
+ newStatus = std::string(thStatus);
+ newError = std::string(thError);
+ pthread_mutex_unlock(&taskMutex);
+
+ success = newSuccess;
+
+ if(newProgress!=progress) {
+ progress = newProgress;
+ notifyProgressMain();
+ }
+
+ if(newError!=error) {
+ error = std::string(newError);
+ notifyErrorMain();
+ }
+
+ if(newStatus!=status) {
+ status = std::string(newStatus);
+ notifyStatusMain();
+ }
+
+ if(newDone!=done)
+ {
+ done = newDone;
+
+ pthread_join(doWorkThread, NULL);
+ pthread_mutex_destroy(&taskMutex);
+
+ after();
+
+ notifyDoneMain();
+ }
+ }
+}
+
+Task::~Task()
+{
+ if(!done)
+ {
+ pthread_join(doWorkThread, NULL);
+ pthread_mutex_destroy(&taskMutex);
+ }
+}
+
+void Task::before()
+{
+
+}
+
+bool Task::doWork()
+{
+ notifyStatus("Fake progress");
+ for(int i = 0; i < 100; i++)
+ {
+ notifyProgress(i);
+ }
+ return true;
+}
+
+void Task::after()
+{
+
+}
+
+void * Task::doWork_helper(void * ref)
+{
+ bool newSuccess = ((Task*)ref)->doWork();
+ pthread_mutex_lock(&((Task*)ref)->taskMutex);
+ ((Task*)ref)->thSuccess = newSuccess;
+ ((Task*)ref)->thDone = true;
+ pthread_mutex_unlock(&((Task*)ref)->taskMutex);
+ return NULL;
+}
+
+void Task::notifyProgress(int progress)
+{
+ pthread_mutex_lock(&taskMutex);
+ thProgress = progress;
+ pthread_mutex_unlock(&taskMutex);
+}
+
+void Task::notifyStatus(std::string status)
+{
+ pthread_mutex_lock(&taskMutex);
+ thStatus = std::string(status);
+ pthread_mutex_unlock(&taskMutex);
+}
+
+void Task::notifyError(std::string error)
+{
+ pthread_mutex_lock(&taskMutex);
+ thError = std::string(error);
+ pthread_mutex_unlock(&taskMutex);
+}
+
+void Task::notifyProgressMain()
+{
+ if(listener)
+ listener->NotifyProgress(this);
+}
+
+void Task::notifyStatusMain()
+{
+ if(listener)
+ listener->NotifyStatus(this);
+}
+
+void Task::notifyDoneMain()
+{
+ if(listener)
+ listener->NotifyDone(this);
+}
+
+void Task::notifyErrorMain()
+{
+ if(listener)
+ listener->NotifyError(this);
+}