00001 /****************************************************************************** 00002 * __ _ _ _ _ _ _____ * 00003 * | \ / | | | | | | | | | | ___| * 00004 * | \/ | | | | | | | | | | |_ * 00005 * | | | | | | | | | | | _| * 00006 * | |\/| | | | | |__ | |__ | | | |__ * 00007 * |_| |_| |_| |____| |____| |_| |____| * 00008 * __________________________________________________________________________ * 00009 * Multifunctional Library For Image Processing * 00010 * * 00011 * * 00012 * * 00013 * (c) Copyright 2007 by Humbert Florent * 00014 * * 00015 * This program is free software; you can redistribute it and/or modify * 00016 * it under the terms of the GNU General Public License as published by * 00017 * the Free Software Foundation; only version 2 of the License. * 00018 * * 00019 * This program is distributed in the hope that it will be useful, * 00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00022 * GNU General Public License for more details. * 00023 * * 00024 * You should have received a copy of the GNU General Public License * 00025 * along with this program; if not, write to the Free Software * 00026 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 00027 * 02111-1307, USA. * 00028 ******************************************************************************/ 00029 00032 #ifndef _MILLIETHREAD_HPP_ 00033 #define _MILLIETHREAD_HPP_ 00034 00035 #include <pthread.h> 00036 #include <iostream> 00037 #include <vector> 00038 #include "MillieExceptions.hpp" 00039 #include "Mutex.hpp" 00040 00041 namespace Millie 00042 { 00043 00047 typedef pthread_t ThreadID; 00048 00053 void * threadHandler(void * pthread); 00054 00055 class ThreadGroup; 00056 00108 class Thread 00109 { 00110 00111 private: 00112 00113 std::string _name; 00114 bool _isAlive; 00115 bool _isStarted; 00116 bool _wasJoined; 00117 00118 Mutex _synchronized; 00119 pthread_mutex_t _waitMutex; 00120 Mutex _synchronizedWait; 00121 Mutex _synchronizedRun; 00122 Mutex _synchronizedIsAlive; 00123 Mutex _synchronizedStart; 00124 Mutex _synchronizedWasJoined; 00125 00126 pthread_cond_t _waitSynchro; 00127 pthread_t _thread; 00128 pthread_mutex_t _mutexWaitAll; 00129 00130 void init(); 00131 00132 protected: 00133 Mutex _synchronizedIsRunning; 00134 bool _isRunning; 00135 public: 00136 00140 Thread(); 00141 00142 00148 Thread(const std::string & name); 00149 00156 bool isRunning(); 00157 00163 void wait(); 00164 00165 bool isAlive(); 00166 00172 bool isStarted(); 00173 00182 void start(); 00183 00187 void join(); 00188 00192 static ThreadID currentThread(); 00193 00197 void notify(); 00198 00204 const std::string & getName() const; 00205 00209 virtual void run() {}; 00210 00214 ThreadID getThreadId() const; 00215 00222 virtual ~Thread(); 00223 00227 friend void * threadHandler(void * pthread); 00228 }; 00229 00230 00231 00232 00233 } 00234 00235 #endif