Mixing skin calls (cancellation points)
Is it in general valid to mix calls from different skins? In particular, can I expect the native skin calls to work as POSIX cancellation points? For example, if I have a POSIX thread with a pthread_t identifier which I know calls rt_cond_wait somewhere, will it work as a cancellation point when I call pthread_cancel?
The example program attached will not cancel if I have CANCEL_DEFERRED defined as 1 at the top, which sets the cancel type to deferred. This is on a PowerPC with Xenomai 2.5.3. I think this worked with Xenomai 2.4.10.
--------
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <native/task.h>
#include <native/cond.h>
#define CANCEL_DEFERRED 1
RT_MUTEX mutex;RT_COND cond;pthread_t thread;
void *entry(void *cookie)
{
fprintf(stderr, "Starting thread\n");
#if CANCEL_DEFERRED
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
#else
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
#endif
rt_mutex_acquire(&mutex, TM_INFINITE); rt_cond_wait(&cond, &mutex, TM_INFINITE); rt_mutex_release(&mutex);
return NULL;
}
int main(int argc, char **argv)
{
int ret;
pthread_attr_t attr;
if((ret = rt_mutex_create(&mutex, NULL)))
{
errno = -ret;
perror("rt_mutex_create");
exit(EXIT_FAILURE);
}
if((ret = rt_cond_create(&cond, NULL)))
{
errno = -ret;
perror("rt_cond_create");
exit(EXIT_FAILURE);
}
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if(pthread_create(&thread, &attr, entry, NULL))
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Sleeping 5 seconds\n");
sleep(5);
fprintf(stderr, "Woke up, cancelling thread\n");
pthread_cancel(thread);
pthread_join(thread, NULL); return 0;
}
Quoted 37 lines · Steve Deiters
Is it in general valid to mix calls from different skins? In particular, can I expect the native skin calls to work as POSIX cancellation points? For example, if I have a POSIX thread with a
pthread_tidentifier which I know callsrt_cond_waitsomewhere, will it work as a cancellation point when I callpthread_cancel?The example program attached will not cancel if I have
CANCEL_DEFERREDdefined as 1 at the top, which sets the cancel type to deferred. This is on a PowerPC with Xenomai 2.5.3. I think this worked with Xenomai 2.4.10.--------
#include <
stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <native/task.h>
#include <native/cond.h>#define
CANCEL_DEFERRED1
RT_MUTEXmutex;RT_CONDcond;pthread_tthread;void *entry(void *cookie)
{ fprintf(stderr, "Starting thread\n");#if CANCEL_DEFERRED pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); #else pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
Quoted 8 lines
#endif
rt_mutex_acquire(&mutex, TM_INFINITE); rt_cond_wait(&cond, &mutex, TM_INFINITE); rt_mutex_release(&mutex);return NULL; }
This program would also deadlock with plain POSIX: you are missing a cleanup handler which releases the mutex.
Signature · 1 line
Gilles.
Quoted 53 lines
-----Original Message-----
From: Gilles Chanteperdrix [mailto:[email protected]
Sent: Tuesday, July 06, 2010 1:42 PM
To: Steve Deiters
Cc: [email protected]
Subject: Re: [Xenomai-help] Mixing skin calls (cancellation points)Quoted 4 lines · Steve Deiters
Is it in general valid to mix calls from different skins? In particular, can I expect the native skin calls to work as POSIX cancellation points? For example, if I have a POSIX thread with a
pthread_tidentifier which I know callsrt_cond_waitsomewhere, will
Quoted 3 lines
it work as a cancellation point when I call
pthread_cancel?The example program attached will not cancel if I have
CANCEL_DEFERREDQuoted 1 line
defined as 1 at the top, which sets the cancel type to
deferred. This
Quoted 1 line
is on a PowerPC with Xenomai 2.5.3. I think this worked
with Xenomai
Quoted 32 lines
2.4.10.
--------
#include <
stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <native/task.h>
#include <native/cond.h>#define
CANCEL_DEFERRED1
RT_MUTEXmutex;RT_CONDcond;pthread_tthread;void *entry(void *cookie)
{ fprintf(stderr, "Starting thread\n");#if CANCEL_DEFERRED pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); #else pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); #endif rt_mutex_acquire(&mutex, TM_INFINITE); rt_cond_wait(&cond, &mutex, TM_INFINITE); rt_mutex_release(&mutex);
Quoted 9 lines
Quoted 2 lines
return NULL; }This program would also deadlock with plain POSIX: you are missing a cleanup handler which releases the mutex.
Signature · 1 line
Gilles.
My situation here is that I have some library routines, which were written using the native skin. It was done like this so users of the library were not forced to link against the POSIX skin. Not to say that they would use the libc POSIX implementation, just that they may want to use only the native skin.
Is there a deadlock issue here if I have the cancellation as deferred? Won't the rt_cond_wait already release the mutex. This is similar to pthread_cond_wait which is a cancellation point in POSIX. I realize the potential for deadlock if it asynchronous which is why I want the cancellation as deferred.
If I ever do need to do something like this, can I use the rt_task_add_hook looking at T_HOOK_DELETE in place of a POSIX cleanup handler?
This still doesn't say whether or not I can rely on the rt_cond_wait as a cancellation point. If not I may have only two options: rewrite the library using the POSIX skin and force the users to use the POSIX skin, or force the user to use the native skin or avoid using pthread_cancel. Neither one is an appealing option.
Thanks.
Quoted 68 lines · Steve Deiters
Quoted 49 lines
-----Original Message-----
From: Gilles Chanteperdrix [mailto:[email protected]]
Sent: Tuesday, July 06, 2010 1:42 PM
To: Steve Deiters
Cc: [email protected]
Subject: Re: [Xenomai-help] Mixing skin calls (cancellation points)Quoted 4 lines · Steve Deiters
Is it in general valid to mix calls from different skins? In particular, can I expect the native skin calls to work as POSIX cancellation points? For example, if I have a POSIX thread with a
pthread_tidentifier which I know callsrt_cond_waitsomewhere, will
Quoted 3 lines
it work as a cancellation point when I call
pthread_cancel?The example program attached will not cancel if I have
CANCEL_DEFERREDQuoted 1 line
defined as 1 at the top, which sets the cancel type to
deferred. This
Quoted 1 line
is on a PowerPC with Xenomai 2.5.3. I think this worked
with Xenomai
Quoted 28 lines
2.4.10.
--------
#include <
stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <native/task.h>
#include <native/cond.h>#define
CANCEL_DEFERRED1
RT_MUTEXmutex;RT_CONDcond;pthread_tthread;void *entry(void *cookie)
{ fprintf(stderr, "Starting thread\n");#if
CANCEL_DEFERRED
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); #elsepthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); #endifQuoted 12 lines
Quoted 6 lines
rt_mutex_acquire(&mutex, TM_INFINITE); rt_cond_wait(&cond, &mutex, TM_INFINITE); rt_mutex_release(&mutex);return NULL; }This program would also deadlock with plain POSIX: you are missing a cleanup handler which releases the mutex.
Signature · 1 line
Gilles.My situation here is that I have some library routines, which were written using the native skin. It was done like this so users of the library were not forced to link against the POSIX skin. Not to say that they would use the libc POSIX implementation, just that they may want to use only the native skin.
Quoted 5 lines
Is there a deadlock issue here if I have the cancellation as deferred? Won't the
rt_cond_waitalready release the mutex. This is similar topthread_cond_waitwhich is a cancellation point in POSIX. I realize the potential for deadlock if it asynchronous which is why I want the cancellation as deferred.
pthread_cond_wait, when cancelled, re-acquires the mutex, so, if you want to release it, you have to pthread_cleanup_push a handler which releases it. This is standard practice with the pthread API. rt_cond_wait should behave the same way. Anyway, I do not think this is your problem since no other thread has the mutex in your case.
Quoted 4 lines
If I ever do need to do something like this, can I use the
rt_task_add_hooklooking atT_HOOK_DELETEin place of a POSIX cleanup handler?
Basically, hook do not really work in user-space. So, you have to use pthread_cleanup_push. pthread_cleanup_push is a function of the glibc, not of Xenomai posix skin. So, you do not need linking with Xenomai posix skin to use it.
Quoted 6 lines
This still doesn't say whether or not I can rely on the
rt_cond_waitas a cancellation point. If not I may have only two options: rewrite the library using the POSIX skin and force the users to use the POSIX skin, or force the user to use the native skin or avoid usingpthread_cancel. Neither one is an appealing option.
It should be a cancellation point, but my point was that you may had the impression that it was not because the thread was in fact blocked in the mutex re-acquisition. Anyway, it is probably not your issue, so, we are going to check your test case, and tell you more later.
Quoted 4 lines
Thanks.
Signature · 1 line
Gilles.
Quoted 10 lines · Steve Deiters
Is it in general valid to mix calls from different skins? In particular, can I expect the native skin calls to work as POSIX cancellation points? For example, if I have a POSIX thread with a
pthread_tidentifier which I know callsrt_cond_waitsomewhere, will it work as a cancellation point when I callpthread_cancel?The example program attached will not cancel if I have
CANCEL_DEFERREDdefined as 1 at the top, which sets the cancel type to deferred. This is on a PowerPC with Xenomai 2.5.3. I think this worked with Xenomai 2.4.10.
Hi,
I had a look at the code, and chances are, that in fact it never worked. The native skin threads are created with asynchronous type, and the rest
of the skin assumes that it is always the case. To kill a native skin
thread, you are supposed to use rt_task_delete instead of pthread_cancel, and if you did not change the thread cancellation type, it will work. If you are interested in waiting for the thread deletion, you can create the thread joinable with T_JOINABLE, then wait for it with rt_task_join.
We can, of course, modify the code to allow changing the native thread cancellation type. But the question is: what is the use-case?
Signature · 1 line
Gilles.
Quoted 36 lines
-----Original Message-----
From: Gilles Chanteperdrix [mailto:[email protected]
Sent: Wednesday, July 07, 2010 7:50 AM
To: Steve Deiters
Cc: [email protected]
Subject: Re: [Xenomai-help] Mixing skin calls (cancellation points)Quoted 4 lines · Steve Deiters
Is it in general valid to mix calls from different skins? In particular, can I expect the native skin calls to work as POSIX cancellation points? For example, if I have a POSIX thread with a
pthread_tidentifier which I know callsrt_cond_waitsomewhere, will
Quoted 3 lines
it work as a cancellation point when I call
pthread_cancel?The example program attached will not cancel if I have
CANCEL_DEFERREDQuoted 1 line
defined as 1 at the top, which sets the cancel type to
deferred. This
Quoted 1 line
is on a PowerPC with Xenomai 2.5.3. I think this worked
with Xenomai
Quoted 1 line
2.4.10.
Hi,
I had a look at the code, and chances are, that in fact it never worked.
The native skin threads are created with asynchronous type, and the rest of the skin assumes that it is always the case. To kill a native skin thread, you are supposed to usert_task_deleteinstead ofpthread_cancel, and if you did not change the thread cancellation type, it will work. If you are interested in waiting for the thread deletion, you can create the thread joinable withT_JOINABLE, then wait for it withrt_task_join.
Quoted 7 lines
We can, of course, modify the code to allow changing the native thread cancellation type. But the question is: what is the use-case?
Signature · 1 line
Gilles.
I was not creating a native thread though. I was creating a POSIX thread, which used some native skin calls. I do not have a RT_TASK in this case to even use with rt_task_delete, unless there is something to convert from a pthread_t to a RT_TASK I'm not aware of. In any case I'm wanting to cancel a POSIX thread, not a native one.
My usage case is basically as I described previously. I have some code sectioned off in a static library. This code uses only native calls for stuff like mutexes and condition variable using the assumption that the end application using the library need not link against the POSIX skin. In one of the applications it was using a POSIX skin, with POSIX threads, and was cancelling a thread that was blocked with rt_cond_wait.
Just out of curiosity, is there anything analogous to pthread cleanup handlers in the native skin? You mentioned earlier that you cannot use the rt_add_hook in userspace. So is there anyway then to protect against the situation described earlier, where you delete a thread waiting on a condition variable. Will the thread reacquire the mutex before deletion as in the POSIX case and does the rt_task_delete make sure not to delete the thread while it is holding the mutex in this case?
Quoted 44 lines · Steve Deiters
Quoted 35 lines
-----Original Message-----
From: Gilles Chanteperdrix [mailto:[email protected]]
Sent: Wednesday, July 07, 2010 7:50 AM
To: Steve Deiters
Cc: [email protected]
Subject: Re: [Xenomai-help] Mixing skin calls (cancellation points)Quoted 4 lines · Steve Deiters
Is it in general valid to mix calls from different skins? In particular, can I expect the native skin calls to work as POSIX cancellation points? For example, if I have a POSIX thread with a
pthread_tidentifier which I know callsrt_cond_waitsomewhere, will
Quoted 3 lines
it work as a cancellation point when I call
pthread_cancel?The example program attached will not cancel if I have
CANCEL_DEFERREDQuoted 1 line
defined as 1 at the top, which sets the cancel type to
deferred. This
Quoted 1 line
is on a PowerPC with Xenomai 2.5.3. I think this worked
with Xenomai
Quoted 1 line
2.4.10.
Hi,
I had a look at the code, and chances are, that in fact it never worked.
The native skin threads are created with asynchronous type, and the rest of the skin assumes that it is always the case. To kill a native skin thread, you are supposed to usert_task_deleteinstead ofpthread_cancel, and if you did not change the thread cancellation type, it will work. If you are interested in waiting for the thread deletion, you can create the thread joinable withT_JOINABLE, then wait for it withrt_task_join.Quoted 7 lines
We can, of course, modify the code to allow changing the native thread cancellation type. But the question is: what is the use-case?
Signature · 1 line
Gilles.
Quoted 5 lines
I was not creating a native thread though. I was creating a POSIX thread, which used some native skin calls. I do not have a
RT_TASKin this case to even use withrt_task_delete, unless there is something to convert from apthread_tto aRT_TASKI'm not aware of. In any case I'm wanting to cancel a POSIX thread, not a native one.
The point is that rt_cond_wait assumes that the caller is a native skin thread, so that it is running with the asynchronous type.
Quoted 8 lines
My usage case is basically as I described previously. I have some code sectioned off in a static library. This code uses only native calls for stuff like mutexes and condition variable using the assumption that the end application using the library need not link against the POSIX skin. In one of the applications it was using a POSIX skin, with POSIX threads, and was cancelling a thread that was blocked with
rt_cond_wait.
Quoted 8 lines
Just out of curiosity, is there anything analogous to pthread cleanup handlers in the native skin? You mentioned earlier that you cannot use the
rt_add_hookin userspace. So is there anyway then to protect against the situation described earlier, where you delete a thread waiting on a condition variable. Will the thread reacquire the mutex before deletion as in the POSIX case and does thert_task_deletemake sure not to delete the thread while it is holding the mutex in this case?
I already answered that: yes, the thread will re-acquire the mutex. The pthread_cleanup_push/pthread_cleanup_pop services can be called from a native skin thread, they are part of the glibc, not of Xenomai posix skin, so you do not need to link against the Xenomai posix skin library.
Anyway, the proper way to fix this, which we use in the POSIX skin, is to set the cancellation type to asynchronous just before calling rt_cond_wait, saving the previoys cancellation type, then restore the cancellation type after rt_cond_wait.
Signature · 1 line
Gilles.
Quoted 10 lines
I already answered that: yes, the thread will re-acquire the mutex. The pthread_cleanup_push/pthread_cleanup_pop services can be called from a native skin thread, they are part of the glibc, not of Xenomai posix skin, so you do not need to link against the Xenomai posix skin library.
Anyway, the proper way to fix this, which we use in the POSIX skin, is to set the cancellation type to asynchronous just before calling
rt_cond_wait, saving the previoys cancellation type, then restore the cancellation type afterrt_cond_wait.
This is probably the best solution for me. Can I also use pthread_setcanceltype without linking to the POSIX skin or is there some method in the native skin to change the cancellation type? Can I assume anything not wrapped in the posix.wrappers is provided by glibc?
Thanks.
Quoted 11 lines · Steve Deiters
Quoted 10 lines
I already answered that: yes, the thread will re-acquire the mutex. The pthread_cleanup_push/pthread_cleanup_pop services can be called from a native skin thread, they are part of the glibc, not of Xenomai posix skin, so you do not need to link against the Xenomai posix skin library.
Anyway, the proper way to fix this, which we use in the POSIX skin, is to set the cancellation type to asynchronous just before calling
rt_cond_wait, saving the previoys cancellation type, then restore the cancellation type afterrt_cond_wait.
Quoted 4 lines
This is probably the best solution for me. Can I also use
pthread_setcanceltypewithout linking to the POSIX skin or is there some method in the native skin to change the cancellation type? Can I assume anything not wrapped in the posix.wrappers is provided by glibc?
Yes, anything not wrapped in posix.wrappers is provided by glibc. It is safe to call pthread_setcanceltype without linke to the POSIX skin library.
Signature · 1 line
Gilles.
Quoted 11 lines · Steve Deiters
Quoted 10 lines
I already answered that: yes, the thread will re-acquire the mutex. The pthread_cleanup_push/pthread_cleanup_pop services can be called from a native skin thread, they are part of the glibc, not of Xenomai posix skin, so you do not need to link against the Xenomai posix skin library.
Anyway, the proper way to fix this, which we use in the POSIX skin, is to set the cancellation type to asynchronous just before calling
rt_cond_wait, saving the previoys cancellation type, then restore the cancellation type afterrt_cond_wait.
Quoted 4 lines
This is probably the best solution for me. Can I also use
pthread_setcanceltypewithout linking to the POSIX skin or is there some method in the native skin to change the cancellation type? Can I assume anything not wrapped in the posix.wrappers is provided by glibc?
Ok. We will also fix this on Xenomai side.
Signature · 1 line
Gilles.
Quoted 11 lines · Gilles Chanteperdrix
Quoted 10 lines · Steve Deiters
Quoted 10 lines
I already answered that: yes, the thread will re-acquire the mutex. The pthread_cleanup_push/pthread_cleanup_pop services can be called from a native skin thread, they are part of the glibc, not of Xenomai posix skin, so you do not need to link against the Xenomai posix skin library.
Anyway, the proper way to fix this, which we use in the POSIX skin, is to set the cancellation type to asynchronous just before calling
rt_cond_wait, saving the previoys cancellation type, then restore the cancellation type afterrt_cond_wait.
Quoted 7 lines
Quoted 4 lines
This is probably the best solution for me. Can I also use
pthread_setcanceltypewithout linking to the POSIX skin or is there some method in the native skin to change the cancellation type? Can I assume anything not wrapped in the posix.wrappers is provided by glibc?Ok. We will also fix this on Xenomai side.
Should be fixed with commit 7a39eff4ed5f9e4aa8acb111328b1343511e41ae
http://git.xenomai.org/?p=xenomai-2.5.git;a=commit;h=7a39eff4ed5f9e4aa8acb111328b1343511e41ae
Signature · 1 line
Gilles.