12 #ifndef _U_Thread_Posix_ 13 #define _U_Thread_Posix_ 27 req.tv_sec = ms / 1000;
28 req.tv_nsec = (ms - req.tv_sec * 1000) * 1000 * 1000;
29 nanosleep (&req, &rem);
39 req.tv_sec = us / 1000000;
40 req.tv_nsec = (us - req.tv_sec * 1000000) * 1000;
41 nanosleep (&req, &rem);
51 req.tv_sec = ns / 1000000000;
52 req.tv_nsec = (ns - req.tv_sec * 1000000000);
53 nanosleep (&req, &rem);
57 #define InvalidHandle 0 58 #define THREAD_HANDLE pthread_t 60 typedef void *( * pthread_fn )(
void * );
89 { pthread_testcancel(); }
93 {
return (Handle)pthread_self(); }
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 109 pthread_attr_init(&attr);
111 if ( CreateDetached )
112 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
115 pthread_attr_setstacksize(&attr,StackSize);
117 Instance I(Param,0,Function,CancelEnable,CancelAsync);
122 pthread_attr_destroy(&attr);
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 142 pthread_attr_init(&attr);
144 if ( CreateDetached )
145 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
148 pthread_attr_setstacksize(&attr,StackSize);
150 Instance I(Param,const_cast<UThreadC *>(
this),0,CancelEnable,CancelAsync);
155 pthread_attr_destroy(&attr);
165 {
return pthread_join(H,0); }
169 {
return pthread_cancel(H); }
173 {
return pthread_detach(H); }
183 Thread_T Data(I.
Data);
189 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,
NULL);
192 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,
NULL);
194 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,
NULL);
198 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
NULL);
203 I.
Owner->ThreadMain(Data);
247 { pthread_testcancel(); }
251 {
return (Handle)pthread_self(); }
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 266 pthread_attr_init(&attr);
268 if ( CreateDetached )
269 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
272 pthread_attr_setstacksize(&attr,StackSize);
274 Instance I(0,Function,CancelEnable,CancelAsync);
279 pthread_attr_destroy(&attr);
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 298 pthread_attr_init(&attr);
300 if ( CreateDetached )
301 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
304 pthread_attr_setstacksize(&attr,StackSize);
306 Instance I(const_cast<UThreadC *>(
this),0,CancelEnable,CancelAsync);
311 pthread_attr_destroy(&attr);
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 331 pthread_attr_init(&attr);
333 if ( CreateDetached )
334 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
337 pthread_attr_setstacksize(&attr,StackSize);
339 Instance I(const_cast<UThreadC *>(
this),0,CancelEnable,CancelAsync);
344 ThreadId = (
unsigned long)*H;
346 pthread_attr_destroy(&attr);
355 {
return pthread_join(H,0); }
359 {
return pthread_cancel(H); }
363 {
return pthread_detach(H); }
378 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,
NULL);
381 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,
NULL);
383 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,
NULL);
387 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
NULL);
392 I.Owner->ThreadMain();
411 #endif // !_U_Thread_Posix_
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
static int Detach(Handle H)
static int Kill(Handle H)
static USemaphore & S_Create()
static void * ThreadMainHandler(Instance *Param)
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)
static int Join(Handle H)
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)
static USemaphore & S_Create()
bool acquire(int n=1, int ms=0)
Instance(UThreadC< void > *const &O, const UThreadC< void >::Handler &pH=0, const bool &CE=false, const bool &CA=false)
static int Join(Handle H)
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)
virtual void ThreadMain(Thread_R)=0
static int Detach(Handle H)
const Thread_T & Thread_C_R
static void * ThreadMainHandler(Instance *Param)
static int Kill(Handle H)
void uSleepNano(unsigned int ns)
void *(* pthread_fn)(void *)
UThreadC< void >::Handler pFN
UThreadC< Thread_T > * Owner
static const UMutex & M_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
void uSleep(unsigned int ms)
static const UMutex & M_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
void(* Handler)(Thread_R)
void uSleepMicro(unsigned int us)