X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/a9cb053146d2f1dc73e536ed87597f458deebea3..c82138b139ddc29ded10368db1327a565b688aeb:/Server/src/runnable.cpp diff --git a/Server/src/runnable.cpp b/Server/src/runnable.cpp index d962980..6d000ce 100644 --- a/Server/src/runnable.cpp +++ b/Server/src/runnable.cpp @@ -25,31 +25,56 @@ // $Id$ // -#include "runnable.h" -#include +#include "plaqui/server/runnable.h" +#include #include #ifdef DEBUG # include #endif // DEBUG -using namespace Plaqui; +using namespace std; -Runnable::Runnable(void): thread(0) { +namespace PlaQui { + +namespace Server { + +Runnable::~Runnable(void) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": destructor(this = " << this << ")." << endl; +#endif // DEBUG +} + +Runnable::Runnable(void): _thread(NULL), _stop(false) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": constructor(this = " << this << ")." << endl; +#endif // DEBUG +} + +void Runnable::static_run(Runnable* runner) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": static_run(runner = " << runner << ")" << endl; +#endif // DEBUG + // Corre tarea. + runner->real_run(); + // Manda señal de tarea finalizada + runner->_finished(); + delete runner; } void Runnable::run(bool detach) { #ifdef DEBUG - std::cerr << __FILE__ << ": 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. - thread = Glib::Thread::create( - SigC::slot_class(*this, &Runnable::real_run), true); + // Corremos el thread en una funcion estática para poder destruirlo al + // finalizar, pasandole el puntero al objeto. + _thread = Glib::Thread::create( + SigC::bind(SigC::slot(&Runnable::static_run), this), false); // Si no corremos la tarea normalmente. } else { real_run(); @@ -57,3 +82,36 @@ void Runnable::run(bool detach) { } +void Runnable::finish(void) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": finish();" << endl; +#endif // DEBUG + stop(true); +} + +bool Runnable::stop(void) { + Glib::Mutex::Lock lock(stop_mutex); + bool tmp = _stop; + return tmp; +} + +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 +