35 #include <sys/param.h>
36 #include <sys/sysctl.h>
41 #ifdef __DEVELOPMENT_ASSERTIONS__
51 GetSystemInfo(&sysinfo);
52 return sysinfo.dwNumberOfProcessors;
58 nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
59 sysctl(nm, 2, &count, &len, NULL, 0);
63 sysctl(nm, 2, &count, &len, NULL, 0);
64 if(count < 1) { count = 1; }
68 return sysconf(_SC_NPROCESSORS_ONLN);
74 #ifdef __DEVELOPMENT_ASSERTIONS__
82 th->
_state = THREAD_RUNNING;
88 th->
_state = THREAD_FINALIZING;
95 th->
_state = THREAD_FINALIZED;
99 th->
_state = THREAD_JOINABLE;
108 : _state(THREAD_CREATED, THREAD_FINALIZED), tid(), stack(size), priority(0), _detached(
false)
110 : _state(THREAD_CREATED, THREAD_FINALIZED), tid(0), stack(size), priority(0), _detached(
false)
113 pthread_attr_init(&attr);
118 #if defined(HAVE_PTHREAD_YIELD_NP)
120 #elif defined(HAVE_PTHREAD_YIELD)
129 #if defined(HAVE_PTHREAD_SETCONCURRENCY)
130 pthread_setconcurrency(level);
136 #if defined(HAVE_PTHREAD_DELAY)
138 ts.tv_sec = timeout / 1000l;
139 ts.tv_nsec = (timeout % 1000l) * 1000000l;
141 #elif defined(HAVE_PTHREAD_DELAY_NP)
143 ts.tv_sec = timeout / 1000l;
144 ts.tv_nsec = (timeout % 1000l) * 1000000l;
145 pthread_delay_np(&ts);
147 ::usleep(static_cast<useconds_t>(timeout) * 1000);
153 pthread_attr_destroy(&
attr);
159 throw ThreadException(0,
"invalid state for reset");
161 pthread_attr_destroy(&
attr);
171 pthread_attr_init(&
attr);
176 if (pthread_equal(
tid, pthread_t()))
return -1;
178 return pthread_kill(
tid, sig);
212 return (pthread_equal(t1, t2) != 0);
227 #ifdef __DEVELOPMENT_ASSERTIONS__
231 std::cerr <<
"FAILURE: Thread not finalized before! Current state:" <<
_state.
get() << std::endl;
247 if (ls != THREAD_CREATED)
return;
260 _state = THREAD_INITIALIZED;
264 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
268 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
272 #ifdef PTHREAD_STACK_MIN
274 if(stack && stack < PTHREAD_STACK_MIN)
277 stack = PTHREAD_STACK_MIN;
281 if (stack && stack < 2)
296 pthread_attr_setstacksize(&attr, stack);
305 _state = THREAD_FINALIZED;
306 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.");
308 _state = THREAD_FINALIZED;
311 _state = THREAD_FINALIZED;
312 throw ThreadException(ret,
"The caller does not have appropriate permission to set the required scheduling parameters or scheduling policy.");
340 pthread_t
self = pthread_self();
342 #ifdef __DEVELOPMENT_ASSERTIONS__
349 if ((ret = pthread_join(
tid, NULL)) == 0)
356 throw ThreadException(ret,
"Join on a thread failed");
377 if (ls != THREAD_CREATED)
return;
390 _state = THREAD_INITIALIZED;
394 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
398 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
402 #ifdef PTHREAD_STACK_MIN
404 if(stack && stack < PTHREAD_STACK_MIN)
407 stack = PTHREAD_STACK_MIN;
411 if (stack && stack < 2)
426 pthread_attr_setstacksize(&attr, stack);
439 _state = THREAD_FINALIZED;
440 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.");
442 _state = THREAD_FINALIZED;
445 _state = THREAD_FINALIZED;
446 throw ThreadException(ret,
"The caller does not have appropriate permission to set the required scheduling parameters or scheduling policy.");