aboutsummaryrefslogtreecommitdiffstats
path: root/gui/ice40/worker.cc
blob: 4b101e7e80c69e1b9648735eca1ac902c0a67f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "worker.h"
#include <fstream>
#include "bitstream.h"
#include "design_utils.h"
#include "jsonparse.h"
#include "log.h"
#include "pack.h"
#include "pcf.h"
#include "place_sa.h"
#include "route.h"
#include "timing.h"

struct WorkerInterruptionRequested
{
};

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)
{
    std::string fn = filename;
    std::ifstream f(fn);
    try {
        if (!parse_json_file(f, fn, ctx))
            log_error("Loading design failed.\n");
        if (!pack_design(ctx))
            log_error("Packing design failed.\n");
        double freq = 50e6;
        assign_budget(ctx, freq);
        print_utilisation(ctx);

        if (!place_design_sa(ctx))
            log_error("Placing design failed.\n");
        if (!route_design(ctx))
            log_error("Routing design failed.\n");
        Q_EMIT log("DONE\n");
    } catch (log_execution_error_exception) {
    } catch (WorkerInterruptionRequested) {
        Q_EMIT log("CANCELED\n");
    }
}

TaskManager::TaskManager(Context *ctx) : toTerminate(false)
{
    Worker *worker = new Worker(ctx, this);
    worker->moveToThread(&workerThread);
    connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
    connect(this, &TaskManager::parsejson, worker, &Worker::parsejson);
    connect(worker, &Worker::log, this, &TaskManager::info);
    workerThread.start();
}

TaskManager::~TaskManager()
{
    if (workerThread.isRunning()) 
        terminate_thread();
    workerThread.quit();
    workerThread.wait();
}

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;
}