Line data Source code
1 : #include <csignal> 2 : #include <cstdlib> 3 : 4 : #include "Tools/Display/Terminal/Terminal.hpp" 5 : 6 : using namespace spu; 7 : using namespace spu::tools; 8 : 9 14 : Terminal::Terminal() 10 14 : : stop_terminal(false) 11 14 : , t_term(std::chrono::steady_clock::now()) 12 14 : , real_time_state(0) 13 : { 14 14 : } 15 : 16 14 : Terminal::~Terminal() 17 : { 18 14 : stop_temp_report(); // try to join the thread if this is not been done by the user 19 14 : } 20 : 21 : void 22 103 : Terminal::temp_report(std::ostream& stream) 23 : { 24 103 : this->report(stream, false); 25 103 : } 26 : 27 : void 28 14 : Terminal::final_report(std::ostream& stream) 29 : { 30 14 : this->stop_temp_report(); 31 14 : this->report(stream, true); 32 14 : t_term = std::chrono::steady_clock::now(); 33 14 : } 34 : 35 : void 36 0 : Terminal::start_temp_report(const std::chrono::milliseconds freq) 37 : { 38 0 : this->stop_temp_report(); 39 : 40 : // launch a thread dedicated to the terminal display 41 0 : term_thread = std::thread(Terminal::start_thread_terminal, this, freq); 42 : 43 0 : t_term = std::chrono::steady_clock::now(); 44 0 : } 45 : 46 : void 47 28 : Terminal::stop_temp_report() 48 : { 49 28 : if (term_thread.joinable()) 50 : { 51 0 : stop_terminal = true; 52 0 : cond_terminal.notify_all(); 53 : // wait the terminal thread to finish 54 0 : term_thread.join(); 55 0 : stop_terminal = false; 56 : } 57 : 58 28 : real_time_state = 0; 59 28 : } 60 : 61 : void 62 0 : Terminal::start_thread_terminal(Terminal* terminal, const std::chrono::milliseconds freq) 63 : { 64 0 : const auto sleep_time = freq - std::chrono::milliseconds(0); 65 0 : while (!terminal->stop_terminal) 66 : { 67 0 : std::unique_lock<std::mutex> lock(terminal->mutex_terminal); 68 0 : if (terminal->cond_terminal.wait_for(lock, sleep_time) == std::cv_status::timeout) 69 0 : terminal->temp_report(std::clog); // display statistics in the terminal 70 0 : } 71 0 : }