72 typedef Thread_T & Thread_R;
73 typedef const Thread_T & Thread_C_R;
75 typedef THREAD_HANDLE Handle;
76 typedef void ( *Handler)( Thread_R );
83 virtual void ThreadMain( Thread_R ) = 0;
88 static void TestCancel()
89 { pthread_testcancel(); }
93 {
return (Handle)pthread_self(); }
98 const Handler & Function,
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);
119 Handle h=InvalidHandle;
120 int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(
void *)&I);
122 pthread_attr_destroy(&attr);
125 if ( !R ) S_Create().
acquire();
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);
152 Handle h=InvalidHandle;
153 int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(
void *)&I);
155 pthread_attr_destroy(&attr);
158 if ( !R ) S_Create().
acquire();
164 static int Join( Handle H )
165 {
return pthread_join(H,0); }
168 static int Kill( Handle H )
169 {
return pthread_cancel(H); }
172 static int Detach( Handle H )
173 {
return pthread_detach(H); }
177 static const UMutex &M_Create() {
static UMutex M;
return M; }
180 static void *ThreadMainHandler( Instance *Param )
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);
213 : Data(P), Owner(O), pFN(pH), Flags(0) {
if ( CE ) Flags|=1;
if ( CA ) Flags|=2; }
232 typedef THREAD_HANDLE Handle;
233 typedef void ( *Handler)();
240 virtual void ThreadMain() = 0;
246 static void TestCancel()
247 { pthread_testcancel(); }
251 {
return (Handle)pthread_self(); }
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
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);
276 Handle h=InvalidHandle;
277 int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(
void *)&I);
279 pthread_attr_destroy(&attr);
282 if ( !R ) S_Create().
acquire();
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);
308 Handle h=InvalidHandle;
309 int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(
void *)&I);
311 pthread_attr_destroy(&attr);
314 if ( !R ) S_Create().
acquire();
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);
342 int R = pthread_create((pthread_t *)&(*H),&attr,(pthread_fn)ThreadMainHandler,(
void *)&I);
344 ThreadId = (
unsigned long)*H;
346 pthread_attr_destroy(&attr);
348 if ( !R ) S_Create().
acquire();
354 static int Join( Handle H )
355 {
return pthread_join(H,0); }
358 static int Kill( Handle H )
359 {
return pthread_cancel(H); }
362 static int Detach( Handle H )
363 {
return pthread_detach(H); }
367 static const UMutex &M_Create() {
static UMutex M;
return M; }
370 static void *ThreadMainHandler( Instance *Param )
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();
402 : pFN(pH), Owner(O), Flags(0) {
if ( CE ) Flags|=1;
if ( CA ) Flags|=2; }