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