13 #include <sys/resource.h> 14 #include <sys/syscall.h> 15 #include <sys/prctl.h> 28 if (pthread_tryjoin_np(thread, &exitValue) != 0)
36 if (!pthread_kill(thread, 0))
44 mpThreadContext(pThreadContext),
50 mThreadStarted(
false),
54 int rc = pthread_mutex_init(&mStartMutex, 0);
60 rc = pthread_cond_init(&mStartCond, 0);
66 rc = pthread_mutex_init(&mExitMutex, 0);
72 rc = pthread_cond_init(&mExitCond, 0);
124 rc |= pthread_attr_init(&attr);
125 rc |= pthread_attr_setstacksize(&attr,
STACK_SIZE);
126 rc |= pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
168 "AJAThread(%p)::Start pthread_cond_wait returned error %d",
179 "AJAThread(%p)::Start error %d unlocking start mutex",
184 #if 0 // This should be set within the running thread itself if a dynamic change 221 if (timeout == 0xffffffff)
223 ts.tv_sec += 60 * 60 * 24 * 365;
229 uint32_t nsec = ((timeout % 1000) * 1000000);
241 ts.tv_sec += (timeout / 1000) + (nsec / 1000000000);
242 ts.tv_nsec = (nsec % 1000000000);
260 "AJAThread(%p)::Stop pthread_cond_timedwait returned error %d",
272 "AJAThread(%p)::Stop error %d unlocking timeout mutex",
292 "AJAThread(%p)::Stop error %d unlocking exit mutex",
302 rc = pthread_join(
mThread, &exitValue);
309 "AJAThread(%p)::Stop error %d from pthread_join",
330 int rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,
NULL);
337 "AJAThread(%p)::Kill error %d from pthread_setcancelstate",
340 rc = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,
NULL);
347 "AJAThread(%p)::Kill error %d from pthread_setcanceltype",
359 "AJAThread(%p)::Kill error %d from pthread_cancel",
393 if( pthread_equal(
mThread, pthread_self()) )
422 bool bRTPriority =
false;
425 switch (threadPriority)
454 struct sched_param newParam;
455 newParam.sched_priority = (bRTPriority ? newPriority : 0);
456 int newPolicy = (bRTPriority ? SCHED_RR : SCHED_OTHER);
457 int rc = pthread_setschedparam(
mThread, newPolicy, &newParam);
465 int newNice = (bRTPriority ? 0 : newPriority);
466 rc = setpriority(PRIO_PROCESS,
mTid, newNice);
480 if (pThreadPriority ==
NULL)
508 for(
int i = 0; i < 30; i++)
515 struct sched_param newParam;
516 memset(&newParam, 0,
sizeof(newParam));
517 newParam.sched_priority = priority;
518 int rc = pthread_setschedparam(
mThread, pval, &newParam);
548 if (pThreadImpl ==
NULL)
556 pid_t myTid = syscall( SYS_gettid );
558 pThreadImpl->
mTid = myTid;
562 int rc = pthread_mutex_lock(&pThreadImpl->
mStartMutex);
571 rc = pthread_cond_signal(&pThreadImpl->
mStartCond);
577 rc = pthread_mutex_unlock(&pThreadImpl->
mStartMutex);
608 rc = pthread_mutex_lock(&pThreadImpl->
mExitMutex);
614 rc = pthread_cond_signal(&pThreadImpl->
mExitCond);
620 rc = pthread_mutex_unlock(&pThreadImpl->
mExitMutex);
631 int ret = prctl(PR_SET_NAME, (
unsigned long)name, 0, 0);
642 pid_t tid = syscall(SYS_gettid);
644 return uint64_t(tid);
#define AJA_SUCCESS(_status_)
Declares the AJADebug class.
pthread_cond_t mStartCond
#define AJA_ASSERT(_expression_)
static uint64_t GetThreadId()
AJAStatus Attach(AJAThreadFunction *pThreadFunction, void *pUserContext)
pthread_mutex_t mStartMutex
AJAThreadPriority mPriority
pthread_mutex_t mExitMutex
static const size_t STACK_SIZE
void AJAThreadFunction(AJAThread *pThread, void *pContext)
Declares the AJAThreadImpl class.
AJAThreadFunction * mThreadFunc
#define AJA_REPORT(_index_, _severity_, _format_,...)
AJAThreadImpl(AJAThread *pThreadContext)
AJAStatus GetPriority(AJAThreadPriority *pThreadPriority)
bool is_pthread_alive(pthread_t thread)
AJAThread * mpThreadContext
AJAStatus SetPriority(AJAThreadPriority threadPriority)
AJAStatus Kill(uint32_t exitCode)
static void * ThreadProcStatic(void *pThreadImplContext)
AJAStatus SetThreadName(const char *name)
AJAStatus SetRealTime(AJAThreadRealTimePolicy policy, int priority)
virtual AJAStatus ThreadRun()
AJAStatus Stop(uint32_t timeout=0xffffffff)