X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/ab9300aaf1217f0c3b45dd5f1f1fdc1c5e5bd1be..6f70c17ffefd6150b68ac0f64a8116e730437408:/Server/src/runnable.cpp?ds=inline diff --git a/Server/src/runnable.cpp b/Server/src/runnable.cpp index 7fd4105..9587fd9 100644 --- a/Server/src/runnable.cpp +++ b/Server/src/runnable.cpp @@ -26,50 +26,56 @@ // #include "plaqui/server/runnable.h" -#include +#include #include #ifdef DEBUG # include #endif // DEBUG -PlaQui::Server::Runnable::~Runnable(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +Runnable::~Runnable(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor(this = " << this << ")" - << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": destructor(this = " << this << ")." << endl; #endif // DEBUG } -PlaQui::Server::Runnable::Runnable(void): thread(0), stop(false) { +Runnable::Runnable(void): _thread(NULL), _stop(false) { #ifdef DEBUG - std::cerr << __FILE__ << ": constructor." << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": constructor(this = " << this << ")." << endl; #endif // DEBUG } -void PlaQui::Server::Runnable::static_run(Runnable* runner) { +void Runnable::static_run(Runnable* runner) { #ifdef DEBUG - std::cerr << __FILE__ << ": static_run(runner = " << runner << ")" - << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": static_run(runner = " << runner << ")" << endl; #endif // DEBUG + // Corre tarea. runner->real_run(); - runner->signal_finished().emit(); + // Manda señal de tarea finalizada + runner->_finished(); delete runner; } -void PlaQui::Server::Runnable::run(bool detach) { +void Runnable::run(bool detach) { #ifdef DEBUG - std::cerr << __FILE__ << ": run(detach = " << detach << ")" << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": run(detach = " << detach << ")" << endl; #endif // DEBUG // Si vamos a correr la tarea en un thread. if (detach) { - // Nos aseguramos de tener threads. - if (!Glib::thread_supported()) { - Glib::thread_init(); - } // Corremos el thread en una funcion estática para poder destruirlo al // finalizar, pasandole el puntero al objeto. - thread = Glib::Thread::create( + _thread = Glib::Thread::create( SigC::bind(SigC::slot(&Runnable::static_run), this), - true); + false); // Si no corremos la tarea normalmente. } else { real_run(); @@ -77,19 +83,36 @@ void PlaQui::Server::Runnable::run(bool detach) { } -void PlaQui::Server::Runnable::finish(bool attach) { +void Runnable::finish(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": finish(attach = " << attach << ")" << std::endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": finish();" << endl; #endif // DEBUG - // TODO - necesita un mutex? - stop = true; - if (attach) { - thread->join(); - } + stop(true); +} + +bool Runnable::stop(void) { + Glib::Mutex::Lock lock(stop_mutex); + bool tmp = _stop; + return tmp; } -PlaQui::Server::Runnable::SignalFinished& -PlaQui::Server::Runnable::signal_finished(void) { - return finished; +bool Runnable::stop(bool stop) { + Glib::Mutex::Lock lock(stop_mutex); + bool tmp = _stop; + _stop = stop; + return tmp; } +Runnable::SignalFinished& Runnable::signal_finished(void) { + return _finished; +} + +Runnable::SignalError& Runnable::signal_error(void) { + return _error; +} + +} // namespace Server + +} // namespace PlaQui +