summaryrefslogtreecommitdiff
path: root/platform/qt/src/run_loop.cpp
blob: 1c199be9de76df100b3964efbc58f1c95b80f04e (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "run_loop_impl.hpp"

#include <mbgl/util/thread_local.hpp>

#include <QCoreApplication>

#include <cassert>
#include <functional>
#include <utility>

namespace {

using namespace mbgl::util;
static ThreadLocal<RunLoop>& current = *new ThreadLocal<RunLoop>;

}

namespace mbgl {
namespace util {

void RunLoop::Impl::onReadEvent(int fd) {
    readPoll[fd].second(fd, Event::Read);
}

void RunLoop::Impl::onWriteEvent(int fd) {
    writePoll[fd].second(fd, Event::Write);
}

RunLoop* RunLoop::Get() {
    return current.get();
}

RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) {
    // XXX: We should probably throw an runtime exception
    // here instead of creating a QCoreApplication which is
    // way too intrusive. This is a hack mostly for the unit
    // tests to work, as you always need a QCoreApplication
    // prior to run a Qt app.
    if (!QCoreApplication::instance()) {
        static const char* argv[] = { "mbgl" };
        static int argc = 1;

        // We need to keep this around because it would otherwise crash
        // on Qt4 due to a bug on QNetworkConfigurationManager when recreating
        // a QCoreApplication: https://bugreports.qt.io/browse/QTBUG-36897
        static auto* app = new QCoreApplication(argc, const_cast<char**>(argv));
        Q_UNUSED(app);
    }

    switch (type) {
    case Type::New:
        impl->loop = std::make_unique<QEventLoop>();
        break;
    case Type::Default:
        // Use QCoreApplication::instance().
        break;
    }

    impl->type = type;

    current.set(this);
    impl->async = std::make_unique<AsyncTask>(std::bind(&RunLoop::process, this));
}

RunLoop::~RunLoop() {
    MBGL_VERIFY_THREAD(tid);

    current.set(nullptr);
}

LOOP_HANDLE RunLoop::getLoopHandle() {
    throw std::runtime_error("Should not be used in Qt.");

    return nullptr;
}

void RunLoop::push(std::shared_ptr<WorkTask> task) {
    withMutex([&] { queue.push(task); });
    impl->async->send();
}

void RunLoop::run() {
    MBGL_VERIFY_THREAD(tid);

    if (impl->type == Type::Default) {
        QCoreApplication::instance()->exec();
    } else {
        impl->loop->exec();
    }
}

void RunLoop::stop() {
    invoke([&] {
        if (impl->type == Type::Default) {
            QCoreApplication::instance()->exit();
        } else {
            impl->loop->exit();
        }
    });
}

void RunLoop::runOnce() {
    MBGL_VERIFY_THREAD(tid);

    if (impl->type == Type::Default) {
        QCoreApplication::instance()->processEvents();
    } else {
        impl->loop->processEvents();
    }
}

void RunLoop::addWatch(int fd, Event event, std::function<void(int, Event)>&& cb) {
    MBGL_VERIFY_THREAD(tid);

    if (event == Event::Read || event == Event::ReadWrite) {
        auto notifier = std::make_unique<QSocketNotifier>(fd, QSocketNotifier::Read);
        QObject::connect(notifier.get(), SIGNAL(activated(int)), impl.get(), SLOT(onReadEvent(int)));
        impl->readPoll[fd] = WatchPair(std::move(notifier), std::move(cb));
    }

    if (event == Event::Write || event == Event::ReadWrite) {
        auto notifier = std::make_unique<QSocketNotifier>(fd, QSocketNotifier::Write);
        QObject::connect(notifier.get(), SIGNAL(activated(int)), impl.get(), SLOT(onWriteEvent(int)));
        impl->writePoll[fd] = WatchPair(std::move(notifier), std::move(cb));
    }
}

void RunLoop::removeWatch(int fd) {
    MBGL_VERIFY_THREAD(tid);

    auto writePollIter = impl->writePoll.find(fd);
    if (writePollIter != impl->writePoll.end()) {
        impl->writePoll.erase(writePollIter);
    }

    auto readPollIter = impl->readPoll.find(fd);
    if (readPollIter != impl->readPoll.end()) {
        impl->readPoll.erase(readPollIter);
    }
}

}
}