00001 /*************************************************************************** 00002 LOW_thread_thread_POSIX.h - description 00003 ------------------- 00004 begin : Thu Oct 2 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_POSIX_H 00019 #define LOW_THREAD_THREAD_POSIX_H 00020 00021 00022 #include "LOW_exception.h" 00023 #include "LOW_thread_mutex.h" 00024 #include "LOW_thread_runable.h" 00025 #include "LOW_thread_thread.h" 00026 00027 00028 00029 /** Platform specific thread_thread for POSIX platforms. 00030 00031 This class is thread-safe. 00032 00033 @todo PROVIDE ASSIGNMENT OPERATOR 00034 @todo Throw more fine grained exceptions. 00035 00036 @see LOW_thread_thread 00037 @see IEEE Standard 1003.1-2003 (http://www.opengroup.org/onlinepubs/007904975/toc.htm) 00038 00039 @author Harald Roelle 00040 @author Parts of the documentation taken from Linux man pages by Xavier Leroy. 00041 @author Parts of the documentation taken from IEEE Standard 1003.1-2003. 00042 */ 00043 class LOW_thread_thread_POSIX : public LOW_thread_thread { 00044 00045 //======================================================================================= 00046 public: 00047 00048 //===================================================================================== 00049 // 00050 // constants 00051 // 00052 00053 static const LOW_thread_Factory::schedPrio_t schedPrio_default = -1; /**< indicator for default scheduling priority. */ 00054 00055 00056 //===================================================================================== 00057 // 00058 // constructors 00059 // 00060 00061 /** Destructor. 00062 */ 00063 virtual ~LOW_thread_thread_POSIX(); 00064 00065 00066 //===================================================================================== 00067 // 00068 // methods 00069 // 00070 00071 /** 00072 @name Methods callable by any thread. 00073 */ 00074 //!@{ 00075 00076 /** Indicates if the thread was started. 00077 00078 @return Bool indicating if thread was started. 00079 */ 00080 virtual bool getIsStarted() const; 00081 00082 00083 /** Indicates if the thread has terminated. 00084 00085 @return Bool indicating if thread has terminated. 00086 */ 00087 virtual bool getIsTerminated() const; 00088 00089 00090 /** Get the return value of the thread. 00091 00092 @return Return value of terminated thread. 00093 @throw thread_thread_error When thread has not terminated yet. 00094 */ 00095 virtual int getReturnValue() const; 00096 00097 00098 /** Actually create the thread and starts it's runnable's run() method. 00099 00100 Execution is synchronized with threadRunner() via createSyncMutex. 00101 00102 @throw thread_thread_error On any error. 00103 */ 00104 virtual void create(); 00105 00106 00107 /** Cancel the execution of the thread. 00108 00109 Note that the thread is not forced to quit. 00110 Cancellation of POSIX threads depends on cancel state and cancel type. 00111 The thread can then either ignore the request, honor it immediately, or 00112 defer it till it reaches a cancellation point. 00113 00114 @throw thread_thread_error On any error. 00115 */ 00116 virtual void cancel(); 00117 00118 00119 /** Wait for the termination of the thread. 00120 00121 Suspends the execution of the calling thread until the thread terminates, either 00122 by calling thread_exit() or by being cancelled. 00123 00124 The thread must be in the joinable state: it must not have been detached using detach() 00125 or constructed as detached. 00126 00127 When a joinable thread terminates, its memory resources (thread descriptor and stack) are 00128 not deallocated until another thread performs join() on it. Therefore, join() must be 00129 called once for each joinable thread created to avoid memory leaks. When class is 00130 destructed the destructor takes care of this. 00131 00132 At most one thread can wait for the termination of a thread. Calling join() on a thread 00133 on which another thread is already waiting for termination throws an exception. 00134 00135 Implementation is mandatory. 00136 00137 @throw thread_thread_error On any error. 00138 00139 @see detach() 00140 */ 00141 virtual int join(); 00142 00143 00144 /** Put a running thread in the detached state. 00145 00146 Puts the thread in the detached state. This guarantees that the memory resources 00147 consumed by will be freed immediately when it terminates. This also prevents 00148 other threads from synchronizing on the termination using join(). 00149 00150 A thread can be created initially in the detached state, appropriate constructor argument. 00151 In contrast, detach() applies to threads created in the joinable state, and which need to 00152 be put in the detached state later. 00153 00154 After detach() completes, subsequent attempts to perform join() on it will fail. If 00155 another thread is already joining the thread at the time detach() is called, detach() does 00156 nothing and leaves the thread in the joinable state. 00157 00158 @throw thread_thread_error On any error. 00159 */ 00160 virtual void detach(); 00161 00162 00163 /** Set policy and parameter of scheduling. 00164 00165 @param inPolicy Scheduling policy to apply. 00166 @param inPolicy Scheduling parameter to apply. 00167 00168 @throw thread_thread_error On any error. 00169 */ 00170 virtual void setScheduling( const LOW_thread_Factory::schedPolicy_t inPolicy, const LOW_thread_Factory::schedPrio_t inPrio); 00171 00172 00173 /** Get the scheduling policy. 00174 00175 @throw thread_thread_error On any error. 00176 */ 00177 virtual LOW_thread_Factory::schedPolicy_t getSchedPolicy() const; 00178 00179 00180 /** Get the scheduling parameter. 00181 00182 @throw thread_thread_error On any error. 00183 */ 00184 virtual LOW_thread_Factory::schedPrio_t getSchedPrio() const; 00185 00186 00187 /** Get cancelling behaviour. 00188 00189 @throw thread_thread_error On any error. 00190 */ 00191 virtual LOW_thread_Factory::cancelAttrib_t getCancelAttrib() const; 00192 00193 //!@} 00194 00195 00196 /** 00197 @name Methods only callable by the own thread. 00198 */ 00199 //!@{ 00200 00201 /** Terminate the calling thread. 00202 00203 Method can only be called by the own thread. 00204 00205 @param inRetVal Return value to quit with. 00206 00207 @throw thread_thread_permission Calling thread is not the object's thread. 00208 @throw thread_thread_error On any other error. 00209 */ 00210 virtual void thread_exit( const int inRetVal); 00211 00212 00213 /** Test on pending cancellation. 00214 00215 Method does nothing except testing for pending cancellation and executing it. 00216 Its purpose is to introduce explicit checks for cancellation in long sequences 00217 of code that do not call cancellation point functions otherwise. 00218 00219 @throw thread_thread_permission Calling thread is not the object's thread. 00220 @throw thread_thread_error On any other error. 00221 00222 @see cancel() 00223 */ 00224 virtual void testCancel(); 00225 00226 00227 /** Set cancelling behaviour. 00228 00229 Changes the cancellation state for the calling thread -- that is, whether 00230 cancellation requests are ignored or not and when they are handled. 00231 00232 @param inCancelAttrib Cancelling handling attribute to set. 00233 00234 @throw thread_thread_permission Calling thread is not the object's thread. 00235 @throw thread_thread_error On any other error. 00236 */ 00237 virtual void setCancelAttrib( const LOW_thread_Factory::cancelAttrib_t inCancelAttrib); 00238 00239 //!@} 00240 00241 00242 00243 //======================================================================================= 00244 protected: 00245 00246 //===================================================================================== 00247 // 00248 // friend classes 00249 // 00250 00251 friend class LOW_thread_Factory; /**< To allow construction. */ 00252 00253 00254 //===================================================================================== 00255 // 00256 // constructors 00257 // 00258 00259 /** Constructor. 00260 Not publicly constructable. Use LOW_thread_Factory. 00261 @param inDetachState 00262 @param inSchedPolicy 00263 @param inSchedPrio 00264 */ 00265 LOW_thread_thread_POSIX( LOW_thread_runable *inRunable, 00266 const LOW_thread_Factory::detachState_t inDetachState = LOW_thread_Factory::detachState_default, 00267 const LOW_thread_Factory::schedPolicy_t inSchedPolicy = LOW_thread_Factory::schedPolicy_default, 00268 const LOW_thread_Factory::schedPrio_t inSchedPrio = schedPrio_default); 00269 00270 00271 /** Constructor. 00272 Not publicly constructable. Use LOW_thread_Factory. 00273 */ 00274 LOW_thread_thread_POSIX(); 00275 00276 00277 00278 //======================================================================================= 00279 private: 00280 00281 //===================================================================================== 00282 // 00283 // attributes 00284 // 00285 00286 LOW_thread_runable *myRunable; /**< Object providing the run() method. */ 00287 pthread_attr_t initAttr; /**< Initial thread attributes. */ 00288 pthread_t threadID; /**< ID of the thread. */ 00289 bool isStarted; /**< Indicates that thread was launched. */ 00290 bool isTerminated; /**< Indicated that thread has terminated. */ 00291 int returnValue; /**< Return Value of run() method. */ 00292 LOW_thread_Factory::cancelAttrib_t cancelAttrib; /**< Current cancel attribute. */ 00293 LOW_thread_mutex *createSyncMutex; /**< Mutex to synchronize with threadRunner on creation. */ 00294 00295 00296 //===================================================================================== 00297 // 00298 // static methods 00299 // 00300 00301 static void* threadRunner( void *inLaunchMe); 00302 }; 00303 00304 #endif