Add ability to terminate running tasks
This commit is contained in:
parent
71176ac538
commit
3cd12e3671
@ -17,18 +17,24 @@ MainWindow::MainWindow(Context *_ctx, QWidget *parent)
|
|||||||
std::string title = "nextpnr-ice40 - " + ctx->getChipName();
|
std::string title = "nextpnr-ice40 - " + ctx->getChipName();
|
||||||
setWindowTitle(title.c_str());
|
setWindowTitle(title.c_str());
|
||||||
|
|
||||||
createMenu();
|
|
||||||
|
|
||||||
task = new TaskManager(_ctx);
|
task = new TaskManager(_ctx);
|
||||||
connect(task, SIGNAL(log(std::string)), this, SLOT(writeInfo(std::string)));
|
connect(task, SIGNAL(log(std::string)), this, SLOT(writeInfo(std::string)));
|
||||||
|
|
||||||
|
createMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow() {}
|
MainWindow::~MainWindow() { delete task; }
|
||||||
|
|
||||||
void MainWindow::createMenu()
|
void MainWindow::createMenu()
|
||||||
{
|
{
|
||||||
QMenu *menu_Custom = new QMenu("&ICE 40", menuBar);
|
QMenu *menu_Custom = new QMenu("&ICE 40", menuBar);
|
||||||
menuBar->addAction(menu_Custom->menuAction());
|
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()
|
void MainWindow::open()
|
||||||
@ -39,7 +45,7 @@ void MainWindow::open()
|
|||||||
tabWidget->setCurrentWidget(info);
|
tabWidget->setCurrentWidget(info);
|
||||||
|
|
||||||
std::string fn = fileName.toStdString();
|
std::string fn = fileName.toStdString();
|
||||||
task->parsejson(fn);
|
Q_EMIT task->parsejson(fn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool MainWindow::save() { return false; }
|
bool MainWindow::save() { return false; }
|
@ -10,9 +10,19 @@
|
|||||||
#include "route.h"
|
#include "route.h"
|
||||||
#include "timing.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)
|
void Worker::parsejson(const std::string &filename)
|
||||||
@ -32,14 +42,16 @@ void Worker::parsejson(const std::string &filename)
|
|||||||
log_error("Placing design failed.\n");
|
log_error("Placing design failed.\n");
|
||||||
if (!route_design(ctx))
|
if (!route_design(ctx))
|
||||||
log_error("Routing design failed.\n");
|
log_error("Routing design failed.\n");
|
||||||
Q_EMIT log("done");
|
Q_EMIT log("DONE\n");
|
||||||
} catch (log_execution_error_exception) {
|
} 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);
|
worker->moveToThread(&workerThread);
|
||||||
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
|
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
|
||||||
connect(this, &TaskManager::parsejson, worker, &Worker::parsejson);
|
connect(this, &TaskManager::parsejson, worker, &Worker::parsejson);
|
||||||
@ -54,3 +66,21 @@ TaskManager::~TaskManager()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TaskManager::info(const std::string &result) { Q_EMIT log(result); }
|
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;
|
||||||
|
}
|
@ -1,17 +1,20 @@
|
|||||||
#ifndef WORKER_H
|
#ifndef WORKER_H
|
||||||
#define WORKER_H
|
#define WORKER_H
|
||||||
|
|
||||||
|
#include <QMutex>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include "nextpnr.h"
|
#include "nextpnr.h"
|
||||||
|
|
||||||
// FIXME
|
// FIXME
|
||||||
USING_NEXTPNR_NAMESPACE
|
USING_NEXTPNR_NAMESPACE
|
||||||
|
|
||||||
|
class TaskManager;
|
||||||
|
|
||||||
class Worker : public QObject
|
class Worker : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
Worker(Context *ctx);
|
Worker(Context *ctx, TaskManager *parent);
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
void parsejson(const std::string &filename);
|
void parsejson(const std::string &filename);
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
@ -29,11 +32,19 @@ class TaskManager : public QObject
|
|||||||
public:
|
public:
|
||||||
TaskManager(Context *ctx);
|
TaskManager(Context *ctx);
|
||||||
~TaskManager();
|
~TaskManager();
|
||||||
|
bool shouldTerminate();
|
||||||
|
void clearTerminate();
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
void info(const std::string &text);
|
void info(const std::string &text);
|
||||||
|
void terminate_thread();
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
|
void terminate();
|
||||||
void parsejson(const std::string &);
|
void parsejson(const std::string &);
|
||||||
void log(const std::string &text);
|
void log(const std::string &text);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QMutex mutex;
|
||||||
|
bool toTerminate;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // WORKER_H
|
#endif // WORKER_H
|
||||||
|
Loading…
Reference in New Issue
Block a user