The std::async calls need a lock on them.

master
Zed A. Shaw 3 months ago
parent 4b2ed2951e
commit ea89e083dd
  1. 7
      builder.cpp
  2. 3
      builder.hpp
  3. 2
      game_engine.cpp

@ -14,6 +14,7 @@
#include <nlohmann/json.hpp>
#include <fstream>
#include <future>
#include <mutex>
using std::string;
using namespace fmt;
@ -32,13 +33,15 @@ Builder::Builder(GUI &g, GameEngine &engine)
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");
dbc::check(build_out != nullptr, "Failed to run command.");
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 *res = fgets(buffer, BUF_MAX, build_out);
done_out = res == nullptr;

@ -42,6 +42,7 @@ class Builder : DeadSimpleFSM<BuildState, BuildEvent> {
string line = "";
std::future<FILE *> build_fut;
std::future<string> read_fut;
std::mutex fsm_mutex;
git_repository* repo = nullptr;
public:
@ -49,6 +50,8 @@ class Builder : DeadSimpleFSM<BuildState, BuildEvent> {
Builder(GUI &g, GameEngine &engine);
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 {
try {

@ -64,7 +64,7 @@ void GameEngine::idle(GameEvent ev) {
void GameEngine::in_round(GameEvent ev, string &hit_type) {
switch(ev) {
case GameEvent::HIT:
hit(hit_type); // FIXME: bring back error type
hit(hit_type);
if(is_dead()) {
state(GameState::DEAD);
} else {

Loading…
Cancel
Save