1 #ifndef CARMA_UTIL_START_PTHREAD_H
2 #define CARMA_UTIL_START_PTHREAD_H
9 #include <log4cpp/NDC.hh>
11 #include "carma/util/PthreadAttr.h"
12 #include "carma/util/PthreadMutex.h"
13 #include "carma/util/PthreadCond.h"
14 #include "carma/util/ScopedLock.h"
15 #include "carma/util/ScopedPthreadCancelDisable.h"
16 #include "carma/util/ThreadQuit.h"
59 template <
typename ArgType >
61 void (*ep)( ArgType & a ),
63 const ::std::string & initialLogNDC = ::std::string(),
64 const ::pthread_attr_t * attr = 0,
65 bool startCancelable =
false );
68 template <
typename ArgType >
70 void (*ep)( ArgType & a ),
72 const ::std::string & initialLogNDC,
73 const PthreadAttr & attr,
74 bool startCancelable =
false );
77 template <
typename ArgType >
79 void (*ep)(
const ArgType & a ),
81 const ::std::string & initialLogNDC = ::std::string(),
82 const ::pthread_attr_t * attr = 0,
83 bool startCancelable =
false );
86 template <
typename ArgType >
88 void (*ep)(
const ArgType & a ),
90 const ::std::string & initialLogNDC,
91 const PthreadAttr & attr,
92 bool startCancelable =
false );
95 template <
typename ArgType >
97 void (*ep)(
const ArgType * & a ),
98 ArgType *
const & arg,
99 const ::std::string & initialLogNDC = ::std::string(),
100 const ::pthread_attr_t * attr = 0,
101 bool startCancelable =
false );
104 template <
typename ArgType >
106 void (*ep)(
const ArgType * & a ),
107 ArgType *
const & arg,
108 const ::std::string & initialLogNDC,
109 const PthreadAttr & attr,
110 bool startCancelable =
false );
126 template <
typename ArgType >
128 void (*ep)( ArgType & a ),
130 const ::std::string & initialLogNDC = ::std::string(),
131 const ::pthread_attr_t * attr = 0,
132 bool startCancelable =
false );
135 template <
typename ArgType >
137 void (*ep)( ArgType & a ),
139 const ::std::string & initialLogNDC,
140 const PthreadAttr & attr,
141 bool startCancelable =
false );
144 template <
typename ArgType >
146 void (*ep)(
const ArgType & a ),
148 const ::std::string & initialLogNDC = ::std::string(),
149 const ::pthread_attr_t * attr = 0,
150 bool startCancelable =
false );
153 template <
typename ArgType >
155 void (*ep)(
const ArgType & a ),
157 const ::std::string & initialLogNDC,
158 const PthreadAttr & attr,
159 bool startCancelable =
false );
162 template <
typename ArgType >
164 void (*ep)(
const ArgType & a ),
166 const ::std::string & initialLogNDC = ::std::string(),
167 const ::pthread_attr_t * attr = 0,
168 bool startCancelable =
false );
171 template <
typename ArgType >
173 void (*ep)(
const ArgType & a ),
175 const ::std::string & initialLogNDC,
176 const PthreadAttr & attr,
177 bool startCancelable =
false );
188 template <
typename A,
typename AT,
typename AC >
193 template <
typename I >
198 template <
typename A,
typename AT,
typename AC >
201 const ::std::string & initialLogNDC,
202 const ::pthread_attr_t * attr,
203 bool startCancelable );
206 template <
typename A,
typename AT,
typename AC >
209 typedef void (*EpType)( A );
211 typedef AC ArgCopyType;
213 friend void * BoilerPlate< BoilerPlateInfo >(
void * bpInfoArg );
215 friend ::pthread_t StartPthreadCommon< A, AT, AC >(
218 const ::std::string & initialLogNDC,
219 const ::pthread_attr_t * attr,
220 bool startCancelable );
224 const ::std::string & initialLogNDC,
225 bool startCancelable );
229 bool clientCanContinue_;
236 const ::std::string & initialLogNDC_;
237 const bool startCancelable_;
246 template <
typename A,
typename AT,
typename AC >
251 const ::std::string & initialLogNDC,
252 const bool startCancelable ) :
253 clientCanContinue_( false ),
256 argTransfer_( argTransfer ),
257 initialLogNDC_( initialLogNDC ),
258 startCancelable_( startCancelable )
263 template <
typename I >
267 bool clientCanContinueSignalled =
false;
269 I * info =
static_cast< I *
>( bpInfoArg );
272 if ( info->initialLogNDC_.empty() == false )
273 log4cpp::NDC::push( info->initialLogNDC_ );
275 if ( info->startCancelable_ ==
false ) {
276 int oldState = PTHREAD_CANCEL_DISABLE;
278 if ( ::pthread_setcancelstate( PTHREAD_CANCEL_DISABLE,
280 throw std::runtime_error(
"::pthread_setcancelstate failed" );
283 const ScopedThreadQuitRegisterSelf quitReg;
285 typename I::EpType ep = info->ep_;
286 typename I::ArgCopyType argCopy = info->argTransfer_;
290 lock( info->clientCanContinueGuard_ );
292 info->clientCanContinue_ =
true;
293 info->epReached_ =
true;
300 info->clientCanContinueCond_.Signal();
303 clientCanContinueSignalled =
true;
309 if ( clientCanContinueSignalled ==
false ) {
312 lock( info->clientCanContinueGuard_ );
314 info->clientCanContinue_ =
true;
315 info->epReached_ =
false;
322 info->clientCanContinueCond_.Signal();
328 MarkCaughtExceptionOkayToDestructIfThreadQuitRequestedError();
335 template <
typename A,
typename AT,
typename AC >
340 const ::std::string & initialLogNDC,
341 const ::pthread_attr_t *
const attr,
342 const bool startCancelable )
344 const ScopedPthreadCancelDisable cancelDisable;
345 const ScopedThreadQuitDeferSelf quitDefer;
347 typedef BoilerPlateInfo< A, AT, AC > InfoType;
349 InfoType info( ep, argTransfer, initialLogNDC, startCancelable );
353 if ( ::pthread_create( &thread,
355 BoilerPlate< InfoType >,
357 throw std::runtime_error(
"::pthread_create failed" );
362 while ( info.clientCanContinue_ ==
false )
363 info.clientCanContinueCond_.Wait( info.clientCanContinueGuard_ );
365 if ( info.epReached_ ==
false )
366 throw std::runtime_error(
"thread died before entry point call" );
373 template <
typename ArgType >
376 void (*ep)( ArgType & a ),
378 const ::std::string & initialLogNDC,
379 const ::pthread_attr_t *
const attr,
380 const bool startCancelable )
395 template <
typename ArgType >
398 void (*ep)( ArgType & a ),
400 const ::std::string & initialLogNDC,
401 const PthreadAttr & attr,
402 const bool startCancelable )
412 &(attr.InternalPthreadAttr()),
417 template <
typename ArgType >
420 void (*ep)(
const ArgType & a ),
422 const ::std::string & initialLogNDC,
423 const ::pthread_attr_t *
const attr,
424 const bool startCancelable )
439 template <
typename ArgType >
442 void (*ep)(
const ArgType & a ),
444 const ::std::string & initialLogNDC,
445 const PthreadAttr & attr,
446 const bool startCancelable )
456 &(attr.InternalPthreadAttr()),
461 template <
typename ArgType >
464 void (*ep)(
const ArgType * & a ),
465 ArgType *
const & arg,
466 const ::std::string & initialLogNDC,
467 const ::pthread_attr_t *
const attr,
468 const bool startCancelable )
486 template <
typename ArgType >
489 void (*ep)(
const ArgType * & a ),
490 ArgType *
const & arg,
491 const ::std::string & initialLogNDC,
492 const PthreadAttr & attr,
493 const bool startCancelable )
506 &(attr.InternalPthreadAttr()),
511 template <
typename ArgType >
514 void (*ep)( ArgType & a ),
516 const ::std::string & initialLogNDC,
517 const ::pthread_attr_t *
const attr,
518 const bool startCancelable )
533 template <
typename ArgType >
536 void (*ep)( ArgType & a ),
538 const ::std::string & initialLogNDC,
539 const PthreadAttr & attr,
540 const bool startCancelable )
550 &(attr.InternalPthreadAttr()),
555 template <
typename ArgType >
558 void (*ep)(
const ArgType & a ),
560 const ::std::string & initialLogNDC,
561 const ::pthread_attr_t *
const attr,
562 const bool startCancelable )
577 template <
typename ArgType >
580 void (*ep)(
const ArgType & a ),
582 const ::std::string & initialLogNDC,
583 const PthreadAttr & attr,
584 const bool startCancelable )
594 &(attr.InternalPthreadAttr()),
599 template <
typename ArgType >
602 void (*ep)(
const ArgType & a ),
604 const ::std::string & initialLogNDC,
605 const ::pthread_attr_t *
const attr,
606 const bool startCancelable )
621 template <
typename ArgType >
624 void (*ep)(
const ArgType & a ),
626 const ::std::string & initialLogNDC,
627 const PthreadAttr & attr,
628 const bool startCancelable )
638 &(attr.InternalPthreadAttr()),
::pthread_t StartPthreadCommon(void(*ep)(A), AT argTransfer, const ::std::string &initialLogNDC, const ::pthread_attr_t *attr, bool startCancelable)
::pthread_t StartPthreadWithRef(void(*ep)(ArgType &a), ArgType &arg, const ::std::string &initialLogNDC=::std::string(), const ::pthread_attr_t *attr=0, bool startCancelable=false)
Starts up a new pthread.
A simple wrapper class that makes use of ::pthread_cond_t easier in a C++ world.
::pthread_t StartPthreadWithCopy(void(*ep)(ArgType &a), const ArgType &arg, const ::std::string &initialLogNDC=::std::string(), const ::pthread_attr_t *attr=0, bool startCancelable=false)
Starts up a new pthread.
A templated scope class (i.e.
void * BoilerPlate(void *bpInfoArg)
A simple wrapper class that makes use of ::pthread_mutex_t easier in a C++ world. ...