34 #include <sys/param.h>
35 #include <sys/sysctl.h>
40 #ifdef __DEVELOPMENT_ASSERTIONS__
50 GetSystemInfo(&sysinfo);
51 return sysinfo.dwNumberOfProcessors;
57 nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
58 sysctl(nm, 2, &count, &len, NULL, 0);
62 sysctl(nm, 2, &count, &len, NULL, 0);
63 if(count < 1) { count = 1; }
67 return sysconf(_SC_NPROCESSORS_ONLN);
73 #ifdef __DEVELOPMENT_ASSERTIONS__
81 th->
_state = THREAD_RUNNING;
87 th->
_state = THREAD_FINALIZING;
94 th->
_state = THREAD_FINALIZED;
98 th->
_state = THREAD_JOINABLE;
106 : _state(THREAD_CREATED, THREAD_FINALIZED), tid(0), stack(size), priority(0), _detached(false)
108 pthread_attr_init(&
attr);
113 #if defined(HAVE_PTHREAD_YIELD_NP)
115 #elif defined(HAVE_PTHREAD_YIELD)
124 #if defined(HAVE_PTHREAD_SETCONCURRENCY)
125 pthread_setconcurrency(level);
131 #if defined(HAVE_PTHREAD_DELAY)
133 ts.tv_sec = timeout / 1000l;
134 ts.tv_nsec = (timeout % 1000l) * 1000000l;
136 #elif defined(HAVE_PTHREAD_DELAY_NP)
138 ts.tv_sec = timeout / 1000l;
139 ts.tv_nsec = (timeout % 1000l) * 1000000l;
140 pthread_delay_np(&ts);
142 usleep(static_cast<useconds_t>(timeout) * 1000);
148 pthread_attr_destroy(&
attr);
154 throw ThreadException(0,
"invalid state for reset");
156 pthread_attr_destroy(&
attr);
162 pthread_attr_init(&
attr);
167 if (
tid == 0)
return -1;
168 return pthread_kill(
tid, sig);
202 return (pthread_equal(t1, t2) != 0);
217 #ifdef __DEVELOPMENT_ASSERTIONS__
221 std::cerr <<
"FAILURE: Thread not finalized before! Current state:" <<
_state.
get() << std::endl;
237 if (ls != THREAD_CREATED)
return;
250 _state = THREAD_INITIALIZED;
254 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
258 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
262 #ifdef PTHREAD_STACK_MIN
264 if(stack && stack < PTHREAD_STACK_MIN)
267 stack = PTHREAD_STACK_MIN;
271 if (stack && stack < 2)
286 pthread_attr_setstacksize(&attr, stack);
295 _state = THREAD_FINALIZED;
296 throw ThreadException(ret,
"The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process PTHREAD_THREADS_MAX would be exceeded.");
298 _state = THREAD_FINALIZED;
301 _state = THREAD_FINALIZED;
302 throw ThreadException(ret,
"The caller does not have appropriate permission to set the required scheduling parameters or scheduling policy.");
330 pthread_t
self = pthread_self();
332 #ifdef __DEVELOPMENT_ASSERTIONS__
339 if ((ret = pthread_join(
tid, NULL)) == 0)
346 throw ThreadException(ret,
"Join on a thread failed");
367 if (ls != THREAD_CREATED)
return;
380 _state = THREAD_INITIALIZED;
384 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
388 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
392 #ifdef PTHREAD_STACK_MIN
394 if(stack && stack < PTHREAD_STACK_MIN)
397 stack = PTHREAD_STACK_MIN;
401 if (stack && stack < 2)
416 pthread_attr_setstacksize(&attr, stack);
429 _state = THREAD_FINALIZED;
430 throw ThreadException(ret,
"The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process PTHREAD_THREADS_MAX would be exceeded.");
432 _state = THREAD_FINALIZED;
435 _state = THREAD_FINALIZED;
436 throw ThreadException(ret,
"The caller does not have appropriate permission to set the required scheduling parameters or scheduling policy.");