Now using a std::async and future to do an async popen but I also need the FILE read in read_line to be async, so now I'm at a point where I have to refactor into a better statemachine.
This commit is contained in:
parent
501cb5fe25
commit
356314406f
2 changed files with 58 additions and 50 deletions
39
builder.cpp
39
builder.cpp
|
@ -19,10 +19,12 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <future>
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using namespace fmt;
|
using namespace fmt;
|
||||||
using namespace nlohmann;
|
using namespace nlohmann;
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
#define BUF_MAX 1024
|
#define BUF_MAX 1024
|
||||||
|
|
||||||
|
@ -37,6 +39,7 @@ Builder::Builder(GUI &g, GameEngine &engine)
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *start_command(string &build_cmd) {
|
FILE *start_command(string &build_cmd) {
|
||||||
|
println(">>>>>>>>> start_command {}", build_cmd);
|
||||||
FILE *build_out = popen(build_cmd.c_str(), "r");
|
FILE *build_out = popen(build_cmd.c_str(), "r");
|
||||||
dbc::check(build_out != nullptr, "Failed to run command.");
|
dbc::check(build_out != nullptr, "Failed to run command.");
|
||||||
return build_out;
|
return build_out;
|
||||||
|
@ -80,9 +83,10 @@ MatchResult Builder::parse_line(const string &line) {
|
||||||
}
|
}
|
||||||
|
|
||||||
enum BuildState {
|
enum BuildState {
|
||||||
WAITING, BUILDING, DONE
|
WAITING, BUILDING, DONE, STARTING, READING
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void Builder::run() {
|
void Builder::run() {
|
||||||
git_repository* repo = nullptr;
|
git_repository* repo = nullptr;
|
||||||
|
|
||||||
|
@ -104,7 +108,9 @@ void Builder::run() {
|
||||||
|
|
||||||
FILE *build_out = NULL;
|
FILE *build_out = NULL;
|
||||||
bool build_done = false;
|
bool build_done = false;
|
||||||
|
string line = "";
|
||||||
BuildState state = WAITING;
|
BuildState state = WAITING;
|
||||||
|
std::future<FILE *> build_fut;
|
||||||
|
|
||||||
fileWatcher->watch();
|
fileWatcher->watch();
|
||||||
|
|
||||||
|
@ -115,15 +121,34 @@ void Builder::run() {
|
||||||
game.start_round();
|
game.start_round();
|
||||||
gui.building();
|
gui.building();
|
||||||
gui.output(format("CHANGES! Running build {}", build_cmd));
|
gui.output(format("CHANGES! Running build {}", build_cmd));
|
||||||
build_out = start_command(build_cmd);
|
build_fut = std::async([&]() {
|
||||||
state = BUILDING;
|
return start_command(build_cmd);
|
||||||
|
});
|
||||||
|
state = STARTING;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BUILDING: {
|
case STARTING: {
|
||||||
// check if there's output
|
println(">>> STARTING");
|
||||||
string line = read_line(build_out, build_done);
|
std::future_status status = build_fut.wait_for(0ms);
|
||||||
|
|
||||||
|
if(status == std::future_status::ready) {
|
||||||
|
build_out = build_fut.get();
|
||||||
|
state = READING;
|
||||||
|
} else {
|
||||||
|
state = STARTING;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case READING: {
|
||||||
|
line = read_line(build_out, build_done);
|
||||||
|
state = BUILDING;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case BUILDING: {
|
||||||
|
println(">>> BUILDING");
|
||||||
|
// check if there's output
|
||||||
if(build_done) {
|
if(build_done) {
|
||||||
bool good = end_build(build_out, gui, build_cmd);
|
bool good = end_build(build_out, gui, build_cmd);
|
||||||
|
|
||||||
|
@ -143,7 +168,7 @@ void Builder::run() {
|
||||||
gui.output(format("HIT WITH {} @ {}:{}:{} {}", m.type, m.file_name, m.lnumber, m.col, m.message));
|
gui.output(format("HIT WITH {} @ {}:{}:{} {}", m.type, m.file_name, m.lnumber, m.col, m.message));
|
||||||
game.hit(m.type);
|
game.hit(m.type);
|
||||||
}
|
}
|
||||||
state = BUILDING;
|
state = READING;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,54 +1,37 @@
|
||||||
#include <condition_variable>
|
#include <chrono>
|
||||||
|
#include <future>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
|
||||||
#include <string>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
using namespace std::chrono_literals;
|
||||||
std::mutex m;
|
|
||||||
std::condition_variable cv;
|
|
||||||
std::string data;
|
|
||||||
bool ready = false;
|
|
||||||
bool processed = false;
|
|
||||||
|
|
||||||
void worker_thread()
|
|
||||||
{
|
|
||||||
// wait until main() sends data
|
|
||||||
std::unique_lock lk(m);
|
|
||||||
cv.wait(lk, []{ return ready; });
|
|
||||||
|
|
||||||
// after the wait, we own the lock
|
|
||||||
std::cout << "Worker thread is processing data\n";
|
|
||||||
data += " after processing";
|
|
||||||
|
|
||||||
// send data back to main()
|
|
||||||
processed = true;
|
|
||||||
std::cout << "Worker thread signals data processing completed\n";
|
|
||||||
|
|
||||||
// manual unlocking is done before notifying, to avoid waking up
|
|
||||||
// the waiting thread only to block again (see notify_one for details)
|
|
||||||
lk.unlock();
|
|
||||||
cv.notify_one();
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::thread worker(worker_thread);
|
std::future<int> future = std::async(std::launch::async, []()
|
||||||
|
|
||||||
data = "Example data";
|
|
||||||
// send data to the worker thread
|
|
||||||
{
|
{
|
||||||
std::lock_guard lk(m);
|
std::this_thread::sleep_for(3s);
|
||||||
ready = true;
|
return 8;
|
||||||
std::cout << "main() signals data ready for processing\n";
|
});
|
||||||
}
|
|
||||||
cv.notify_one();
|
|
||||||
|
|
||||||
// wait for the worker
|
std::cout << "waiting...\n";
|
||||||
|
std::future_status status;
|
||||||
|
|
||||||
|
do
|
||||||
{
|
{
|
||||||
std::unique_lock lk(m);
|
status = future.wait_for(100ms);
|
||||||
cv.wait(lk, []{ return processed; });
|
switch (status)
|
||||||
|
{
|
||||||
|
case std::future_status::deferred:
|
||||||
|
std::cout << "deferred\n";
|
||||||
|
break;
|
||||||
|
case std::future_status::timeout:
|
||||||
|
std::cout << "timeout\n";
|
||||||
|
break;
|
||||||
|
case std::future_status::ready:
|
||||||
|
std::cout << "ready!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
std::cout << "Back in main(), data = " << data << '\n';
|
while (status != std::future_status::ready);
|
||||||
|
|
||||||
worker.join();
|
std::cout << "result is " << future.get() << '\n';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue