Posix/UThreadC.h
Go to the documentation of this file.
1 // Written by Phillip Sitbon
3 // Copyright 2003
4 //
5 // Modified by Mathieu Labbe
6 //
7 // Posix/Thread.h
8 // - Posix thread
9 //
11 
12 #ifndef _U_Thread_Posix_
13 #define _U_Thread_Posix_
14 
16 #include "rtabmap/utilite/UMutex.h"
17 
18 #include <pthread.h>
19 
23 inline void uSleep(unsigned int ms)
24 {
25  struct timespec req;
26  struct timespec rem;
27  req.tv_sec = ms / 1000;
28  req.tv_nsec = (ms - req.tv_sec * 1000) * 1000 * 1000;
29  nanosleep (&req, &rem);
30 }
31 
35 inline void uSleepMicro(unsigned int us)
36 {
37  struct timespec req;
38  struct timespec rem;
39  req.tv_sec = us / 1000000;
40  req.tv_nsec = (us - req.tv_sec * 1000000) * 1000;
41  nanosleep (&req, &rem);
42 }
43 
47 inline void uSleepNano(unsigned int ns)
48 {
49  struct timespec req;
50  struct timespec rem;
51  req.tv_sec = ns / 1000000000;
52  req.tv_nsec = (ns - req.tv_sec * 1000000000);
53  nanosleep (&req, &rem);
54 }
55 
56 
57 #define InvalidHandle 0
58 #define THREAD_HANDLE pthread_t
59 
60 typedef void *( * pthread_fn )( void * );
61 
62 template
63 <
64  typename Thread_T
65 >
66 class UThreadC
67 {
68  private:
69  struct Instance;
70 
71  public:
72  typedef Thread_T & Thread_R;
73  typedef const Thread_T & Thread_C_R;
74 
76  typedef void ( *Handler)( Thread_R );
77 
78  virtual ~UThreadC() {}
79 
80  protected:
81  UThreadC() {}
82 
83  virtual void ThreadMain( Thread_R ) = 0;
84 
85  static void Exit()
86  { pthread_exit(0); }
87 #ifndef ANDROID
88  static void TestCancel()
89  { pthread_testcancel(); }
90 #endif
91 
92  static Handle Self()
93  { return (Handle)pthread_self(); }
94 
95  public:
96 
97  static int Create(
98  const Handler & Function,
99  Thread_C_R Param,
100  Handle * const & H = 0,
101  const bool & CreateDetached = false,
102  const unsigned int & StackSize = 0,
103  const bool & CancelEnable = false,
104  const bool & CancelAsync = false
105  )
106  {
107  M_Create().lock();
108  pthread_attr_t attr;
109  pthread_attr_init(&attr);
110 
111  if ( CreateDetached )
112  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
113 
114  if ( StackSize )
115  pthread_attr_setstacksize(&attr,StackSize);
116 
117  Instance I(Param,0,Function,CancelEnable,CancelAsync);
118 
120  int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
121 
122  pthread_attr_destroy(&attr);
123 
124  if(H) *H = h;
125  if ( !R ) S_Create().acquire();
126 
127  M_Create().unlock();
128  return R;
129  }
130 
131  int Create(
132  Thread_C_R Param,
133  Handle * const & H = 0,
134  const bool & CreateDetached = false,
135  const unsigned int & StackSize = 0,
136  const bool & CancelEnable = false,
137  const bool & CancelAsync = false
138  ) const
139  {
140  M_Create().lock();
141  pthread_attr_t attr;
142  pthread_attr_init(&attr);
143 
144  if ( CreateDetached )
145  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
146 
147  if ( StackSize )
148  pthread_attr_setstacksize(&attr,StackSize);
149 
150  Instance I(Param,const_cast<UThreadC *>(this),0,CancelEnable,CancelAsync);
151 
153  int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
154 
155  pthread_attr_destroy(&attr);
156 
157  if(H) *H = h;
158  if ( !R ) S_Create().acquire();
159 
160  M_Create().unlock();
161  return R;
162  }
163 
164  static int Join( Handle H )
165  { return pthread_join(H,0); }
166 
167 #ifndef ANDROID
168  static int Kill( Handle H )
169  { return pthread_cancel(H); }
170 #endif
171 
172  static int Detach( Handle H )
173  { return pthread_detach(H); }
174 
175  private:
176 
177  static const UMutex &M_Create() { static UMutex M; return M; }
178  static USemaphore &S_Create() { static USemaphore S; return S; }
179 
180  static void *ThreadMainHandler( Instance *Param )
181  {
182  Instance I(*Param);
183  Thread_T Data(I.Data);
184  S_Create().release();
185 
186 #ifndef ANDROID
187  if ( I.Flags & 1 /*CancelEnable*/ )
188  {
189  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
190 
191  if ( I.Flags & 2 /*CancelAsync*/ )
192  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
193  else
194  pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
195  }
196  else
197  {
198  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
199  }
200 #endif
201 
202  if ( I.Owner )
203  I.Owner->ThreadMain(Data);
204  else
205  I.pFN(Data);
206 
207  return 0;
208  }
209 
210  struct Instance
211  {
212  Instance( Thread_C_R P, UThreadC<Thread_T> *const &O, const UThreadC<Thread_T>::Handler &pH = 0, const bool &CE=false, const bool &CA=false )
213  : Data(P), Owner(O), pFN(pH), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; }
214 
218  unsigned char Flags;
219  };
220 };
221 
223 // Explicit specialization, no thread parameters
224 //
225 template<>
226 class UThreadC<void>
227 {
228  private:
229  struct Instance;
230 
231  public:
232  typedef THREAD_HANDLE Handle;
233  typedef void ( *Handler)();
234 
235  virtual ~UThreadC<void>() {}
236 
237  protected:
239 
240  virtual void ThreadMain() = 0;
241 
242  static void Exit()
243  { pthread_exit(0); }
244 
245 #ifndef ANDROID
246  static void TestCancel()
247  { pthread_testcancel(); }
248 #endif
249 
250  static Handle Self()
251  { return (Handle)pthread_self(); }
252 
253  public:
254 
255  static int Create(
256  const Handler & Function,
257  Handle * const & H = 0,
258  const bool & CreateDetached = false,
259  const unsigned int & StackSize = 0,
260  const bool & CancelEnable = false,
261  const bool & CancelAsync = false
262  )
263  {
264  M_Create().lock();
265  pthread_attr_t attr;
266  pthread_attr_init(&attr);
267 
268  if ( CreateDetached )
269  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
270 
271  if ( StackSize )
272  pthread_attr_setstacksize(&attr,StackSize);
273 
274  Instance I(0,Function,CancelEnable,CancelAsync);
275 
277  int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
278 
279  pthread_attr_destroy(&attr);
280 
281  if(H) *H = h;
282  if ( !R ) S_Create().acquire();
283 
284  M_Create().unlock();
285  return R;
286  }
287 
288  int Create(
289  Handle * const & H = 0,
290  const bool & CreateDetached = false,
291  const unsigned int & StackSize = 0,
292  const bool & CancelEnable = false,
293  const bool & CancelAsync = false
294  ) const
295  {
296  M_Create().lock();
297  pthread_attr_t attr;
298  pthread_attr_init(&attr);
299 
300  if ( CreateDetached )
301  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
302 
303  if ( StackSize )
304  pthread_attr_setstacksize(&attr,StackSize);
305 
306  Instance I(const_cast<UThreadC *>(this),0,CancelEnable,CancelAsync);
307 
309  int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
310 
311  pthread_attr_destroy(&attr);
312 
313  if(H) *H = h;
314  if ( !R ) S_Create().acquire();
315 
316  M_Create().unlock();
317  return R;
318  }
319 
320  int Create(
321  unsigned long & ThreadId,
322  Handle * const & H = 0,
323  const bool & CreateDetached = false,
324  const unsigned int & StackSize = 0,
325  const bool & CancelEnable = false,
326  const bool & CancelAsync = false
327  ) const
328  {
329  M_Create().lock();
330  pthread_attr_t attr;
331  pthread_attr_init(&attr);
332 
333  if ( CreateDetached )
334  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
335 
336  if ( StackSize )
337  pthread_attr_setstacksize(&attr,StackSize);
338 
339  Instance I(const_cast<UThreadC *>(this),0,CancelEnable,CancelAsync);
340 
341  *H = InvalidHandle;
342  int R = pthread_create((pthread_t *)&(*H),&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
343 
344  ThreadId = (unsigned long)*H;
345 
346  pthread_attr_destroy(&attr);
347 
348  if ( !R ) S_Create().acquire();
349 
350  M_Create().unlock();
351  return R;
352  }
353 
354  static int Join( Handle H )
355  { return pthread_join(H,0); }
356 
357 #ifndef ANDROID
358  static int Kill( Handle H )
359  { return pthread_cancel(H); }
360 #endif
361 
362  static int Detach( Handle H )
363  { return pthread_detach(H); }
364 
365  private:
366 
367  static const UMutex &M_Create() { static UMutex M; return M; }
368  static USemaphore &S_Create() { static USemaphore S; return S; }
369 
370  static void *ThreadMainHandler( Instance *Param )
371  {
372  Instance I(*Param);
373  S_Create().release();
374 
375 #ifndef ANDROID
376  if ( I.Flags & 1 /*CancelEnable*/ )
377  {
378  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
379 
380  if ( I.Flags & 2 /*CancelAsync*/ )
381  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
382  else
383  pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
384  }
385  else
386  {
387  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
388  }
389 #endif
390 
391  if ( I.Owner )
392  I.Owner->ThreadMain();
393  else
394  I.pFN();
395 
396  return 0;
397  }
398 
399  struct Instance
400  {
401  Instance( UThreadC<void> *const &O, const UThreadC<void>::Handler &pH = 0, const bool &CE=false, const bool &CA=false )
402  : pFN(pH), Owner(O), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; }
403 
406  unsigned char Flags;
407 
408  };
409 };
410 
411 #endif // !_U_Thread_Posix_
UThreadC< void >::Self
static Handle Self()
Definition: Posix/UThreadC.h:250
UMutex::lock
int lock() const
Definition: UMutex.h:87
H
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[ *:*] noreverse nowriteback set trange[ *:*] noreverse nowriteback set urange[ *:*] noreverse nowriteback set vrange[ *:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
USemaphore::acquire
bool acquire(int n=1, int ms=0)
Definition: USemaphore.h:101
UThreadC::M_Create
static const UMutex & M_Create()
Definition: Posix/UThreadC.h:177
UThreadC::Thread_R
Thread_T & Thread_R
Definition: Posix/UThreadC.h:69
UMutex::unlock
int unlock() const
Definition: UMutex.h:113
h
const double h
UThreadC< void >::M_Create
static const UMutex & M_Create()
Definition: Posix/UThreadC.h:367
UThreadC< void >::TestCancel
static void TestCancel()
Definition: Posix/UThreadC.h:246
UThreadC::S_Create
static USemaphore & S_Create()
Definition: Posix/UThreadC.h:178
UThreadC< void >::Exit
static void Exit()
Definition: Posix/UThreadC.h:242
UThreadC< void >::Instance::Instance
Instance(UThreadC< void > *const &O, const UThreadC< void >::Handler &pH=0, const bool &CE=false, const bool &CA=false)
Definition: Posix/UThreadC.h:401
UThreadC::Create
static int Create(const Handler &Function, Thread_C_R Param, Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false)
Definition: Posix/UThreadC.h:97
UThreadC::ThreadMainHandler
static void * ThreadMainHandler(Instance *Param)
Definition: Posix/UThreadC.h:180
UThreadC< void >::Handle
THREAD_HANDLE Handle
Definition: Posix/UThreadC.h:229
UThreadC::Kill
static int Kill(Handle H)
Definition: Posix/UThreadC.h:168
UThreadC::Instance::Instance
Instance(Thread_C_R P, UThreadC< Thread_T > *const &O, const UThreadC< Thread_T >::Handler &pH=0, const bool &CE=false, const bool &CA=false)
Definition: Posix/UThreadC.h:212
UThreadC::Instance::pFN
Handler pFN
Definition: Posix/UThreadC.h:217
UThreadC::Handle
THREAD_HANDLE Handle
Definition: Posix/UThreadC.h:75
pthread_fn
void *(* pthread_fn)(void *)
Definition: Posix/UThreadC.h:60
UMutex
Definition: UMutex.h:54
UThreadC::Thread_C_R
const typedef Thread_T & Thread_C_R
Definition: Posix/UThreadC.h:73
UThreadC::UThreadC
UThreadC()
Definition: Posix/UThreadC.h:81
uSleepMicro
void uSleepMicro(unsigned int us)
Definition: Posix/UThreadC.h:35
UThreadC< void >::Create
int Create(Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false) const
Definition: Posix/UThreadC.h:288
UThreadC< void >::Create
int Create(unsigned long &ThreadId, Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false) const
Definition: Posix/UThreadC.h:320
UThreadC::ThreadMain
virtual void ThreadMain(Thread_R)=0
UMutex.h
UThreadC::Instance::Flags
unsigned char Flags
Definition: Posix/UThreadC.h:218
UThreadC::Instance::Data
Thread_C_R Data
Definition: Posix/UThreadC.h:215
UThreadC::Instance::Owner
UThreadC< Thread_T > * Owner
Definition: Posix/UThreadC.h:216
UThreadC< void >::Instance::pFN
UThreadC< void >::Handler pFN
Definition: Posix/UThreadC.h:404
InvalidHandle
#define InvalidHandle
Definition: Posix/UThreadC.h:57
UThreadC::~UThreadC
virtual ~UThreadC()
Definition: Posix/UThreadC.h:78
UThreadC< void >::Kill
static int Kill(Handle H)
Definition: Posix/UThreadC.h:358
UThreadC< void >
Definition: Posix/UThreadC.h:226
UThreadC::Handler
void(* Handler)(Thread_R)
Definition: Posix/UThreadC.h:76
UThreadC
Definition: Posix/UThreadC.h:66
O
Key O(std::uint64_t j)
UThreadC< void >::Instance::Flags
unsigned char Flags
Definition: Posix/UThreadC.h:406
USemaphore::release
void release(int n=1)
Definition: USemaphore.h:168
UThreadC::Detach
static int Detach(Handle H)
Definition: Posix/UThreadC.h:172
UThreadC::Self
static Handle Self()
Definition: Posix/UThreadC.h:92
UThreadC< void >::Create
static int Create(const Handler &Function, Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false)
Definition: Posix/UThreadC.h:255
UThreadC< void >::Join
static int Join(Handle H)
Definition: Posix/UThreadC.h:354
PointMatcherSupport::Parametrizable
uSleepNano
void uSleepNano(unsigned int ns)
Definition: Posix/UThreadC.h:47
USemaphore
Definition: USemaphore.h:54
THREAD_HANDLE
#define THREAD_HANDLE
Definition: Posix/UThreadC.h:58
USemaphore.h
UThreadC::Instance
Definition: Posix/UThreadC.h:210
UThreadC::Exit
static void Exit()
Definition: Posix/UThreadC.h:85
uSleep
void uSleep(unsigned int ms)
Definition: Posix/UThreadC.h:23
UThreadC< void >::S_Create
static USemaphore & S_Create()
Definition: Posix/UThreadC.h:368
UThreadC< void >::Detach
static int Detach(Handle H)
Definition: Posix/UThreadC.h:362
Eigen.Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >
I
I
NULL
#define NULL
UThreadC::Create
int Create(Thread_C_R Param, Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false) const
Definition: Posix/UThreadC.h:131
UThreadC< void >::Instance::Owner
UThreadC< void > * Owner
Definition: Posix/UThreadC.h:405
UThreadC< void >::ThreadMainHandler
static void * ThreadMainHandler(Instance *Param)
Definition: Posix/UThreadC.h:370
UThreadC::TestCancel
static void TestCancel()
Definition: Posix/UThreadC.h:88
UThreadC::Join
static int Join(Handle H)
Definition: Posix/UThreadC.h:164
S
S
R
R


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:23