00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef MRPT_SYSTEM_THREADS_H
00029 #define MRPT_SYSTEM_THREADS_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032
00033 namespace mrpt
00034 {
00035 namespace system
00036 {
00037
00038
00039
00040
00041
00042
00043
00044 struct TThreadHandle
00045 {
00046 #ifdef MRPT_OS_WINDOWS
00047 TThreadHandle() :
00048 hThread(NULL),
00049 idThread(0)
00050 {
00051 }
00052
00053
00054
00055
00056 void clear()
00057 {
00058 idThread = 0;
00059 hThread = NULL;
00060 }
00061 void *hThread;
00062 # if defined(HAVE_OPENTHREAD) // defined(_MSC_VER) && (_MSC_VER>=1400)
00063 uintptr_t idThread;
00064 # else
00065 unsigned long idThread;
00066 # endif
00067 #endif
00068 #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE)
00069 TThreadHandle() : idThread(0)
00070 {
00071 }
00072 unsigned long idThread;
00073
00074
00075
00076
00077 void clear()
00078 {
00079 idThread = 0;
00080 }
00081 #endif
00082
00083 bool isClear() const { return idThread==0; }
00084 };
00085
00086
00087
00088
00089 enum TProcessPriority {
00090 ppIdle = 0,
00091 ppNormal,
00092 ppHigh,
00093 ppVeryHigh
00094 };
00095
00096
00097
00098
00099 enum TThreadPriority {
00100 tpLowests =-15,
00101 tpLower = -2,
00102 tpLow = -1,
00103 tpNormal = 0,
00104 tpHigh = 1,
00105 tpHigher = 2,
00106 tpHighest = 15
00107 };
00108
00109
00110 namespace detail {
00111 TThreadHandle BASE_IMPEXP createThreadImpl(void (*func)(void *),void *param);
00112 template<typename T> class ThreadCreateFunctor {
00113 public:
00114 void (*func)(T);
00115 T obj;
00116 inline ThreadCreateFunctor(void (*f)(T),T o):func(f),obj(o) {}
00117 inline static void createThreadAux(void *obj) {
00118 ThreadCreateFunctor<T> *auxStruct=static_cast<ThreadCreateFunctor<T> *>(obj);
00119 auxStruct->func(auxStruct->obj);
00120 delete auxStruct;
00121 }
00122 inline static TThreadHandle createThread(void (*f)(T),T param) {
00123 ThreadCreateFunctor *tcs=new ThreadCreateFunctor(f,param);
00124 return createThreadImpl(&createThreadAux,static_cast<void *>(tcs));
00125 }
00126 };
00127
00128 template<> class ThreadCreateFunctor<void *> {
00129 public:
00130 inline static TThreadHandle createThread(void (*f)(void *),void *param) {
00131 return createThreadImpl(f,param);
00132 }
00133 };
00134
00135 class ThreadCreateFunctorNoParams {
00136 public:
00137 void (*func)(void);
00138 ThreadCreateFunctorNoParams( void (*f)(void) ) : func(f) { }
00139
00140 inline static void createThreadAux(void *f) {
00141 ThreadCreateFunctorNoParams *d=static_cast<ThreadCreateFunctorNoParams*>(f);
00142 d->func();
00143 delete d;
00144 }
00145 inline static TThreadHandle createThread( void (*f)(void) ) {
00146 ThreadCreateFunctorNoParams *dat = new ThreadCreateFunctorNoParams(f);
00147 return createThreadImpl(&createThreadAux, static_cast<void*>(dat) );
00148 }
00149 };
00150
00151 template <class CLASS,class PARAM>
00152 class ThreadCreateObjectFunctor {
00153 public:
00154 typedef void (CLASS::*objectfunctor_t)(PARAM);
00155 CLASS *obj;
00156 objectfunctor_t func;
00157 PARAM p;
00158 inline ThreadCreateObjectFunctor(CLASS *o,objectfunctor_t f, PARAM param):obj(o),func(f),p(param) {}
00159 inline static void createThreadAux(void *p) {
00160 ThreadCreateObjectFunctor<CLASS,PARAM> *auxStruct=static_cast<ThreadCreateObjectFunctor<CLASS,PARAM>*>(p);
00161 objectfunctor_t f = auxStruct->func;
00162 (auxStruct->obj->*f)(auxStruct->p);
00163 delete auxStruct;
00164 }
00165 inline static TThreadHandle createThread(CLASS *o,objectfunctor_t f, PARAM param) {
00166 ThreadCreateObjectFunctor *tcs=new ThreadCreateObjectFunctor(o,f,param);
00167 return createThreadImpl(&createThreadAux,static_cast<void *>(tcs));
00168 }
00169 };
00170
00171 template <class CLASS>
00172 class ThreadCreateObjectFunctorNoParams {
00173 public:
00174 typedef void (CLASS::*objectfunctor_t)(void);
00175 CLASS *obj;
00176 objectfunctor_t func;
00177 inline ThreadCreateObjectFunctorNoParams(CLASS *o,objectfunctor_t f):obj(o),func(f) {}
00178 inline static void createThreadAux(void *p) {
00179 ThreadCreateObjectFunctorNoParams<CLASS> *auxStruct=static_cast<ThreadCreateObjectFunctorNoParams<CLASS>*>(p);
00180 objectfunctor_t f = auxStruct->func;
00181 (auxStruct->obj->*f)();
00182 delete auxStruct;
00183 }
00184 inline static TThreadHandle createThread(CLASS *o,objectfunctor_t f) {
00185 ThreadCreateObjectFunctorNoParams *tcs=new ThreadCreateObjectFunctorNoParams(o,f);
00186 return createThreadImpl(&createThreadAux,static_cast<void *>(tcs));
00187 }
00188 };
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 template<typename T> inline TThreadHandle createThread(void (*func)(T),T param) {
00201 return detail::ThreadCreateFunctor<T>::createThread(func,param);
00202 }
00203
00204 template<typename T> inline TThreadHandle createThreadRef(void (*func)(T&),T& param) {
00205 return detail::ThreadCreateFunctor<T&>::createThread(func,param);
00206 }
00207
00208 inline TThreadHandle createThread(void (*func)(void)) {
00209 return detail::ThreadCreateFunctorNoParams::createThread(func);
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 template <typename CLASS,typename PARAM>
00235 inline TThreadHandle createThreadFromObjectMethod(CLASS *obj, void (CLASS::*func)(PARAM), PARAM param) {
00236 return detail::ThreadCreateObjectFunctor<CLASS,PARAM>::createThread(obj,func,param);
00237 }
00238
00239 template <typename CLASS,typename PARAM>
00240 inline TThreadHandle createThreadFromObjectMethodRef(CLASS *obj, void (CLASS::*func)(PARAM), PARAM ¶m) {
00241 return detail::ThreadCreateObjectFunctor<CLASS,PARAM&>::createThread(obj,func,param);
00242 }
00243
00244 template <typename CLASS>
00245 inline TThreadHandle createThreadFromObjectMethod(CLASS *obj, void (CLASS::*func)(void)) {
00246 return detail::ThreadCreateObjectFunctorNoParams<CLASS>::createThread(obj,func);
00247 }
00248
00249
00250
00251
00252
00253 void BASE_IMPEXP joinThread( const TThreadHandle &threadHandle );
00254
00255
00256
00257
00258 unsigned long BASE_IMPEXP getCurrentThreadId() MRPT_NO_THROWS;
00259
00260
00261
00262 TThreadHandle BASE_IMPEXP getCurrentThreadHandle() MRPT_NO_THROWS;
00263
00264
00265
00266
00267
00268 void BASE_IMPEXP exitThread() MRPT_NO_THROWS;
00269
00270
00271
00272
00273
00274
00275
00276
00277 void BASE_IMPEXP getCurrentThreadTimes(
00278 time_t &creationTime,
00279 time_t &exitTime,
00280 double &cpuTime );
00281
00282
00283
00284
00285 void BASE_IMPEXP changeThreadPriority( const TThreadHandle &threadHandle, TThreadPriority priority );
00286
00287
00288 void BASE_IMPEXP terminateThread( TThreadHandle &threadHandle) MRPT_NO_THROWS;
00289
00290
00291
00292
00293 void BASE_IMPEXP changeCurrentProcessPriority( TProcessPriority priority );
00294
00295
00296
00297 unsigned int BASE_IMPEXP getNumberOfProcessors();
00298
00299
00300
00301
00302 void BASE_IMPEXP sleep( int time_ms ) MRPT_NO_THROWS;
00303
00304
00305
00306
00307 bool BASE_IMPEXP launchProcess( const std::string & command );
00308
00309
00310
00311 }
00312
00313 }
00314
00315 #endif