X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/6e0194f46ac77c18bfeabf84ef94a32f23e41073..4404d8472c0088ec061b2f4f6527412f7ba23569:/Server/src/runnable.cpp diff --git a/Server/src/runnable.cpp b/Server/src/runnable.cpp index 0e8b4bd..c134b83 100644 --- a/Server/src/runnable.cpp +++ b/Server/src/runnable.cpp @@ -26,30 +26,56 @@ // #include "plaqui/server/runnable.h" -#include +#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(0), stop(false) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": constructor." << endl; +#endif // DEBUG +} + +void Runnable::static_run(Runnable* runner) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": static_run(runner = " << runner << ")" + << endl; +#endif // DEBUG + runner->real_run(); + 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. + // Corremos el thread en una funcion estática para poder destruirlo al + // finalizar, pasandole el puntero al objeto. thread = Glib::Thread::create( - SigC::slot_class(*this, &Runnable::real_run), true); + SigC::bind(SigC::slot(&Runnable::static_run), this), + true); // Si no corremos la tarea normalmente. } else { real_run(); @@ -57,3 +83,26 @@ void Runnable::run(bool detach) { } +void Runnable::finish(bool attach) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": finish(attach = " << attach << ")" << endl; +#endif // DEBUG + stop = true; + if (attach) { + thread->join(); + } +} + +Runnable::SignalFinished& Runnable::signal_finished(void) { + return finished; +} + +Runnable::SignalError& Runnable::signal_error(void) { + return error; +} + +} // namespace Server + +} // namespace PlaQui +