From 3cd12e3671e5ee108f039cb4350bf885164a8cf5 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 22 Jun 2018 12:11:22 +0200 Subject: Add ability to terminate running tasks --- gui/ice40/mainwindow.cc | 14 ++++++++++---- gui/ice40/worker.cc | 42 ++++++++++++++++++++++++++++++++++++------ gui/ice40/worker.h | 13 ++++++++++++- 3 files changed, 58 insertions(+), 11 deletions(-) (limited to 'gui/ice40') diff --git a/gui/ice40/mainwindow.cc b/gui/ice40/mainwindow.cc index 934798bb..e36464a0 100644 --- a/gui/ice40/mainwindow.cc +++ b/gui/ice40/mainwindow.cc @@ -17,18 +17,24 @@ MainWindow::MainWindow(Context *_ctx, QWidget *parent) std::string title = "nextpnr-ice40 - " + ctx->getChipName(); setWindowTitle(title.c_str()); - createMenu(); - task = new TaskManager(_ctx); connect(task, SIGNAL(log(std::string)), this, SLOT(writeInfo(std::string))); + + createMenu(); } -MainWindow::~MainWindow() {} +MainWindow::~MainWindow() { delete task; } void MainWindow::createMenu() { QMenu *menu_Custom = new QMenu("&ICE 40", menuBar); menuBar->addAction(menu_Custom->menuAction()); + + QAction *actionTerminate = new QAction("Terminate", this); + actionTerminate->setStatusTip("Terminate running task"); + connect(actionTerminate, SIGNAL(triggered()), task, + SLOT(terminate_thread())); + menu_Custom->addAction(actionTerminate); } void MainWindow::open() @@ -39,7 +45,7 @@ void MainWindow::open() tabWidget->setCurrentWidget(info); std::string fn = fileName.toStdString(); - task->parsejson(fn); + Q_EMIT task->parsejson(fn); } } bool MainWindow::save() { return false; } \ No newline at end of file diff --git a/gui/ice40/worker.cc b/gui/ice40/worker.cc index 5702137b..a309f868 100644 --- a/gui/ice40/worker.cc +++ b/gui/ice40/worker.cc @@ -10,9 +10,19 @@ #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(); + } + }; } void Worker::parsejson(const std::string &filename) @@ -32,14 +42,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) { - 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); @@ -53,4 +65,22 @@ TaskManager::~TaskManager() 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; +} \ No newline at end of file diff --git a/gui/ice40/worker.h b/gui/ice40/worker.h index 12d740dd..a2d220bd 100644 --- a/gui/ice40/worker.h +++ b/gui/ice40/worker.h @@ -1,17 +1,20 @@ #ifndef WORKER_H #define WORKER_H +#include #include #include "nextpnr.h" // FIXME USING_NEXTPNR_NAMESPACE +class TaskManager; + class Worker : public QObject { Q_OBJECT public: - Worker(Context *ctx); + Worker(Context *ctx, TaskManager *parent); public Q_SLOTS: void parsejson(const std::string &filename); Q_SIGNALS: @@ -29,11 +32,19 @@ class TaskManager : public QObject public: TaskManager(Context *ctx); ~TaskManager(); + bool shouldTerminate(); + void clearTerminate(); public Q_SLOTS: void info(const std::string &text); + void terminate_thread(); Q_SIGNALS: + void terminate(); void parsejson(const std::string &); void log(const std::string &text); + + private: + QMutex mutex; + bool toTerminate; }; #endif // WORKER_H -- cgit v1.2.3 From 5cb893aebdfdf711755b9bf610b36b8ff2d942ff Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 22 Jun 2018 12:24:50 +0200 Subject: terminate on close --- gui/ice40/worker.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'gui/ice40') diff --git a/gui/ice40/worker.cc b/gui/ice40/worker.cc index a309f868..4b101e7e 100644 --- a/gui/ice40/worker.cc +++ b/gui/ice40/worker.cc @@ -61,6 +61,8 @@ TaskManager::TaskManager(Context *ctx) : toTerminate(false) TaskManager::~TaskManager() { + if (workerThread.isRunning()) + terminate_thread(); workerThread.quit(); workerThread.wait(); } -- cgit v1.2.3 From 11d99853ab4514b1f6b87c5beb87c91f50e702a6 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 22 Jun 2018 12:49:20 +0200 Subject: more task control --- gui/ice40/mainwindow.cc | 35 ++++++++++++++++++++++++++++++---- gui/ice40/nextpnr.qrc | 7 +++++++ gui/ice40/resources/control_pause.png | Bin 0 -> 598 bytes gui/ice40/resources/control_play.png | Bin 0 -> 592 bytes gui/ice40/resources/control_stop.png | Bin 0 -> 403 bytes gui/ice40/worker.cc | 22 ++++++++++++++++++++- gui/ice40/worker.h | 4 ++++ 7 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 gui/ice40/nextpnr.qrc create mode 100644 gui/ice40/resources/control_pause.png create mode 100644 gui/ice40/resources/control_play.png create mode 100644 gui/ice40/resources/control_stop.png (limited to 'gui/ice40') diff --git a/gui/ice40/mainwindow.cc b/gui/ice40/mainwindow.cc index e36464a0..9ce7f41e 100644 --- a/gui/ice40/mainwindow.cc +++ b/gui/ice40/mainwindow.cc @@ -14,6 +14,8 @@ MainWindow::MainWindow(Context *_ctx, QWidget *parent) : BaseMainWindow(_ctx, parent) { + Q_INIT_RESOURCE(nextpnr); + std::string title = "nextpnr-ice40 - " + ctx->getChipName(); setWindowTitle(title.c_str()); @@ -30,11 +32,36 @@ void MainWindow::createMenu() QMenu *menu_Custom = new QMenu("&ICE 40", menuBar); menuBar->addAction(menu_Custom->menuAction()); - QAction *actionTerminate = new QAction("Terminate", this); - actionTerminate->setStatusTip("Terminate running task"); - connect(actionTerminate, SIGNAL(triggered()), task, + QAction *actionPlay = new QAction("Play", this); + QIcon icon1; + icon1.addFile(QStringLiteral(":/icons/resources/control_play.png")); + actionPlay->setIcon(icon1); + actionPlay->setStatusTip("Continue running task"); + connect(actionPlay, SIGNAL(triggered()), task, + SLOT(continue_thread())); + + QAction *actionPause = new QAction("Pause", this); + QIcon icon2; + icon2.addFile(QStringLiteral(":/icons/resources/control_pause.png")); + actionPause->setIcon(icon2); + actionPause->setStatusTip("Pause running task"); + connect(actionPause, SIGNAL(triggered()), task, + SLOT(pause_thread())); + + QAction *actionStop = new QAction("Stop", this); + QIcon icon3; + icon3.addFile(QStringLiteral(":/icons/resources/control_stop.png")); + actionStop->setIcon(icon3); + actionStop->setStatusTip("Stop running task"); + connect(actionStop, SIGNAL(triggered()), task, SLOT(terminate_thread())); - menu_Custom->addAction(actionTerminate); + + QToolBar *taskToolBar = new QToolBar(); + addToolBar(Qt::TopToolBarArea, taskToolBar); + + taskToolBar->addAction(actionPlay); + taskToolBar->addAction(actionPause); + taskToolBar->addAction(actionStop); } void MainWindow::open() diff --git a/gui/ice40/nextpnr.qrc b/gui/ice40/nextpnr.qrc new file mode 100644 index 00000000..cbdb8b26 --- /dev/null +++ b/gui/ice40/nextpnr.qrc @@ -0,0 +1,7 @@ + + + resources/control_play.png + resources/control_pause.png + resources/control_stop.png + + diff --git a/gui/ice40/resources/control_pause.png b/gui/ice40/resources/control_pause.png new file mode 100644 index 00000000..2d9ce9c4 Binary files /dev/null and b/gui/ice40/resources/control_pause.png differ diff --git a/gui/ice40/resources/control_play.png b/gui/ice40/resources/control_play.png new file mode 100644 index 00000000..0846555d Binary files /dev/null and b/gui/ice40/resources/control_play.png differ diff --git a/gui/ice40/resources/control_stop.png b/gui/ice40/resources/control_stop.png new file mode 100644 index 00000000..893bb60e Binary files /dev/null and b/gui/ice40/resources/control_stop.png differ diff --git a/gui/ice40/worker.cc b/gui/ice40/worker.cc index 4b101e7e..3854c67f 100644 --- a/gui/ice40/worker.cc +++ b/gui/ice40/worker.cc @@ -22,6 +22,9 @@ Worker::Worker(Context *_ctx, TaskManager *parent) : ctx(_ctx) parent->clearTerminate(); throw WorkerInterruptionRequested(); } + while (parent->isPaused()){ + QThread::sleep(1); + } }; } @@ -49,7 +52,7 @@ void Worker::parsejson(const std::string &filename) } } -TaskManager::TaskManager(Context *ctx) : toTerminate(false) +TaskManager::TaskManager(Context *ctx) : toTerminate(false), toPause(false) { Worker *worker = new Worker(ctx, this); worker->moveToThread(&workerThread); @@ -85,4 +88,21 @@ 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 diff --git a/gui/ice40/worker.h b/gui/ice40/worker.h index a2d220bd..49d1df4d 100644 --- a/gui/ice40/worker.h +++ b/gui/ice40/worker.h @@ -34,9 +34,12 @@ class TaskManager : public QObject ~TaskManager(); bool shouldTerminate(); void clearTerminate(); + bool isPaused(); public Q_SLOTS: void info(const std::string &text); void terminate_thread(); + void pause_thread(); + void continue_thread(); Q_SIGNALS: void terminate(); void parsejson(const std::string &); @@ -45,6 +48,7 @@ class TaskManager : public QObject private: QMutex mutex; bool toTerminate; + bool toPause; }; #endif // WORKER_H -- cgit v1.2.3