21 #if defined(YARP_HAS_ACE)
22 # include <ace/Thread.h>
29 #if defined(__linux__) // Use the POSIX syscalls for the gettid()
30 # include <sys/syscall.h>
46 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
64 yCDebug(THREADIMPL,
"Thread starting up");
66 bool success = thread->threadInit();
67 thread->notify(success);
68 thread->notifyOpened(success);
69 thread->synchroPost();
74 thread->id = std::this_thread::get_id();
75 #if defined(__linux__)
78 thread->tid =
static_cast<long int>(syscall(SYS_gettid));
80 thread->tid =
static_cast<long int>(std::hash<std::thread::id>()(thread->id));
83 thread->setPriority();
85 thread->threadRelease();
89 yCDebug(THREADIMPL,
"Thread shutting down");
91 thread->notify(
false);
92 thread->synchroPost();
100 yCDebug(THREADIMPL,
"Thread being deleted");
113 #if defined(__linux__)
116 return static_cast<long int>(syscall(SYS_gettid));
118 return static_cast<long int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
128 if (!initWasSuccessful) {
130 yCError(THREADIMPL,
"Tried to join a thread before starting it");
140 if (thread.joinable()) {
147 while (synchro.
check()) {
191 initWasSuccessful =
false;
194 int result = thread.joinable() ? 0 : 1;
200 yCDebug(THREADIMPL,
"Child thread initializing");
202 initWasSuccessful =
true;
205 yCDebug(THREADIMPL,
"Child thread initialized ok");
209 yCDebug(THREADIMPL,
"Child thread did not initialize ok");
214 yCError(THREADIMPL,
"A thread failed to start with error code: %d", result);
251 if (priority == -1) {
252 priority = defaultPriority;
253 policy = defaultPolicy;
255 defaultPriority = priority;
256 defaultPolicy = policy;
258 if (active && priority != -1) {
259 #if defined(YARP_HAS_ACE)
260 if (std::is_same<std::thread::native_handle_type, ACE_hthread_t>::value) {
261 return ACE_Thread::setprio(thread.native_handle(), priority, policy);
263 yCError(THREADIMPL,
"Cannot set priority without ACE");
264 #elif defined(__unix__)
265 if (std::is_same<std::thread::native_handle_type, pthread_t>::value) {
266 struct sched_param thread_param;
267 thread_param.sched_priority = priority;
268 int ret = pthread_setschedparam(thread.native_handle(), policy, &thread_param);
269 return (
ret != 0) ? -1 : 0;
271 yCError(THREADIMPL,
"Cannot set priority without ACE");
274 yCError(THREADIMPL,
"Cannot set priority without ACE");
282 int prio = defaultPriority;
284 #if defined(YARP_HAS_ACE)
285 if (std::is_same<std::thread::native_handle_type, ACE_hthread_t>::value) {
286 ACE_Thread::getprio(thread.native_handle(), prio);
288 yCError(THREADIMPL,
"Cannot get priority without ACE");
290 #elif defined(__unix__)
291 if (std::is_same<std::thread::native_handle_type, pthread_t>::value) {
292 struct sched_param thread_param;
294 if (pthread_getschedparam(thread.native_handle(), &policy, &thread_param) == 0) {
295 prio = thread_param.sched_priority;
297 yCError(THREADIMPL,
"Cannot get priority without ACE");
301 yCError(THREADIMPL,
"Cannot get priority without ACE");
309 int policy = defaultPolicy;
311 #if defined(YARP_HAS_ACE)
312 if (std::is_same<std::thread::native_handle_type, ACE_hthread_t>::value) {
314 ACE_Thread::getprio(thread.native_handle(), prio, policy);
316 yCError(THREADIMPL,
"Cannot get scheduling policy without ACE");
318 #elif defined(__unix__)
319 if (std::is_same<std::thread::native_handle_type, pthread_t>::value) {
320 struct sched_param thread_param;
321 if (pthread_getschedparam(thread.native_handle(), &policy, &thread_param) != 0) {
322 policy = defaultPolicy;
325 yCError(THREADIMPL,
"Cannot get scheduling policy without ACE");
328 yCError(THREADIMPL,
"Cannot get scheduling policy without ACE");