Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GRPC Blocking Thread #92

Merged
merged 18 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
c635f7d
Restore GRPC Server Plugin
RobertBColton Mar 7, 2020
d4d34a9
GRPC Blocking Thread
RobertBColton Mar 7, 2020
6c44119
Merge branch 'master' of https://github.com/enigma-dev/RadialGM into …
RobertBColton Mar 7, 2020
53a7988
Add QtConcurrent to pro for now
RobertBColton Mar 7, 2020
bece01a
Future deadline helper is no longer needed
RobertBColton Mar 7, 2020
e0cfce9
comments are too long
RobertBColton Mar 7, 2020
2374a81
Don't need chrono include now either
RobertBColton Mar 7, 2020
acffbad
Use std::thread so we don't have to use another Qt module
RobertBColton Mar 7, 2020
48d4d71
Merge branch 'master' of https://github.com/enigma-dev/RadialGM into …
RobertBColton Mar 7, 2020
41c479e
Clean up comments a little
RobertBColton Mar 7, 2020
1a328b8
Merge branch 'master' of https://github.com/enigma-dev/RadialGM into …
RobertBColton Mar 8, 2020
c2ed8c2
QTimer include is not used now
RobertBColton Mar 8, 2020
03e0609
Merge branch 'master' of https://github.com/enigma-dev/RadialGM into …
RobertBColton Mar 8, 2020
c16b1e6
Merge branch 'master' of https://github.com/enigma-dev/RadialGM into …
RobertBColton Mar 9, 2020
f7a1923
Merge branch 'master' of https://github.com/enigma-dev/RadialGM into …
RobertBColton Mar 9, 2020
4d6db3c
Merge branch 'master' of https://github.com/enigma-dev/RadialGM into …
RobertBColton Mar 9, 2020
a76ece5
Merge branch 'master' of https://github.com/enigma-dev/RadialGM into …
RobertBColton Mar 15, 2020
31f50ed
Merge branch 'master' of https://github.com/enigma-dev/RadialGM into …
RobertBColton Mar 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions Plugins/ServerPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@
#include <QFileDialog>
#include <QList>
#include <QTemporaryFile>
#include <QTimer>

#include <chrono>
#include <thread>
#include <memory>

namespace {

inline std::chrono::system_clock::time_point future_deadline(size_t us) {
return (std::chrono::system_clock::now() + std::chrono::microseconds(us));
}

} // anonymous namespace

CallData::~CallData() {}

template <class T>
Expand Down Expand Up @@ -140,7 +131,20 @@ struct SyntaxCheckReader : public AsyncResponseReadWorker<SyntaxError> {
CompilerClient::~CompilerClient() {}

CompilerClient::CompilerClient(std::shared_ptr<Channel> channel, MainWindow& mainWindow)
: QObject(&mainWindow), stub(Compiler::NewStub(channel)), mainWindow(mainWindow) {}
: QObject(&mainWindow), stub(Compiler::NewStub(channel)), mainWindow(mainWindow) {
// start a blocking thread to poll for GRPC events
// and dispatch them back to the GUI thread
std::thread([this](){
while(true) {
void* got_tag = nullptr; bool ok = false;
// block for next GRPC event, break if shutdown
if (!this->cq.Next(&got_tag, &ok)) break;
// block until GUI thread handles GRPC event
QMetaObject::invokeMethod(this, "UpdateLoop", Qt::BlockingQueuedConnection,
Q_ARG(void*, got_tag), Q_ARG(bool, ok));
}
}).detach();
}

void CompilerClient::CompileBuffer(Game* game, CompileMode mode, std::string name) {
emit CompileStatusChanged();
Expand Down Expand Up @@ -221,18 +225,13 @@ T* CompilerClient::ScheduleTask() {
return callData;
}

void CompilerClient::UpdateLoop() {
void* got_tag = nullptr;
bool ok = false;

auto asyncStatus = cq.AsyncNext(&got_tag, &ok, future_deadline(0));
void CompilerClient::UpdateLoop(void* got_tag, bool ok) {
if (!got_tag) return;
auto callData = static_cast<CallData*>(got_tag);
if (callData && callData->state != AsyncState::DISCONNECTED && !ok) {
if (callData->state != AsyncState::DISCONNECTED && !ok) {
callData->finish();
return;
}
// yield to application main event loop
if (asyncStatus != CompletionQueue::NextStatus::GOT_EVENT || !got_tag) return;

(*callData)(callData->status);
if (callData->state == AsyncState::FINISH) {
Expand Down Expand Up @@ -281,13 +280,6 @@ ServerPlugin::ServerPlugin(MainWindow& mainWindow) : RGMPlugin(mainWindow) {
// main output dock widget (thread safe and don't block the main event loop!)
connect(compilerClient, &CompilerClient::LogOutput, this, &RGMPlugin::LogOutput);

// use a timer to process async grpc events at regular intervals
// without us needing any threading or blocking the main thread
QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, compilerClient, &CompilerClient::UpdateLoop);
// timer delay larger than one so we don't hog the CPU core
timer->start(1);

// update initial keyword set and systems
compilerClient->GetResources();
compilerClient->GetSystems();
Expand Down
2 changes: 1 addition & 1 deletion Plugins/ServerPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CompilerClient : public QObject {
void LogOutput(const QString& output);

public slots:
void UpdateLoop();
void UpdateLoop(void* got_tag = nullptr, bool ok = false);

private:
CompletionQueue cq;
Expand Down