aboutsummaryrefslogtreecommitdiffstats
path: root/gui/ice40/worker.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gui/ice40/worker.cc')
-rw-r--r--gui/ice40/worker.cc64
1 files changed, 58 insertions, 6 deletions
diff --git a/gui/ice40/worker.cc b/gui/ice40/worker.cc
index 5702137b..3854c67f 100644
--- a/gui/ice40/worker.cc
+++ b/gui/ice40/worker.cc
@@ -10,9 +10,22 @@
#include "route.h"
#include "timing.h"
-Worker::Worker(Context *_ctx) : ctx(_ctx)
+struct WorkerInterruptionRequested
{
- log_write_function = [this](std::string text) { Q_EMIT log(text); };
+};
+
+Worker::Worker(Context *_ctx, TaskManager *parent) : ctx(_ctx)
+{
+ log_write_function = [this, parent](std::string text) {
+ Q_EMIT log(text);
+ if (parent->shouldTerminate()) {
+ parent->clearTerminate();
+ throw WorkerInterruptionRequested();
+ }
+ while (parent->isPaused()){
+ QThread::sleep(1);
+ }
+ };
}
void Worker::parsejson(const std::string &filename)
@@ -32,14 +45,16 @@ void Worker::parsejson(const std::string &filename)
log_error("Placing design failed.\n");
if (!route_design(ctx))
log_error("Routing design failed.\n");
- Q_EMIT log("done");
+ Q_EMIT log("DONE\n");
} catch (log_execution_error_exception) {
+ } catch (WorkerInterruptionRequested) {
+ Q_EMIT log("CANCELED\n");
}
}
-TaskManager::TaskManager(Context *ctx)
+TaskManager::TaskManager(Context *ctx) : toTerminate(false), toPause(false)
{
- Worker *worker = new Worker(ctx);
+ Worker *worker = new Worker(ctx, this);
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &TaskManager::parsejson, worker, &Worker::parsejson);
@@ -49,8 +64,45 @@ TaskManager::TaskManager(Context *ctx)
TaskManager::~TaskManager()
{
+ if (workerThread.isRunning())
+ terminate_thread();
workerThread.quit();
workerThread.wait();
}
-void TaskManager::info(const std::string &result) { Q_EMIT log(result); } \ No newline at end of file
+void TaskManager::info(const std::string &result) { Q_EMIT log(result); }
+
+void TaskManager::terminate_thread()
+{
+ QMutexLocker locker(&mutex);
+ toTerminate = true;
+}
+
+bool TaskManager::shouldTerminate()
+{
+ QMutexLocker locker(&mutex);
+ return toTerminate;
+}
+
+void TaskManager::clearTerminate()
+{
+ QMutexLocker locker(&mutex);
+ toTerminate = false;
+}
+
+void TaskManager::pause_thread()
+{
+ QMutexLocker locker(&mutex);
+ toPause = true;
+}
+
+void TaskManager::continue_thread()
+{
+ QMutexLocker locker(&mutex);
+ toPause = false;
+}
+bool TaskManager::isPaused()
+{
+ QMutexLocker locker(&mutex);
+ return toPause;
+} \ No newline at end of file