First step in refactoring out the build running from the gui for later thread fiascos.

master
Zed A. Shaw 2 months ago
parent b0c9fefa9b
commit 0bac4dbfd9
  1. 134
      builder.cpp
  2. 3
      builder.hpp
  3. 1
      gui.cpp
  4. 3
      meson.build
  5. 64
      threadtest.cpp

@ -36,71 +36,60 @@ Builder::Builder(GUI &g, GameEngine &engine)
config["build_cmd"].template get_to<string>(build_cmd); config["build_cmd"].template get_to<string>(build_cmd);
} }
void Builder::run_build() { FILE *start_command(string &build_cmd) {
std::regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*");
char buffer[BUF_MAX]; // BUF_MAX is a define already?
std::ofstream stats_out;
stats_out.open("stats.csv", std::ios::out | std::ios::app);
std::time_t tstamp = std::time(nullptr);
dbc::check(stats_out.good(), "Error opening stats.csv file.");
dbc::pre("simple test", [&]() { return stats_out.good(); });
// need to catch the error message when the command is bad
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;
}
game.start_round(); string read_line(FILE *build_out, bool &done_out) {
char buffer[BUF_MAX];
while(fgets(buffer, BUF_MAX, build_out) != nullptr) { char *res = fgets(buffer, BUF_MAX, build_out);
string line(buffer); // yeah, that's probably a problem //!!! exception if res == nullptr
done_out = res == nullptr;
std::smatch err;
bool match = std::regex_match(line, err, err_re);
if(match) {
string file_name = err[1].str();
string lnumber = err[2].str();
string col = err[3].str();
string type = err[4].str();
string message = err[5].str();
string result = format("{:%FT%T},{},{},{},{},{}",
fmt::localtime(tstamp), file_name,
lnumber, col, type, message);
stats_out << result;
gui.output(format("HIT WITH {} @ {}:{}:{} {}", type, file_name, lnumber, col, message));
game.hit(type);
// refactor this if(!done_out) {
if(game.is_dead()) { return string{buffer}; // yeah, that's probably a problem
gui.you_died(); } else {
} return "";
}
} }
}
void end_build(FILE *build_out, GUI &gui, string &build_cmd) {
game.end_round();
int rc = pclose(build_out); int rc = pclose(build_out);
if(rc == 0) { if(rc == 0) {
gui.build_works(); gui.build_works();
} else { } else {
gui.output(format(">>>>>>>> DIED? {}", game.is_dead())); // BUG: make it not play two sounds
gui.build_failed(!game.is_dead(), build_cmd); gui.build_failed(true, build_cmd);
} }
}
void Builder::run_build(const string &line) {
std::regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*");
std::smatch err;
bool match = std::regex_match(line, err, err_re);
stats_out.close(); if(match) {
dbc::post("a post test", [&]() { return !stats_out.is_open(); }); string file_name = err[1].str();
string lnumber = err[2].str();
string col = err[3].str();
string type = err[4].str();
string message = err[5].str();
gui.output(format("HIT WITH {} @ {}:{}:{} {}", type, file_name, lnumber, col, message));
game.hit(type);
}
} }
enum BuildState {
WAITING, BUILDING, DONE
};
void Builder::run() { void Builder::run() {
git_repository* repo = nullptr; git_repository* repo = nullptr;
@ -120,22 +109,53 @@ void Builder::run() {
gui.output(format("Watching directory {} for changes...", git_path)); gui.output(format("Watching directory {} for changes...", git_path));
efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true); efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true);
int rc = gui.main_loop(game, [&] { FILE *build_out = NULL;
fileWatcher->watch(); bool build_done = false;
BuildState state = WAITING;
if(listener->changes) { fileWatcher->watch();
gui.building();
gui.output(format("CHANGES! Running build {}", build_cmd));
run_build(); int rc = gui.main_loop(game, [&] {
switch(state) {
case WAITING:
if(listener->changes) {
game.start_round();
gui.building();
gui.output(format("CHANGES! Running build {}", build_cmd));
build_out = start_command(build_cmd);
state = BUILDING;
}
break;
case BUILDING: {
// check if there's output
string line = read_line(build_out, build_done);
if(build_done) {
end_build(build_out, gui, build_cmd);
build_out = NULL;
state = DONE;
} else {
// get the command line
// run the build if it's available
run_build(line);
state = BUILDING;
}
}
break;
case DONE: {
game.end_round();
if(game.is_dead()) { if(game.is_dead()) {
gui.output("!!!! YOU DIED! !!!! Learn to code luser."); gui.you_died();
game.reset(); game.reset();
} }
listener->reset_state(); listener->reset_state();
gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^"); gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^");
state = WAITING;
}
break;
} }
return 0; return 0;

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "gui.hpp" #include "gui.hpp"
#include "game_engine.hpp" #include "game_engine.hpp"
#include <stdio.h>
using std::string; using std::string;
@ -14,7 +15,7 @@ class Builder {
Builder(GUI &g, GameEngine &engine); Builder(GUI &g, GameEngine &engine);
void run_build(); void run_build(const string &line);
void run(); void run();
}; };

@ -85,6 +85,7 @@ void GUI::build_failed(bool play_sound, const string &command) {
void GUI::you_died() { void GUI::you_died() {
building_sound.stop(); building_sound.stop();
you_died_sound.play(); you_died_sound.play();
output("!!!! YOU DIED! !!!! Learn to code luser.");
output("YOU DIED!"); output("YOU DIED!");
} }

@ -46,6 +46,9 @@ executable('jsontest', 'jsontest.cpp',
executable('threadtest', 'threadtest.cpp', executable('threadtest', 'threadtest.cpp',
dependencies: dependencies) dependencies: dependencies)
executable('badref', 'badref.cpp',
dependencies: dependencies)
executable('corotest', [ executable('corotest', [
'corotest.cpp' 'corotest.cpp'
], ],

@ -1,34 +1,54 @@
#include <thread> #include <condition_variable>
#include <fmt/core.h>
#include <chrono>
#include <iostream>
#include <thread>
#include <utility>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <string>
#include <thread>
using namespace std::chrono_literals; std::mutex m;
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;
std::atomic_int counter = 0; void worker_thread()
std::mutex counter_mutex; {
// 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";
void locked_counter() { // manual unlocking is done before notifying, to avoid waking up
for (int i = 0; i < 5; ++i) // the waiting thread only to block again (see notify_one for details)
{ lk.unlock();
std::lock_guard<std::mutex> lock(counter_mutex); cv.notify_one();
std::this_thread::sleep_for(100ms);
std::cout << "Thread 1 executing\n";
++counter;
}
} }
int main() int main()
{ {
std::jthread t2(locked_counter); // pass by value std::thread worker(worker_thread);
data = "Example data";
// send data to the worker thread
{
std::lock_guard lk(m);
ready = true;
std::cout << "main() signals data ready for processing\n";
}
cv.notify_one();
// wait for the worker
{
std::unique_lock lk(m);
cv.wait(lk, []{ return processed; });
}
std::cout << "Back in main(), data = " << data << '\n';
for(int i = 0; i < 5; ++i) { worker.join();
std::lock_guard<std::mutex> lock(counter_mutex);
std::cout << "counter is " << counter << std::endl;
}
} }

Loading…
Cancel
Save