From 1e979990239a846618811f27c002345e9d654fed Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 14 Oct 2018 17:03:57 +0100 Subject: Add terminated status --- src/main/solver/solver.cpp | 18 +++++++++++++++++- src/main/solver/solver.h | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) (limited to 'src/main/solver') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index e0a5990..c4e8c84 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "solver.h" #include "../game/move.h" @@ -29,6 +30,13 @@ using boost::optional; using std::fixed; using std::setprecision; +static bool sigint = false; + +void sigint_handler(int i) { + // So it doesn't complain about unused param + sigint = i == 1 ? true : true; +} + solver::solver(const game_state& gs, uint64_t cache_capacity) : cache(lru_cache(gs, cache_capacity)) , init_state(gs) @@ -52,6 +60,10 @@ solver::node::node(const move m) } solver::result solver::run(boost::optional timeout) { + // Set interrupt handler + signal(SIGINT, sigint_handler); + + // Set timings const clock::time_point start_time = clock::now(); result::type res_type = timeout ? dfs(start_time + *timeout) : dfs(); res.sol_type = res_type; @@ -66,9 +78,10 @@ solver::result::type solver::dfs(boost::optional end_time) { bool states_exhausted = false; while(!(state.is_solved() || states_exhausted)) { - // If the terminate flag was supplied and has been set to true, return if (end_time && clock::now() >= *end_time) { return result::type::TIMEOUT; + } else if (sigint) { + return result::type::TERMINATED; } #ifndef NDEBUG @@ -225,6 +238,9 @@ std::ostream& operator<< (std::ostream& out, const solver::result::type& rt) { case solver::result::type::MEM_LIMIT: out << "memory-limit-reached"; break; + case solver::result::type::TERMINATED: + out << "terminated"; + break; } return out; } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 6b5704e..74c73a8 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -26,7 +26,7 @@ public: }; struct result { - enum class type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT }; + enum class type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT, TERMINATED }; type sol_type; uint64_t states_searched; @@ -73,6 +73,6 @@ private: std::ostream& operator<< (std::ostream&, const solver::result::type&); std::ostream& operator<< (std::ostream&, const solver::result&); - +void sigint_handler(int i); #endif //SOLVITAIRE_SOLVER_H -- cgit v1.2.1