The std::async calls need a lock on them.
This commit is contained in:
parent
4b2ed2951e
commit
ea89e083dd
3 changed files with 9 additions and 3 deletions
|
@ -14,6 +14,7 @@
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <future>
|
#include <future>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using namespace fmt;
|
using namespace fmt;
|
||||||
|
@ -32,13 +33,15 @@ Builder::Builder(GUI &g, GameEngine &engine)
|
||||||
config["build_cmd"].template get_to<string>(build_cmd);
|
config["build_cmd"].template get_to<string>(build_cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *start_command(string &build_cmd) {
|
FILE *Builder::start_command(string &build_cmd) {
|
||||||
|
std::lock_guard<std::mutex> lock(fsm_mutex);
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
string read_line(FILE *build_out, bool &done_out) {
|
string Builder::read_line(FILE *build_out, bool &done_out) {
|
||||||
|
std::lock_guard<std::mutex> lock(fsm_mutex);
|
||||||
char buffer[BUF_MAX];
|
char buffer[BUF_MAX];
|
||||||
char *res = fgets(buffer, BUF_MAX, build_out);
|
char *res = fgets(buffer, BUF_MAX, build_out);
|
||||||
done_out = res == nullptr;
|
done_out = res == nullptr;
|
||||||
|
|
|
@ -42,6 +42,7 @@ class Builder : DeadSimpleFSM<BuildState, BuildEvent> {
|
||||||
string line = "";
|
string line = "";
|
||||||
std::future<FILE *> build_fut;
|
std::future<FILE *> build_fut;
|
||||||
std::future<string> read_fut;
|
std::future<string> read_fut;
|
||||||
|
std::mutex fsm_mutex;
|
||||||
git_repository* repo = nullptr;
|
git_repository* repo = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -49,6 +50,8 @@ class Builder : DeadSimpleFSM<BuildState, BuildEvent> {
|
||||||
Builder(GUI &g, GameEngine &engine);
|
Builder(GUI &g, GameEngine &engine);
|
||||||
|
|
||||||
MatchResult parse_line(const string &line);
|
MatchResult parse_line(const string &line);
|
||||||
|
string read_line(FILE *build_out, bool &done_out);
|
||||||
|
FILE *start_command(string &build_cmd);
|
||||||
|
|
||||||
void event(BuildEvent ev) override {
|
void event(BuildEvent ev) override {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -64,7 +64,7 @@ void GameEngine::idle(GameEvent ev) {
|
||||||
void GameEngine::in_round(GameEvent ev, string &hit_type) {
|
void GameEngine::in_round(GameEvent ev, string &hit_type) {
|
||||||
switch(ev) {
|
switch(ev) {
|
||||||
case GameEvent::HIT:
|
case GameEvent::HIT:
|
||||||
hit(hit_type); // FIXME: bring back error type
|
hit(hit_type);
|
||||||
if(is_dead()) {
|
if(is_dead()) {
|
||||||
state(GameState::DEAD);
|
state(GameState::DEAD);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue