00001 /*************************************************************************** 00002 LOW_thread_thread.h - description 00003 ------------------- 00004 begin : Mon Sep 29 2003 00005 copyright : (C) 2003 by Harald Roelle 00006 email : roelle@informatik.uni-muenchen.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #ifndef LOW_THREAD_THREAD_H 00019 #define LOW_THREAD_THREAD_H 00020 00021 00022 #include "LOW_exception.h" 00023 #include "LOW_thread_Factory.h" 00024 00025 00026 00027 /** Abstract base class for threads. 00028 Each instance represents one thread. 00029 00030 Specific platforms dereive their implementation classes from this class. 00031 This interface is strongly inspired by the abilities of POSIX threads. As not all 00032 OS specific threads have the same abilities, implementation of some methods are 00033 optional. Methods are marked accordingly. The default implementation of optional 00034 methods just throw a thread_thread_notAvailable exception. 00035 00036 Optional methods are mainly related to the following features: 00037 - Scheduling 00038 - Detaching 00039 - Cancellation 00040 Avaliability of optional methods can be determined by corresponding methods. 00041 00042 The instances are created by LOW_thread_Factory, following the factory 00043 design pattern. 00044 00045 This class is thread-safe. 00046 00047 @see LOW_thread_Factory 00048 00049 @author Harald Roelle 00050 @author Parts of the documentation taken from Linux man pages by Xavier Leroy. 00051 @author Parts of the documentation taken from IEEE Standard 1003.1-2003. 00052 */ 00053 class LOW_thread_thread { 00054 00055 //======================================================================================= 00056 public: 00057 00058 //===================================================================================== 00059 // 00060 // exceptions 00061 // 00062 00063 /** Exception base class for all exceptions thrown by LOW_thread_thread. */ 00064 class_DERIVE_FROM_EXCEPTION( thread_thread_error, LOW_exception); 00065 00066 /** Exception class indicating insufficient access rights. */ 00067 class_DERIVE_FROM_EXCEPTION( thread_thread_permission, thread_thread_error); 00068 00069 /** Exception class indicating that an option method is not implemented. */ 00070 class_DERIVE_FROM_EXCEPTION( thread_thread_notAvailable, thread_thread_error); 00071 00072 00073 //===================================================================================== 00074 // 00075 // constructors 00076 // 00077 00078 /** Destructor. 00079 */ 00080 virtual ~LOW_thread_thread(); 00081 00082 00083 //===================================================================================== 00084 // 00085 // methods 00086 // 00087 00088 /** 00089 @name Methods callable by any thread. 00090 */ 00091 //!@{ 00092 00093 /** Indicates if the thread was started. 00094 00095 Implementation is mandatory. 00096 00097 @return Bool indicating if thread was started. 00098 */ 00099 virtual bool getIsStarted() const = 0; 00100 00101 00102 /** Indicates if the thread has terminated. 00103 00104 Implementation is mandatory. 00105 00106 @return Bool indicating if thread has terminated. 00107 */ 00108 virtual bool getIsTerminated() const = 0; 00109 00110 00111 /** Get the return value of the thread. 00112 00113 Implementation is mandatory. 00114 00115 @return Return value of terminated thread. 00116 @throw thread_thread_error When thread has not terminated yet. 00117 */ 00118 virtual int getReturnValue() const = 0; 00119 00120 00121 /** Actually create the thread and starts it's runnable's run() method. 00122 00123 The new thread terminates either explicitly, by calling thread_exit(), or 00124 implicitly, by returning from the run() method. The latter case is equivalent 00125 to calling thread_exit() with the result returned by run() as exit code. 00126 00127 Implementation is mandatory. 00128 00129 @throw thread_thread_error On any error. 00130 */ 00131 virtual void create() = 0; 00132 00133 00134 /** Cancel the execution of the thread. 00135 00136 Note that the thread is not forced to quit. It depends on the thread implementation 00137 when or or if anyway a cancellation request is honored. 00138 00139 Implementation is mandatory. 00140 00141 @throw thread_thread_error On any error. 00142 */ 00143 virtual void cancel() = 0; 00144 00145 00146 /** Wait for the termination of the thread. 00147 00148 Suspends the execution of the calling thread until the thread terminates, either 00149 by calling thread_exit() or by being cancelled. 00150 00151 Implementation is mandatory. 00152 00153 @throw thread_thread_error On any error. 00154 */ 00155 virtual int join() = 0; 00156 00157 00158 /** Indicate if thread detaching is available. 00159 */ 00160 virtual bool getDetachAvialable() const; 00161 00162 00163 /** Put a running thread in the detached state. 00164 00165 Implementation is optional. 00166 00167 @throw thread_thread_error On any error. 00168 00169 @see getDetachAvialable() 00170 */ 00171 virtual void detach(); 00172 00173 00174 /** Indicate if thread scheduling modification is available. 00175 */ 00176 virtual bool getScheduleModifyAvailable() const; 00177 00178 00179 /** Set policy and parameter of scheduling. 00180 00181 @param inPolicy Scheduling policy to apply. 00182 @param inPolicy Scheduling parameter to apply. 00183 00184 @throw thread_thread_error On any error. 00185 */ 00186 virtual void setScheduling( const LOW_thread_Factory::schedPolicy_t inPolicy, const LOW_thread_Factory::schedPrio_t inPrio); 00187 00188 00189 /** Get the scheduling policy. 00190 00191 @throw thread_thread_error On any error. 00192 */ 00193 virtual LOW_thread_Factory::schedPolicy_t getSchedPolicy() const; 00194 00195 00196 /** Get the scheduling parameter. 00197 00198 @throw thread_thread_error On any error. 00199 */ 00200 virtual LOW_thread_Factory::schedPrio_t getSchedPrio() const; 00201 00202 00203 /** Indicate if thread cancelling attributes are available. 00204 */ 00205 virtual bool getCancelAttribsAvialable() const; 00206 00207 00208 /** Get cancelling behaviour. 00209 00210 Implementation is optional. 00211 00212 @throw thread_thread_error On any error. 00213 */ 00214 virtual LOW_thread_Factory::cancelAttrib_t getCancelAttrib() const; 00215 00216 //!@} 00217 00218 00219 /** 00220 @name Methods only callable by the own thread. 00221 */ 00222 //!@{ 00223 00224 /** Terminate the calling thread. 00225 00226 Method can only be called by the own thread. 00227 00228 Implementation is mandatory. 00229 00230 @param inRetVal Return value to quit with. 00231 00232 @throw thread_thread_permission Calling thread is not the object's thread. 00233 @throw thread_thread_error On any other error. 00234 */ 00235 virtual void thread_exit( const int inRetVal) = 0; 00236 00237 00238 /** Test on pending cancellation. 00239 00240 Method does nothing except testing for pending cancellation and executing it. 00241 Its purpose is to introduce explicit checks for cancellation in long sequences 00242 of code that do not call cancellation point functions otherwise. 00243 00244 Implementation is mandatory. 00245 00246 @throw thread_thread_permission Calling thread is not the object's thread. 00247 @throw thread_thread_error On any other error. 00248 00249 @see cancel() 00250 */ 00251 virtual void testCancel() = 0; 00252 00253 00254 /** Set cancelling behaviour. 00255 00256 Changes the cancellation state for the calling thread -- that is, whether 00257 cancellation requests are ignored or not and when they are handled. 00258 00259 Implementation is optional. 00260 00261 @param inCancelAttrib Cancelling handling attribute to set. 00262 00263 @throw thread_thread_permission Calling thread is not the object's thread. 00264 @throw thread_thread_error On any other error. 00265 00266 @see getCancelAttribsAvialable() 00267 */ 00268 virtual void setCancelAttrib( const LOW_thread_Factory::cancelAttrib_t inCancelAttrib); 00269 00270 //!@} 00271 00272 00273 //======================================================================================= 00274 private: 00275 00276 /** Helper method for thread launching. 00277 00278 This method gives the possibility to interface with OS-specific calls which require 00279 C-style linkage conventions. 00280 00281 Note that when imlementing such a method, special care 00282 about thread-safety must be taken. 00283 */ 00284 static void* threadRunner( void*); 00285 }; 00286 00287 #endif