UtiLite
0.3.1
A lite utilities library
|
00001 /* 00002 * utilite is a cross-platform library with 00003 * useful utilities for fast and small developing. 00004 * Copyright (C) 2010 Mathieu Labbe 00005 * 00006 * utilite is free library: you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * utilite is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #ifndef UTHREADNODE_H 00021 #define UTHREADNODE_H 00022 00023 #include "utilite/UtiLiteExp.h" // DLL export/import defines 00024 00025 #include "utilite/UThreadC.h" 00026 00086 class UTILITE_EXP UThread : public UThreadC<void> 00087 { 00088 public: 00092 enum Priority{kPLow, kPBelowNormal, kPNormal, kPAboveNormal, kPRealTime}; 00093 00094 public: 00100 UThread(Priority priority = kPNormal); 00101 00108 virtual ~UThread(); 00109 00115 void start(); 00116 00123 void kill(); 00124 00131 void join(bool killFirst = false); 00132 00137 void setPriority(Priority priority); 00138 00145 void setAffinity(int cpu = 0); 00146 00150 bool isCreating() const; 00151 00155 bool isRunning() const; 00156 00160 bool isIdle() const; 00161 00165 bool isKilled() const; 00166 00167 Handle getThreadHandle() const {return handle_;} 00168 00169 00170 protected: 00171 00172 private: 00179 virtual void mainLoopBegin() {} 00180 00190 virtual void mainLoop() = 0; 00191 00199 virtual void mainLoopKill() {} 00200 00207 virtual void mainLoopEnd() {} 00208 00209 /* 00210 * Inherited method ThreadMain() from Thread. 00211 * @see Thread<void> 00212 */ 00213 void ThreadMain(); 00214 00215 /* 00216 * Apply thread priority. This is called when starting the thread. 00217 * *@todo : Support pthread 00218 */ 00219 void applyPriority(); 00220 00221 /* 00222 * Apply cpu affinity. This is called when starting the thread. 00223 * *@todo : Support Windows 00224 */ 00225 void applyAffinity(); 00226 00227 /* 00228 * Inherited method Create() from Thread. 00229 * Here we force this function to be private so the 00230 * inherited class can't have access to it. 00231 * @see Thread<void> 00232 */ 00233 int Create( 00234 Handle * const & H = 0, 00235 const bool & CreateDetached = false, 00236 const unsigned int & StackSize = 0, 00237 const bool & CancelEnable = false, // UNUSED 00238 const bool & CancelAsync = false // UNUSED 00239 ) const; 00240 00241 //Methods from UThread<void> class hided 00242 static int Join( Handle H ) 00243 { return UThreadC<void>::Join(H); } 00244 static int Kill( Handle H ) 00245 { return UThreadC<void>::Kill(H); } 00246 static int Detach( Handle H ) 00247 { return UThreadC<void>::Detach(H); } 00248 00249 private: 00250 void operator=(UThread & t) {} 00251 UThread( const UThread & t ) {} 00252 00253 private: 00254 enum State{kSIdle, kSCreating, kSRunning, kSKilled}; /* Enum of states. */ 00255 State state_; /* The thread state. */ 00256 Priority priority_; /* The thread priority. */ 00257 Handle handle_; /* The thread handle. */ 00258 #ifdef WIN32 00259 int threadId_; /* The thread id. */ 00260 #else 00261 unsigned long threadId_; /* The thread id. */ 00262 #endif 00263 int cpuAffinity_; /* The cpu affinity. */ 00264 UMutex killSafelyMutex_; /* Mutex used to protect the kill() method. */ 00265 UMutex runningMutex_; /* Mutex used to notify the join method when the thread has finished. */ 00266 }; 00267 00268 #endif // UTHREADNODE_H