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
112
builder.cpp
112
builder.cpp
|
@ -36,29 +36,39 @@ Builder::Builder(GUI &g, GameEngine &engine)
|
|||
config["build_cmd"].template get_to<string>(build_cmd);
|
||||
}
|
||||
|
||||
void Builder::run_build() {
|
||||
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 *start_command(string &build_cmd) {
|
||||
FILE *build_out = popen(build_cmd.c_str(), "r");
|
||||
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) {
|
||||
string line(buffer); // yeah, that's probably a problem
|
||||
if(!done_out) {
|
||||
return string{buffer}; // yeah, that's probably a problem
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void end_build(FILE *build_out, GUI &gui, string &build_cmd) {
|
||||
int rc = pclose(build_out);
|
||||
|
||||
if(rc == 0) {
|
||||
gui.build_works();
|
||||
} else {
|
||||
// BUG: make it not play two sounds
|
||||
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);
|
||||
|
@ -70,37 +80,16 @@ void Builder::run_build() {
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
game.end_round();
|
||||
|
||||
int rc = pclose(build_out);
|
||||
if(rc == 0) {
|
||||
gui.build_works();
|
||||
} else {
|
||||
gui.output(format(">>>>>>>> DIED? {}", game.is_dead()));
|
||||
gui.build_failed(!game.is_dead(), build_cmd);
|
||||
}
|
||||
|
||||
stats_out.close();
|
||||
dbc::post("a post test", [&]() { return !stats_out.is_open(); });
|
||||
}
|
||||
|
||||
enum BuildState {
|
||||
WAITING, BUILDING, DONE
|
||||
};
|
||||
|
||||
void Builder::run() {
|
||||
git_repository* repo = nullptr;
|
||||
|
||||
|
@ -120,22 +109,53 @@ void Builder::run() {
|
|||
gui.output(format("Watching directory {} for changes...", git_path));
|
||||
efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true);
|
||||
|
||||
int rc = gui.main_loop(game, [&] {
|
||||
FILE *build_out = NULL;
|
||||
bool build_done = false;
|
||||
BuildState state = WAITING;
|
||||
|
||||
fileWatcher->watch();
|
||||
|
||||
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;
|
||||
|
||||
run_build();
|
||||
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()) {
|
||||
gui.output("!!!! YOU DIED! !!!! Learn to code luser.");
|
||||
gui.you_died();
|
||||
game.reset();
|
||||
}
|
||||
|
||||
listener->reset_state();
|
||||
gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^");
|
||||
state = WAITING;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
#include "gui.hpp"
|
||||
#include "game_engine.hpp"
|
||||
#include <stdio.h>
|
||||
|
||||
using std::string;
|
||||
|
||||
|
@ -14,7 +15,7 @@ class Builder {
|
|||
|
||||
Builder(GUI &g, GameEngine &engine);
|
||||
|
||||
void run_build();
|
||||
void run_build(const string &line);
|
||||
|
||||
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() {
|
||||
building_sound.stop();
|
||||
you_died_sound.play();
|
||||
output("!!!! YOU DIED! !!!! Learn to code luser.");
|
||||
output("YOU DIED!");
|
||||
}
|
||||
|
||||
|
|
|
@ -46,6 +46,9 @@ executable('jsontest', 'jsontest.cpp',
|
|||
executable('threadtest', 'threadtest.cpp',
|
||||
dependencies: dependencies)
|
||||
|
||||
executable('badref', 'badref.cpp',
|
||||
dependencies: dependencies)
|
||||
|
||||
executable('corotest', [
|
||||
'corotest.cpp'
|
||||
],
|
||||
|
|
|
@ -1,34 +1,54 @@
|
|||
#include <thread>
|
||||
#include <fmt/core.h>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <condition_variable>
|
||||
#include <iostream>
|
||||
#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;
|
||||
std::mutex counter_mutex;
|
||||
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";
|
||||
|
||||
void locked_counter() {
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(counter_mutex);
|
||||
std::this_thread::sleep_for(100ms);
|
||||
std::cout << "Thread 1 executing\n";
|
||||
++counter;
|
||||
}
|
||||
// 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()
|
||||
{
|
||||
std::jthread t2(locked_counter); // pass by value
|
||||
std::thread worker(worker_thread);
|
||||
|
||||
for(int i = 0; i < 5; ++i) {
|
||||
std::lock_guard<std::mutex> lock(counter_mutex);
|
||||
std::cout << "counter is " << counter << std::endl;
|
||||
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';
|
||||
|
||||
worker.join();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue