First step in refactoring out the build running from the gui for later thread fiascos.
This commit is contained in:
parent
b0c9fefa9b
commit
0bac4dbfd9
5 changed files with 125 additions and 80 deletions
134
builder.cpp
134
builder.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];
|
||||||
|
char *res = fgets(buffer, BUF_MAX, build_out);
|
||||||
|
//!!! exception if res == nullptr
|
||||||
|
done_out = res == nullptr;
|
||||||
|
|
||||||
while(fgets(buffer, BUF_MAX, build_out) != nullptr) {
|
if(!done_out) {
|
||||||
string line(buffer); // yeah, that's probably a problem
|
return string{buffer}; // yeah, that's probably a problem
|
||||||
|
} else {
|
||||||
std::smatch err;
|
return "";
|
||||||
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(game.is_dead()) {
|
|
||||||
gui.you_died();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
stats_out.close();
|
|
||||||
dbc::post("a post test", [&]() { return !stats_out.is_open(); });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
FILE *build_out = NULL;
|
||||||
|
bool build_done = false;
|
||||||
|
BuildState state = WAITING;
|
||||||
|
|
||||||
|
fileWatcher->watch();
|
||||||
|
|
||||||
int rc = gui.main_loop(game, [&] {
|
int rc = gui.main_loop(game, [&] {
|
||||||
fileWatcher->watch();
|
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;
|
||||||
|
|
||||||
if(listener->changes) {
|
case BUILDING: {
|
||||||
gui.building();
|
// check if there's output
|
||||||
gui.output(format("CHANGES! Running build {}", build_cmd));
|
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;
|
||||||
|
|
||||||
run_build();
|
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();
|
||||||
};
|
};
|
||||||
|
|
1
gui.cpp
1
gui.cpp
|
@ -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";
|
||||||
|
|
||||||
void locked_counter() {
|
// send data back to main()
|
||||||
for (int i = 0; i < 5; ++i)
|
processed = true;
|
||||||
{
|
std::cout << "Worker thread signals data processing completed\n";
|
||||||
std::lock_guard<std::mutex> lock(counter_mutex);
|
|
||||||
std::this_thread::sleep_for(100ms);
|
// manual unlocking is done before notifying, to avoid waking up
|
||||||
std::cout << "Thread 1 executing\n";
|
// the waiting thread only to block again (see notify_one for details)
|
||||||
++counter;
|
lk.unlock();
|
||||||
}
|
cv.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::jthread t2(locked_counter); // pass by value
|
std::thread worker(worker_thread);
|
||||||
|
|
||||||
for(int i = 0; i < 5; ++i) {
|
data = "Example data";
|
||||||
std::lock_guard<std::mutex> lock(counter_mutex);
|
// send data to the worker thread
|
||||||
std::cout << "counter is " << counter << std::endl;
|
{
|
||||||
}
|
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';
|
||||||
|
|
||||||
|
worker.join();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue