14 #include <sys/resource.h>
15 #include <sys/syscall.h>
17 #include <mach/mach_init.h>
18 #include <mach/thread_policy.h>
19 #include <mach/thread_act.h>
20 #include <mach/mach_time.h>
25 mpThreadContext(pThreadContext),
33 int rc = pthread_mutex_init(&mExitMutex, 0);
39 rc = pthread_cond_init(&mExitCond, 0);
79 rc |= pthread_attr_init(&attr);
80 rc |= pthread_attr_setstacksize(&attr,
STACK_SIZE);
81 rc |= pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
129 if (timeout == 0xffffffff)
131 ts.tv_sec += 60 * 60 * 24 * 365;
137 uint32_t nsec = ((timeout % 1000) * 1000000);
149 ts.tv_sec += (timeout / 1000) + (nsec / 1000000000);
150 ts.tv_nsec = (nsec % 1000000000);
169 "AJAThread(%p)::Stop pthread_cond_timedwait returned error %d",
181 "AJAThread(%p)::Stop error %d unlocking timeout mutex",
201 "AJAThread(%p)::Stop error %d unlocking exit mutex",
210 rc = pthread_join(
mThread, &exitValue);
217 "AJAThread(%p)::Stop error %d from pthread_join",
240 int rc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,
NULL);
247 "AJAThread(%p)::Kill error %d from pthread_setcancelstate",
250 rc = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,
NULL);
257 "AJAThread(%p)::Kill error %d from pthread_setcanceltype",
269 "AJAThread(%p)::Kill error %d from pthread_cancel",
304 if( pthread_equal(
mThread, pthread_self()) )
319 switch (threadPriority)
350 struct sched_param param;
351 int rc = pthread_getschedparam(
mThread, &policy, ¶m);
358 param.sched_priority = nice;
359 rc = pthread_setschedparam(
mThread, policy, ¶m);
368 mach_timebase_info_data_t info;
369 IOReturn ioReturn = mach_timebase_info(&info);
371 uint32_t period = 60000000;
372 uint32_t computation = 2000000;
374 thread_time_constraint_policy timeConstraint;
375 timeConstraint.period = uint32_t(
static_cast<long long>(period) * info.denom / info.numer);
376 timeConstraint.computation = uint32_t(
static_cast<long long>(computation) * info.denom / info.numer);
377 timeConstraint.constraint = timeConstraint.computation * 2;
378 timeConstraint.preemptible =
true;
380 mach_port_t machThread = pthread_mach_thread_np(
mThread);
381 while (MACH_PORT_NULL == machThread)
382 machThread = pthread_mach_thread_np(
mThread);
384 ioReturn = thread_policy_set(machThread, THREAD_TIME_CONSTRAINT_POLICY,
385 reinterpret_cast<thread_policy_t
>(&timeConstraint), THREAD_TIME_CONSTRAINT_POLICY_COUNT);
396 if (pThreadPriority ==
NULL)
433 if (pThreadImpl ==
NULL)
460 int rc = pthread_mutex_lock(&pThreadImpl->
mExitMutex);
466 rc = pthread_cond_signal(&pThreadImpl->
mExitCond);
472 rc = pthread_mutex_unlock(&pThreadImpl->
mExitMutex);
483 int ret = pthread_setname_np(name);
494 pthread_threadid_np(
NULL, &tid);