pthread.cpp

Go to the documentation of this file.
00001 /*
00002 #########################################################################
00003 #
00004 #  This file is part of trustyRC.
00005 #
00006 #  trustyRC, fully modular IRC robot 
00007 #  Copyright (C) 2006-2008 Nicoleau Fabien 
00008 #
00009 #  trustyRC is free software: you can redistribute it and/or modify
00010 #  it under the terms of the GNU General Public License as published by
00011 #  the Free Software Foundation, either version 3 of the License, or
00012 #  (at your option) any later version.
00013 #
00014 #  trustyRC is distributed in the hope that it will be useful,
00015 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 #  GNU General Public License for more details.
00018 #
00019 #  You should have received a copy of the GNU General Public License
00020 #  along with trustyRC.  If not, see <http://www.gnu.org/licenses/>.
00021 #
00022 #########################################################################
00023 */
00024 
00029 #include "pthread.h"
00030 #include <iostream>
00031 using namespace std;
00032 
00037 PThread::PThread() {
00038    this->finished = false;
00039    this->running = false;
00040    this->handle = new pthread_t;
00041 }
00042 
00046 PThread::~PThread() {
00047    this->terminate();
00048    pthread_join (*this->handle, NULL);
00049    delete this->handle;
00050 }
00051 
00060 bool PThread::exec(threadProcess myThreadProcess,void*args) {
00061    if ( ! this->isRunning() ) {
00062       threadParams tp;
00063       tp.process = myThreadProcess;
00064       tp.args = args;
00065       tp.running = &this->running;
00066       tp.finished = &this->finished;
00067       if (pthread_create (this->handle, NULL,PThread::threadStartup,(void*)&tp) < 0) {
00068          return false;
00069       }
00070       return true;
00071    }
00072    return false;
00073 }
00074 
00081 void* PThread::threadStartup(void*targs) {
00082    threadParams* tp = (threadParams*)targs;
00083    bool* running = tp->running;
00084    bool* finished = tp->finished;
00085    threadProcess process = tp->process;
00086    void * args = tp->args;
00087    *running = true;
00088    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
00089    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
00090    process(args);
00091    *running = false;
00092    *finished = true;
00093    pthread_exit(0);
00094 }
00095 
00100 bool PThread::terminate() {
00101    if ( this->isRunning() ) {
00102       if (pthread_cancel (*this->handle) != 0) {
00103          return false;
00104       }
00105       this->running = false;
00106       return true;
00107    }
00108    return false;
00109 }
00110 
00115 bool PThread::isRunning() {
00116    return this->running;
00117 }
00118 
00125 bool PThread::isFinished() {
00126    return this->finished;
00127 }
00128 
00132 void* PThread::join() {
00133    void* ret;
00134    pthread_join(*this->handle,&ret);
00135    return ret;
00136 }
00137 
00144 pthread_t* PThread::getHandle() {
00145    return this->handle;
00146 }

Generated on Sun Apr 19 02:47:18 2009 for trustyRC by  doxygen 1.5.7.1