18 #if defined(YARP_HAS_ACE)
19 # include <ace/Thread.h>
26 #if defined(__linux__)
27 # include <sys/syscall.h>
43 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
61 yCDebug(THREADIMPL,
"Thread starting up");
63 bool success = thread->threadInit();
64 thread->notify(success);
65 thread->notifyOpened(success);
66 thread->synchroPost();
71 thread->id = std::this_thread::get_id();
72 #if defined(__linux__)
75 thread->tid =
static_cast<long int>(syscall(SYS_gettid));
77 thread->tid =
static_cast<long int>(std::hash<std::thread::id>()(thread->id));
80 thread->setPriority();
82 thread->threadRelease();
86 yCDebug(THREADIMPL,
"Thread shutting down");
88 thread->notify(
false);
89 thread->synchroPost();
97 yCDebug(THREADIMPL,
"Thread being deleted");
110 #if defined(__linux__)
113 return static_cast<long int>(syscall(SYS_gettid));
115 return static_cast<long int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
125 if (!initWasSuccessful) {
127 yCError(THREADIMPL,
"Tried to join a thread before starting it");
137 if (thread.joinable()) {
144 while (synchro.
check()) {
188 initWasSuccessful =
false;
191 int result = thread.joinable() ? 0 : 1;
197 yCDebug(THREADIMPL,
"Child thread initializing");
199 initWasSuccessful =
true;
202 yCDebug(THREADIMPL,
"Child thread initialized ok");
206 yCDebug(THREADIMPL,
"Child thread did not initialize ok");
211 yCError(THREADIMPL,
"A thread failed to start with error code: %d", result);
248 if (priority == -1) {
249 priority = defaultPriority;
250 policy = defaultPolicy;
252 defaultPriority = priority;
253 defaultPolicy = policy;
255 if (active && priority != -1) {
256 #if defined(YARP_HAS_ACE)
257 if (std::is_same<std::thread::native_handle_type, ACE_hthread_t>::value) {
258 return ACE_Thread::setprio(thread.native_handle(), priority, policy);
260 yCError(THREADIMPL,
"Cannot set priority without ACE");
261 #elif defined(__unix__)
262 if (std::is_same<std::thread::native_handle_type, pthread_t>::value) {
263 struct sched_param thread_param;
264 thread_param.sched_priority = priority;
265 int ret = pthread_setschedparam(thread.native_handle(), policy, &thread_param);
266 return (
ret != 0) ? -1 : 0;
268 yCError(THREADIMPL,
"Cannot set priority without ACE");
271 yCError(THREADIMPL,
"Cannot set priority without ACE");
279 int prio = defaultPriority;
281 #if defined(YARP_HAS_ACE)
282 if (std::is_same<std::thread::native_handle_type, ACE_hthread_t>::value) {
283 ACE_Thread::getprio(thread.native_handle(), prio);
285 yCError(THREADIMPL,
"Cannot get priority without ACE");
287 #elif defined(__unix__)
288 if (std::is_same<std::thread::native_handle_type, pthread_t>::value) {
289 struct sched_param thread_param;
291 if (pthread_getschedparam(thread.native_handle(), &policy, &thread_param) == 0) {
292 prio = thread_param.sched_priority;
294 yCError(THREADIMPL,
"Cannot get priority without ACE");
298 yCError(THREADIMPL,
"Cannot get priority without ACE");
306 int policy = defaultPolicy;
308 #if defined(YARP_HAS_ACE)
309 if (std::is_same<std::thread::native_handle_type, ACE_hthread_t>::value) {
311 ACE_Thread::getprio(thread.native_handle(), prio, policy);
313 yCError(THREADIMPL,
"Cannot get scheduling policy without ACE");
315 #elif defined(__unix__)
316 if (std::is_same<std::thread::native_handle_type, pthread_t>::value) {
317 struct sched_param thread_param;
318 if (pthread_getschedparam(thread.native_handle(), &policy, &thread_param) != 0) {
319 policy = defaultPolicy;
322 yCError(THREADIMPL,
"Cannot get scheduling policy without ACE");
325 yCError(THREADIMPL,
"Cannot get scheduling policy without ACE");
static std::atomic< int > threadCount
void theExecutiveBranch(void *args)
bool waitWithTimeout(double timeoutInSeconds)
Try to decrement the counter, even if we must wait - but don't wait forever.
void wait()
Decrement the counter, even if we must wait to do that.
bool check()
Decrement the counter, unless that would require waiting.
void post()
Increment the counter.
An abstraction for a thread of execution.
virtual bool threadInit()
virtual void threadRelease()
int join(double seconds=-1)
virtual void afterStart(bool success)
virtual void beforeStart()
static long int getKeyOfCaller()
int setPriority(int priority=-1, int policy=-1)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
void yield()
The calling thread releases its remaining quantum upon calling this function.
The components from which ports and connections are built.