All of lore.kernel.org
 help / color / mirror / Atom feed
* [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
@ 2018-03-28  7:54 Pintu Kumar
  2018-03-28  8:12 ` Philippe Gerum
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Pintu Kumar @ 2018-03-28  7:54 UTC (permalink / raw)
  To: Xenomai@xenomai.org

Hi,

We are facing one issue on Xenomai-3 on x86_64 system.
Kernel: 4.9.51
ipipe version: 4
# /usr/xenomai/sbin/version
Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)

Its a very simple API level test.
We create a condition variable and wait for the condition inside a task.
Then, we immediately signal the condition from the main thread, and simply
wait for the test to finish.

But we observed that condition task is never getting released, and the
program hangs.

This is the output:
--------------------------
# ./condsignal
cond_task --> started
main --> cond signal success: 0
Waiting for task to finish....
^C


This is the code snippet that was used.
-----------------------------------------------------
# cat condsignal.c

/*
 * 1. Create an condition variable and wait inside a task.
 * 2. Delete the condition variable from main task.
 * 3. Observe that the condition wait is released and task ends normally.
 * 4. Finally main task should exit normally.
 *
 * */
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <alchemy/task.h>
#include <alchemy/mutex.h>
#include <alchemy/cond.h>
#include <alchemy/task.h>

static RT_COND cond;
static RT_MUTEX mutex;

static void cond_task(void *cookie)
{
        int err;

        printf("%s --> started \n", __func__);
        rt_mutex_acquire(&mutex, TM_INFINITE);
        err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
        rt_mutex_release(&mutex);
        if (err) {
                printf("rt_cond_wait failed, err = %d", err);
                return;
        }
        printf("%s --> cond wait done\n", __func__);
}

int main(int argc, char **argv)
{
        int err;
        RT_TASK task1;

        mlockall(MCL_CURRENT | MCL_FUTURE);

        err = rt_mutex_create(&mutex, NULL);
        err = rt_cond_create(&cond, "cond_test");
        err = rt_task_create(&task1, NULL, 0, 99, T_JOINABLE);
        err = rt_task_start(&task1, &cond_task, NULL);
        rt_mutex_acquire(&mutex, TM_INFINITE);
        err = rt_cond_signal(&cond);
        rt_mutex_release(&mutex);
        if (err) {
                printf("Error: rt_cond_signal failed, err = %d", err);
                exit(-1);
        }
        printf("%s --> cond signal success: %d\n", __func__, err);
        printf("Waiting for task to finish....\n");
        rt_task_join(&task1);

        return 0;
}
-----------------------------------------------------

Is there any fix available in Xenomai-3 for this?
Please let me know if there is something wrong here.


Thanks,
Pintu


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28  7:54 [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination Pintu Kumar
@ 2018-03-28  8:12 ` Philippe Gerum
  2018-03-28  9:32   ` Pintu Kumar
  2018-03-28  8:14 ` Julien Blanc
  2018-03-28 15:07 ` Philippe Gerum
  2 siblings, 1 reply; 15+ messages in thread
From: Philippe Gerum @ 2018-03-28  8:12 UTC (permalink / raw)
  To: Pintu Kumar, Xenomai@xenomai.org

On 03/28/2018 09:54 AM, Pintu Kumar wrote:
> Hi,
> 
> We are facing one issue on Xenomai-3 on x86_64 system.
> Kernel: 4.9.51
> ipipe version: 4
> # /usr/xenomai/sbin/version
> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
> 
> Its a very simple API level test.
> We create a condition variable and wait for the condition inside a task.
> Then, we immediately signal the condition from the main thread, and simply
> wait for the test to finish.
> 
> But we observed that condition task is never getting released, and the
> program hangs.
> 
> This is the output:
> --------------------------
> # ./condsignal
> cond_task --> started
> main --> cond signal success: 0
> Waiting for task to finish....
> ^C
> 
> 
> This is the code snippet that was used.
> -----------------------------------------------------
> # cat condsignal.c
> 
> /*
>  * 1. Create an condition variable and wait inside a task.
>  * 2. Delete the condition variable from main task.
>  * 3. Observe that the condition wait is released and task ends normally.
>  * 4. Finally main task should exit normally.
>  *
>  * */
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/mman.h>
> #include <alchemy/task.h>
> #include <alchemy/mutex.h>
> #include <alchemy/cond.h>
> #include <alchemy/task.h>
> 
> static RT_COND cond;
> static RT_MUTEX mutex;
> 
> static void cond_task(void *cookie)
> {
>         int err;
> 
>         printf("%s --> started \n", __func__);
>         rt_mutex_acquire(&mutex, TM_INFINITE);
>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>         rt_mutex_release(&mutex);
>         if (err) {
>                 printf("rt_cond_wait failed, err = %d", err);
>                 return;
>         }
>         printf("%s --> cond wait done\n", __func__);
> }
> 
> int main(int argc, char **argv)
> {
>         int err;
>         RT_TASK task1;
> 
>         mlockall(MCL_CURRENT | MCL_FUTURE);
> 
>         err = rt_mutex_create(&mutex, NULL);
>         err = rt_cond_create(&cond, "cond_test");
>         err = rt_task_create(&task1, NULL, 0, 99, T_JOINABLE);
>         err = rt_task_start(&task1, &cond_task, NULL);
>         rt_mutex_acquire(&mutex, TM_INFINITE);
>         err = rt_cond_signal(&cond);
>         rt_mutex_release(&mutex);
>         if (err) {
>                 printf("Error: rt_cond_signal failed, err = %d", err);
>                 exit(-1);
>         }
>         printf("%s --> cond signal success: %d\n", __func__, err);
>         printf("Waiting for task to finish....\n");
>         rt_task_join(&task1);
> 
>         return 0;
> }
> -----------------------------------------------------
> 
> Is there any fix available in Xenomai-3 for this?
> Please let me know if there is something wrong here.
> 
> 

You are not using condition variables the right way, those have no
semaphore semantics. Please read a POSIX documentation about them like
[1], the alchemy API does not differ in behavior.

[1] http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_cond_wait.html

-- 
Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28  7:54 [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination Pintu Kumar
  2018-03-28  8:12 ` Philippe Gerum
@ 2018-03-28  8:14 ` Julien Blanc
  2018-03-28 10:04   ` Pintu Kumar
  2018-03-28 15:07 ` Philippe Gerum
  2 siblings, 1 reply; 15+ messages in thread
From: Julien Blanc @ 2018-03-28  8:14 UTC (permalink / raw)
  To: xenomai

Le mercredi 28 mars 2018 à 13:24 +0530, Pintu Kumar a écrit :
> Hi,
> 
> We are facing one issue on Xenomai-3 on x86_64 system.
> Kernel: 4.9.51
> ipipe version: 4
> # /usr/xenomai/sbin/version
> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
> 
> Its a very simple API level test.
> We create a condition variable and wait for the condition inside a
> task.
> Then, we immediately signal the condition from the main thread, and
> simply
> wait for the test to finish.

Looks like you're signalling the condition variable before it is even
begin waited by the newly created thread, so i would say the behaviour
is normal.

If this is just for test purpose, you can for example add a sleep
inside your main thread so that you make sure that the child thread
reaches the rt_cond_wait call before the main thread executes
rt_cond_signal.

Regards,

Julien


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28  8:12 ` Philippe Gerum
@ 2018-03-28  9:32   ` Pintu Kumar
  2018-03-28  9:46     ` Philippe Gerum
  0 siblings, 1 reply; 15+ messages in thread
From: Pintu Kumar @ 2018-03-28  9:32 UTC (permalink / raw)
  To: Philippe Gerum; +Cc: Xenomai@xenomai.org

On Wed, Mar 28, 2018 at 1:42 PM, Philippe Gerum <rpm@xenomai.org> wrote:
> On 03/28/2018 09:54 AM, Pintu Kumar wrote:
>> Hi,
>>
>> We are facing one issue on Xenomai-3 on x86_64 system.
>> Kernel: 4.9.51
>> ipipe version: 4
>> # /usr/xenomai/sbin/version
>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>
>> Its a very simple API level test.
>> We create a condition variable and wait for the condition inside a task.
>> Then, we immediately signal the condition from the main thread, and simply
>> wait for the test to finish.
>>
>> But we observed that condition task is never getting released, and the
>> program hangs.
>>
>> This is the output:
>> --------------------------
>> # ./condsignal
>> cond_task --> started
>> main --> cond signal success: 0
>> Waiting for task to finish....
>> ^C
>>
>>
>> This is the code snippet that was used.
>> -----------------------------------------------------
>> # cat condsignal.c
>>
>> /*
>>  * 1. Create an condition variable and wait inside a task.
>>  * 2. Delete the condition variable from main task.
>>  * 3. Observe that the condition wait is released and task ends normally.
>>  * 4. Finally main task should exit normally.
>>  *
>>  * */
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <sys/mman.h>
>> #include <alchemy/task.h>
>> #include <alchemy/mutex.h>
>> #include <alchemy/cond.h>
>> #include <alchemy/task.h>
>>
>> static RT_COND cond;
>> static RT_MUTEX mutex;
>>
>> static void cond_task(void *cookie)
>> {
>>         int err;
>>
>>         printf("%s --> started \n", __func__);
>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>>         rt_mutex_release(&mutex);
>>         if (err) {
>>                 printf("rt_cond_wait failed, err = %d", err);
>>                 return;
>>         }
>>         printf("%s --> cond wait done\n", __func__);
>> }
>>
>> int main(int argc, char **argv)
>> {
>>         int err;
>>         RT_TASK task1;
>>
>>         mlockall(MCL_CURRENT | MCL_FUTURE);
>>
>>         err = rt_mutex_create(&mutex, NULL);
>>         err = rt_cond_create(&cond, "cond_test");
>>         err = rt_task_create(&task1, NULL, 0, 99, T_JOINABLE);
>>         err = rt_task_start(&task1, &cond_task, NULL);
>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>         err = rt_cond_signal(&cond);
>>         rt_mutex_release(&mutex);
>>         if (err) {
>>                 printf("Error: rt_cond_signal failed, err = %d", err);
>>                 exit(-1);
>>         }
>>         printf("%s --> cond signal success: %d\n", __func__, err);
>>         printf("Waiting for task to finish....\n");
>>         rt_task_join(&task1);
>>
>>         return 0;
>> }
>> -----------------------------------------------------
>>
>> Is there any fix available in Xenomai-3 for this?
>> Please let me know if there is something wrong here.
>>
>>
>
> You are not using condition variables the right way, those have no
> semaphore semantics. Please read a POSIX documentation about them like
> [1], the alchemy API does not differ in behavior.
>

Thank you for your reply.
Can you point out the mistake in this program?
I will try to develop the posix equivalent and check.
I even tried removing mutex from rt_cond_signal, but it did not help.

> [1] http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_cond_wait.html
>
> --
> Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28  9:32   ` Pintu Kumar
@ 2018-03-28  9:46     ` Philippe Gerum
  0 siblings, 0 replies; 15+ messages in thread
From: Philippe Gerum @ 2018-03-28  9:46 UTC (permalink / raw)
  To: Pintu Kumar; +Cc: Xenomai@xenomai.org

On 03/28/2018 11:32 AM, Pintu Kumar wrote:
> On Wed, Mar 28, 2018 at 1:42 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>> On 03/28/2018 09:54 AM, Pintu Kumar wrote:
>>> Hi,
>>>
>>> We are facing one issue on Xenomai-3 on x86_64 system.
>>> Kernel: 4.9.51
>>> ipipe version: 4
>>> # /usr/xenomai/sbin/version
>>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>>
>>> Its a very simple API level test.
>>> We create a condition variable and wait for the condition inside a task.
>>> Then, we immediately signal the condition from the main thread, and simply
>>> wait for the test to finish.
>>>
>>> But we observed that condition task is never getting released, and the
>>> program hangs.
>>>
>>> This is the output:
>>> --------------------------
>>> # ./condsignal
>>> cond_task --> started
>>> main --> cond signal success: 0
>>> Waiting for task to finish....
>>> ^C
>>>
>>>
>>> This is the code snippet that was used.
>>> -----------------------------------------------------
>>> # cat condsignal.c
>>>
>>> /*
>>>  * 1. Create an condition variable and wait inside a task.
>>>  * 2. Delete the condition variable from main task.
>>>  * 3. Observe that the condition wait is released and task ends normally.
>>>  * 4. Finally main task should exit normally.
>>>  *
>>>  * */
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>> #include <sys/mman.h>
>>> #include <alchemy/task.h>
>>> #include <alchemy/mutex.h>
>>> #include <alchemy/cond.h>
>>> #include <alchemy/task.h>
>>>
>>> static RT_COND cond;
>>> static RT_MUTEX mutex;
>>>
>>> static void cond_task(void *cookie)
>>> {
>>>         int err;
>>>
>>>         printf("%s --> started \n", __func__);
>>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>>>         rt_mutex_release(&mutex);
>>>         if (err) {
>>>                 printf("rt_cond_wait failed, err = %d", err);
>>>                 return;
>>>         }
>>>         printf("%s --> cond wait done\n", __func__);
>>> }
>>>
>>> int main(int argc, char **argv)
>>> {
>>>         int err;
>>>         RT_TASK task1;
>>>
>>>         mlockall(MCL_CURRENT | MCL_FUTURE);
>>>
>>>         err = rt_mutex_create(&mutex, NULL);
>>>         err = rt_cond_create(&cond, "cond_test");
>>>         err = rt_task_create(&task1, NULL, 0, 99, T_JOINABLE);
>>>         err = rt_task_start(&task1, &cond_task, NULL);
>>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>>         err = rt_cond_signal(&cond);
>>>         rt_mutex_release(&mutex);
>>>         if (err) {
>>>                 printf("Error: rt_cond_signal failed, err = %d", err);
>>>                 exit(-1);
>>>         }
>>>         printf("%s --> cond signal success: %d\n", __func__, err);
>>>         printf("Waiting for task to finish....\n");
>>>         rt_task_join(&task1);
>>>
>>>         return 0;
>>> }
>>> -----------------------------------------------------
>>>
>>> Is there any fix available in Xenomai-3 for this?
>>> Please let me know if there is something wrong here.
>>>
>>>
>>
>> You are not using condition variables the right way, those have no
>> semaphore semantics. Please read a POSIX documentation about them like
>> [1], the alchemy API does not differ in behavior.
>>
> 
> Thank you for your reply.
> Can you point out the mistake in this program?

No, you need to figure this out by yourself, reading a proper
documentation about using POSIX synchronization features. I know, I'm mean.

> I will try to develop the posix equivalent and check.
> I even tried removing mutex from rt_cond_signal, but it did not help.
> 

No, removing what makes condvars working properly is definitely not
going to help.

Please grab a POSIX documentation somewhere on the net, a couple of
illustrations and use cases, then get your feet wet with mutexes and
condition variables: those are the fundamental features POSIX provides
for thread synchronization. There is no escape.

-- 
Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28  8:14 ` Julien Blanc
@ 2018-03-28 10:04   ` Pintu Kumar
  2018-03-28 13:29     ` Philippe Gerum
  0 siblings, 1 reply; 15+ messages in thread
From: Pintu Kumar @ 2018-03-28 10:04 UTC (permalink / raw)
  To: Julien Blanc; +Cc: Xenomai@xenomai.org

On Wed, Mar 28, 2018 at 1:44 PM, Julien Blanc <julien.blanc@sprinte.eu> wrote:
> Le mercredi 28 mars 2018 à 13:24 +0530, Pintu Kumar a écrit :
>> Hi,
>>
>> We are facing one issue on Xenomai-3 on x86_64 system.
>> Kernel: 4.9.51
>> ipipe version: 4
>> # /usr/xenomai/sbin/version
>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>
>> Its a very simple API level test.
>> We create a condition variable and wait for the condition inside a
>> task.
>> Then, we immediately signal the condition from the main thread, and
>> simply
>> wait for the test to finish.
>
> Looks like you're signalling the condition variable before it is even
> begin waited by the newly created thread, so i would say the behaviour
> is normal.
>
> If this is just for test purpose, you can for example add a sleep
> inside your main thread so that you make sure that the child thread
> reaches the rt_cond_wait call before the main thread executes
> rt_cond_signal.
>

Oh yes, I already tried by adding 1 second sleep before signal, but still the
condition task hangs.

This test case works fine on Xenomai 2.6


> Regards,
>
> Julien
>
> _______________________________________________
> Xenomai mailing list
> Xenomai@xenomai.org
> https://xenomai.org/mailman/listinfo/xenomai


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28 10:04   ` Pintu Kumar
@ 2018-03-28 13:29     ` Philippe Gerum
  0 siblings, 0 replies; 15+ messages in thread
From: Philippe Gerum @ 2018-03-28 13:29 UTC (permalink / raw)
  To: Pintu Kumar, Julien Blanc; +Cc: Xenomai@xenomai.org

On 03/28/2018 12:04 PM, Pintu Kumar wrote:
> On Wed, Mar 28, 2018 at 1:44 PM, Julien Blanc <julien.blanc@sprinte.eu> wrote:
>> Le mercredi 28 mars 2018 à 13:24 +0530, Pintu Kumar a écrit :
>>> Hi,
>>>
>>> We are facing one issue on Xenomai-3 on x86_64 system.
>>> Kernel: 4.9.51
>>> ipipe version: 4
>>> # /usr/xenomai/sbin/version
>>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>>
>>> Its a very simple API level test.
>>> We create a condition variable and wait for the condition inside a
>>> task.
>>> Then, we immediately signal the condition from the main thread, and
>>> simply
>>> wait for the test to finish.
>>
>> Looks like you're signalling the condition variable before it is even
>> begin waited by the newly created thread, so i would say the behaviour
>> is normal.
>>
>> If this is just for test purpose, you can for example add a sleep
>> inside your main thread so that you make sure that the child thread
>> reaches the rt_cond_wait call before the main thread executes
>> rt_cond_signal.
>>
> 
> Oh yes, I already tried by adding 1 second sleep before signal, but still the
> condition task hangs.
> 
> This test case works fine on Xenomai 2.6
> 

If you believe there is a bug, can you try to debug this? Some help from
users would not harm. (make sure to build with --enable-debug=full if
you intend to do so).

Thanks,

-- 
Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28  7:54 [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination Pintu Kumar
  2018-03-28  8:12 ` Philippe Gerum
  2018-03-28  8:14 ` Julien Blanc
@ 2018-03-28 15:07 ` Philippe Gerum
  2018-03-28 17:20   ` Pintu Kumar
  2 siblings, 1 reply; 15+ messages in thread
From: Philippe Gerum @ 2018-03-28 15:07 UTC (permalink / raw)
  To: Pintu Kumar, Xenomai@xenomai.org

On 03/28/2018 09:54 AM, Pintu Kumar wrote:
> Hi,
> 
> We are facing one issue on Xenomai-3 on x86_64 system.
> Kernel: 4.9.51
> ipipe version: 4
> # /usr/xenomai/sbin/version
> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
> 
> Its a very simple API level test.
> We create a condition variable and wait for the condition inside a task.
> Then, we immediately signal the condition from the main thread, and simply
> wait for the test to finish.
> 
> But we observed that condition task is never getting released, and the
> program hangs.
> 
> This is the output:
> --------------------------
> # ./condsignal
> cond_task --> started
> main --> cond signal success: 0
> Waiting for task to finish....
> ^C
> 
> 
> This is the code snippet that was used.
> -----------------------------------------------------
> # cat condsignal.c
> 
> /*
>  * 1. Create an condition variable and wait inside a task.
>  * 2. Delete the condition variable from main task.
>  * 3. Observe that the condition wait is released and task ends normally.
>  * 4. Finally main task should exit normally.
>  *
>  * */
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/mman.h>
> #include <alchemy/task.h>
> #include <alchemy/mutex.h>
> #include <alchemy/cond.h>
> #include <alchemy/task.h>
> 
> static RT_COND cond;
> static RT_MUTEX mutex;
> 
> static void cond_task(void *cookie)
> {
>         int err;
> 
>         printf("%s --> started \n", __func__);
>         rt_mutex_acquire(&mutex, TM_INFINITE);
>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>         rt_mutex_release(&mutex);
>         if (err) {
>                 printf("rt_cond_wait failed, err = %d", err);
>                 return;
>         }
>         printf("%s --> cond wait done\n", __func__);
> }
> 
> int main(int argc, char **argv)
> {
>         int err;
>         RT_TASK task1;
> 
>         mlockall(MCL_CURRENT | MCL_FUTURE);

	The line above is redundant with libcobalt inits. But you need:

	  err = rt_task_shadow(NULL, "main", <prio>, 0);
	  ...
> 
>         err = rt_mutex_create(&mutex, NULL);
>         err = rt_cond_create(&cond, "cond_test");
>         err = rt_task_create(&task1, NULL, 0, 99, T_JOINABLE);
>         err = rt_task_start(&task1, &cond_task, NULL);
>         rt_mutex_acquire(&mutex, TM_INFINITE);
>         err = rt_cond_signal(&cond);
>         rt_mutex_release(&mutex);
>         if (err) {
>                 printf("Error: rt_cond_signal failed, err = %d", err);
>                 exit(-1);
>         }
>         printf("%s --> cond signal success: %d\n", __func__, err);
>         printf("Waiting for task to finish....\n");
>         rt_task_join(&task1);
> 
>         return 0;
> }

-- 
Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28 15:07 ` Philippe Gerum
@ 2018-03-28 17:20   ` Pintu Kumar
  2018-03-28 17:32     ` Philippe Gerum
  0 siblings, 1 reply; 15+ messages in thread
From: Pintu Kumar @ 2018-03-28 17:20 UTC (permalink / raw)
  To: Philippe Gerum; +Cc: Xenomai@xenomai.org

On Wed, Mar 28, 2018 at 8:37 PM, Philippe Gerum <rpm@xenomai.org> wrote:
> On 03/28/2018 09:54 AM, Pintu Kumar wrote:
>> Hi,
>>
>> We are facing one issue on Xenomai-3 on x86_64 system.
>> Kernel: 4.9.51
>> ipipe version: 4
>> # /usr/xenomai/sbin/version
>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>
>> Its a very simple API level test.
>> We create a condition variable and wait for the condition inside a task.
>> Then, we immediately signal the condition from the main thread, and simply
>> wait for the test to finish.
>>
>> But we observed that condition task is never getting released, and the
>> program hangs.
>>
>> This is the output:
>> --------------------------
>> # ./condsignal
>> cond_task --> started
>> main --> cond signal success: 0
>> Waiting for task to finish....
>> ^C
>>
>>
>> This is the code snippet that was used.
>> -----------------------------------------------------
>> # cat condsignal.c
>>
>> /*
>>  * 1. Create an condition variable and wait inside a task.
>>  * 2. Delete the condition variable from main task.
>>  * 3. Observe that the condition wait is released and task ends normally.
>>  * 4. Finally main task should exit normally.
>>  *
>>  * */
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <sys/mman.h>
>> #include <alchemy/task.h>
>> #include <alchemy/mutex.h>
>> #include <alchemy/cond.h>
>> #include <alchemy/task.h>
>>
>> static RT_COND cond;
>> static RT_MUTEX mutex;
>>
>> static void cond_task(void *cookie)
>> {
>>         int err;
>>
>>         printf("%s --> started \n", __func__);
>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>>         rt_mutex_release(&mutex);
>>         if (err) {
>>                 printf("rt_cond_wait failed, err = %d", err);
>>                 return;
>>         }
>>         printf("%s --> cond wait done\n", __func__);
>> }
>>
>> int main(int argc, char **argv)
>> {
>>         int err;
>>         RT_TASK task1;
>>
>>         mlockall(MCL_CURRENT | MCL_FUTURE);
>
>         The line above is redundant with libcobalt inits. But you need:
>
>           err = rt_task_shadow(NULL, "main", <prio>, 0);
>           ...

OK thank you so much for this. I will try it.
And, yes I am debugging this issue.
I found that rt_cond_wait_timed, did not return from here:

if (abs_timeout)
           ret = -__RT(pthread_cond_timedwait(&ccb->cond,
                                         &mcb->lock, abs_timeout));

How to debug from here. The next call is a normal pthread call.
And what does __RT represent here?
What does it translates to ?

If this does not work, then even normal pthread_cond_wait/signal
example also may not work.
So, I will first try to check the behaviour with normal pthread.


>>
>>         err = rt_mutex_create(&mutex, NULL);
>>         err = rt_cond_create(&cond, "cond_test");
>>         err = rt_task_create(&task1, NULL, 0, 99, T_JOINABLE);
>>         err = rt_task_start(&task1, &cond_task, NULL);
>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>         err = rt_cond_signal(&cond);
>>         rt_mutex_release(&mutex);
>>         if (err) {
>>                 printf("Error: rt_cond_signal failed, err = %d", err);
>>                 exit(-1);
>>         }
>>         printf("%s --> cond signal success: %d\n", __func__, err);
>>         printf("Waiting for task to finish....\n");
>>         rt_task_join(&task1);
>>
>>         return 0;
>> }
>
> --
> Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28 17:20   ` Pintu Kumar
@ 2018-03-28 17:32     ` Philippe Gerum
  2018-03-30  6:42       ` Pintu Kumar
  0 siblings, 1 reply; 15+ messages in thread
From: Philippe Gerum @ 2018-03-28 17:32 UTC (permalink / raw)
  To: Pintu Kumar; +Cc: Xenomai@xenomai.org

On 03/28/2018 07:20 PM, Pintu Kumar wrote:
> On Wed, Mar 28, 2018 at 8:37 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>> On 03/28/2018 09:54 AM, Pintu Kumar wrote:
>>> Hi,
>>>
>>> We are facing one issue on Xenomai-3 on x86_64 system.
>>> Kernel: 4.9.51
>>> ipipe version: 4
>>> # /usr/xenomai/sbin/version
>>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>>
>>> Its a very simple API level test.
>>> We create a condition variable and wait for the condition inside a task.
>>> Then, we immediately signal the condition from the main thread, and simply
>>> wait for the test to finish.
>>>
>>> But we observed that condition task is never getting released, and the
>>> program hangs.
>>>
>>> This is the output:
>>> --------------------------
>>> # ./condsignal
>>> cond_task --> started
>>> main --> cond signal success: 0
>>> Waiting for task to finish....
>>> ^C
>>>
>>>
>>> This is the code snippet that was used.
>>> -----------------------------------------------------
>>> # cat condsignal.c
>>>
>>> /*
>>>  * 1. Create an condition variable and wait inside a task.
>>>  * 2. Delete the condition variable from main task.
>>>  * 3. Observe that the condition wait is released and task ends normally.
>>>  * 4. Finally main task should exit normally.
>>>  *
>>>  * */
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>> #include <sys/mman.h>
>>> #include <alchemy/task.h>
>>> #include <alchemy/mutex.h>
>>> #include <alchemy/cond.h>
>>> #include <alchemy/task.h>
>>>
>>> static RT_COND cond;
>>> static RT_MUTEX mutex;
>>>
>>> static void cond_task(void *cookie)
>>> {
>>>         int err;
>>>
>>>         printf("%s --> started \n", __func__);
>>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>>>         rt_mutex_release(&mutex);
>>>         if (err) {
>>>                 printf("rt_cond_wait failed, err = %d", err);
>>>                 return;
>>>         }
>>>         printf("%s --> cond wait done\n", __func__);
>>> }
>>>
>>> int main(int argc, char **argv)
>>> {
>>>         int err;
>>>         RT_TASK task1;
>>>
>>>         mlockall(MCL_CURRENT | MCL_FUTURE);
>>
>>         The line above is redundant with libcobalt inits. But you need:
>>
>>           err = rt_task_shadow(NULL, "main", <prio>, 0);
>>           ...
> 
> OK thank you so much for this. I will try it.
> And, yes I am debugging this issue.
> I found that rt_cond_wait_timed, did not return from here:

Ok, thanks for looking at this. The issue is that the app is missing the
call to rt_task_shadow() in the main thread. So that thread receives
-EPERM from rt_mutex_acquire(), and the rest is a fallout of that
original error.

-- 
Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-28 17:32     ` Philippe Gerum
@ 2018-03-30  6:42       ` Pintu Kumar
  2018-03-30  6:53         ` Pintu Kumar
  0 siblings, 1 reply; 15+ messages in thread
From: Pintu Kumar @ 2018-03-30  6:42 UTC (permalink / raw)
  To: Philippe Gerum; +Cc: Xenomai@xenomai.org

On Wed, Mar 28, 2018 at 11:02 PM, Philippe Gerum <rpm@xenomai.org> wrote:
> On 03/28/2018 07:20 PM, Pintu Kumar wrote:
>> On Wed, Mar 28, 2018 at 8:37 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>>> On 03/28/2018 09:54 AM, Pintu Kumar wrote:
>>>> Hi,
>>>>
>>>> We are facing one issue on Xenomai-3 on x86_64 system.
>>>> Kernel: 4.9.51
>>>> ipipe version: 4
>>>> # /usr/xenomai/sbin/version
>>>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>>>
>>>> Its a very simple API level test.
>>>> We create a condition variable and wait for the condition inside a task.
>>>> Then, we immediately signal the condition from the main thread, and simply
>>>> wait for the test to finish.
>>>>
>>>> But we observed that condition task is never getting released, and the
>>>> program hangs.
>>>>
>>>> This is the output:
>>>> --------------------------
>>>> # ./condsignal
>>>> cond_task --> started
>>>> main --> cond signal success: 0
>>>> Waiting for task to finish....
>>>> ^C
>>>>
>>>>
>>>> This is the code snippet that was used.
>>>> -----------------------------------------------------
>>>> # cat condsignal.c
>>>>
>>>> /*
>>>>  * 1. Create an condition variable and wait inside a task.
>>>>  * 2. Delete the condition variable from main task.
>>>>  * 3. Observe that the condition wait is released and task ends normally.
>>>>  * 4. Finally main task should exit normally.
>>>>  *
>>>>  * */
>>>> #include <stdio.h>
>>>> #include <stdlib.h>
>>>> #include <sys/mman.h>
>>>> #include <alchemy/task.h>
>>>> #include <alchemy/mutex.h>
>>>> #include <alchemy/cond.h>
>>>> #include <alchemy/task.h>
>>>>
>>>> static RT_COND cond;
>>>> static RT_MUTEX mutex;
>>>>
>>>> static void cond_task(void *cookie)
>>>> {
>>>>         int err;
>>>>
>>>>         printf("%s --> started \n", __func__);
>>>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>>>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>>>>         rt_mutex_release(&mutex);
>>>>         if (err) {
>>>>                 printf("rt_cond_wait failed, err = %d", err);
>>>>                 return;
>>>>         }
>>>>         printf("%s --> cond wait done\n", __func__);
>>>> }
>>>>
>>>> int main(int argc, char **argv)
>>>> {
>>>>         int err;
>>>>         RT_TASK task1;
>>>>
>>>>         mlockall(MCL_CURRENT | MCL_FUTURE);
>>>
>>>         The line above is redundant with libcobalt inits. But you need:
>>>
>>>           err = rt_task_shadow(NULL, "main", <prio>, 0);
>>>           ...
>>
>> OK thank you so much for this. I will try it.
>> And, yes I am debugging this issue.
>> I found that rt_cond_wait_timed, did not return from here:
>
> Ok, thanks for looking at this. The issue is that the app is missing the
> call to rt_task_shadow() in the main thread. So that thread receives
> -EPERM from rt_mutex_acquire(), and the rest is a fallout of that
> original error.
>

Ok, we tried adding rt_task_shadow(NULL, "main", 99, 0), just before
rt_task_create(),
but it did not help.
Still the tasks hangs under rt_cond_wait().
So, we need to debug further.


Thanks,
Pintu


> --
> Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-30  6:42       ` Pintu Kumar
@ 2018-03-30  6:53         ` Pintu Kumar
  2018-03-30  8:04           ` Philippe Gerum
  0 siblings, 1 reply; 15+ messages in thread
From: Pintu Kumar @ 2018-03-30  6:53 UTC (permalink / raw)
  To: Philippe Gerum; +Cc: Xenomai@xenomai.org

On Fri, Mar 30, 2018 at 12:12 PM, Pintu Kumar <pintu.ping@gmail.com> wrote:
> On Wed, Mar 28, 2018 at 11:02 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>> On 03/28/2018 07:20 PM, Pintu Kumar wrote:
>>> On Wed, Mar 28, 2018 at 8:37 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>>>> On 03/28/2018 09:54 AM, Pintu Kumar wrote:
>>>>> Hi,
>>>>>
>>>>> We are facing one issue on Xenomai-3 on x86_64 system.
>>>>> Kernel: 4.9.51
>>>>> ipipe version: 4
>>>>> # /usr/xenomai/sbin/version
>>>>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>>>>
>>>>> Its a very simple API level test.
>>>>> We create a condition variable and wait for the condition inside a task.
>>>>> Then, we immediately signal the condition from the main thread, and simply
>>>>> wait for the test to finish.
>>>>>
>>>>> But we observed that condition task is never getting released, and the
>>>>> program hangs.
>>>>>
>>>>> This is the output:
>>>>> --------------------------
>>>>> # ./condsignal
>>>>> cond_task --> started
>>>>> main --> cond signal success: 0
>>>>> Waiting for task to finish....
>>>>> ^C
>>>>>
>>>>>
>>>>> This is the code snippet that was used.
>>>>> -----------------------------------------------------
>>>>> # cat condsignal.c
>>>>>
>>>>> /*
>>>>>  * 1. Create an condition variable and wait inside a task.
>>>>>  * 2. Delete the condition variable from main task.
>>>>>  * 3. Observe that the condition wait is released and task ends normally.
>>>>>  * 4. Finally main task should exit normally.
>>>>>  *
>>>>>  * */
>>>>> #include <stdio.h>
>>>>> #include <stdlib.h>
>>>>> #include <sys/mman.h>
>>>>> #include <alchemy/task.h>
>>>>> #include <alchemy/mutex.h>
>>>>> #include <alchemy/cond.h>
>>>>> #include <alchemy/task.h>
>>>>>
>>>>> static RT_COND cond;
>>>>> static RT_MUTEX mutex;
>>>>>
>>>>> static void cond_task(void *cookie)
>>>>> {
>>>>>         int err;
>>>>>
>>>>>         printf("%s --> started \n", __func__);
>>>>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>>>>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>>>>>         rt_mutex_release(&mutex);
>>>>>         if (err) {
>>>>>                 printf("rt_cond_wait failed, err = %d", err);
>>>>>                 return;
>>>>>         }
>>>>>         printf("%s --> cond wait done\n", __func__);
>>>>> }
>>>>>
>>>>> int main(int argc, char **argv)
>>>>> {
>>>>>         int err;
>>>>>         RT_TASK task1;
>>>>>
>>>>>         mlockall(MCL_CURRENT | MCL_FUTURE);
>>>>
>>>>         The line above is redundant with libcobalt inits. But you need:
>>>>
>>>>           err = rt_task_shadow(NULL, "main", <prio>, 0);
>>>>           ...
>>>
>>> OK thank you so much for this. I will try it.
>>> And, yes I am debugging this issue.
>>> I found that rt_cond_wait_timed, did not return from here:
>>
>> Ok, thanks for looking at this. The issue is that the app is missing the
>> call to rt_task_shadow() in the main thread. So that thread receives
>> -EPERM from rt_mutex_acquire(), and the rest is a fallout of that
>> original error.
>>
>
> Ok, we tried adding rt_task_shadow(NULL, "main", 99, 0), just before
> rt_task_create(),
> but it did not help.
> Still the tasks hangs under rt_cond_wait().
> So, we need to debug further.
>

OK. Good news :)
I changed as below and it worked.
rt_task_shadow(NULL, "main", 98, 0)

So, basically, I lowered the priority of main task and the cond_task
exited normally this time.
This indicates that cond_task should run first, then the main task.

But, now the question is:
when do we need rt_task_shadow() and why it is important only for this case?
We havent added this in any of our previous tests.
Is there any side effects of adding this in actual code ?


>
> Thanks,
> Pintu
>
>
>> --
>> Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-30  6:53         ` Pintu Kumar
@ 2018-03-30  8:04           ` Philippe Gerum
  2018-03-30  8:08             ` Philippe Gerum
  2018-03-30 13:43             ` Pintu Kumar
  0 siblings, 2 replies; 15+ messages in thread
From: Philippe Gerum @ 2018-03-30  8:04 UTC (permalink / raw)
  To: Pintu Kumar; +Cc: Xenomai@xenomai.org

On 03/30/2018 08:53 AM, Pintu Kumar wrote:
> On Fri, Mar 30, 2018 at 12:12 PM, Pintu Kumar <pintu.ping@gmail.com> wrote:
>> On Wed, Mar 28, 2018 at 11:02 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>>> On 03/28/2018 07:20 PM, Pintu Kumar wrote:
>>>> On Wed, Mar 28, 2018 at 8:37 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>>>>> On 03/28/2018 09:54 AM, Pintu Kumar wrote:
>>>>>> Hi,
>>>>>>
>>>>>> We are facing one issue on Xenomai-3 on x86_64 system.
>>>>>> Kernel: 4.9.51
>>>>>> ipipe version: 4
>>>>>> # /usr/xenomai/sbin/version
>>>>>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>>>>>
>>>>>> Its a very simple API level test.
>>>>>> We create a condition variable and wait for the condition inside a task.
>>>>>> Then, we immediately signal the condition from the main thread, and simply
>>>>>> wait for the test to finish.
>>>>>>
>>>>>> But we observed that condition task is never getting released, and the
>>>>>> program hangs.
>>>>>>
>>>>>> This is the output:
>>>>>> --------------------------
>>>>>> # ./condsignal
>>>>>> cond_task --> started
>>>>>> main --> cond signal success: 0
>>>>>> Waiting for task to finish....
>>>>>> ^C
>>>>>>
>>>>>>
>>>>>> This is the code snippet that was used.
>>>>>> -----------------------------------------------------
>>>>>> # cat condsignal.c
>>>>>>
>>>>>> /*
>>>>>>  * 1. Create an condition variable and wait inside a task.
>>>>>>  * 2. Delete the condition variable from main task.
>>>>>>  * 3. Observe that the condition wait is released and task ends normally.
>>>>>>  * 4. Finally main task should exit normally.
>>>>>>  *
>>>>>>  * */
>>>>>> #include <stdio.h>
>>>>>> #include <stdlib.h>
>>>>>> #include <sys/mman.h>
>>>>>> #include <alchemy/task.h>
>>>>>> #include <alchemy/mutex.h>
>>>>>> #include <alchemy/cond.h>
>>>>>> #include <alchemy/task.h>
>>>>>>
>>>>>> static RT_COND cond;
>>>>>> static RT_MUTEX mutex;
>>>>>>
>>>>>> static void cond_task(void *cookie)
>>>>>> {
>>>>>>         int err;
>>>>>>
>>>>>>         printf("%s --> started \n", __func__);
>>>>>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>>>>>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>>>>>>         rt_mutex_release(&mutex);
>>>>>>         if (err) {
>>>>>>                 printf("rt_cond_wait failed, err = %d", err);
>>>>>>                 return;
>>>>>>         }
>>>>>>         printf("%s --> cond wait done\n", __func__);
>>>>>> }
>>>>>>
>>>>>> int main(int argc, char **argv)
>>>>>> {
>>>>>>         int err;
>>>>>>         RT_TASK task1;
>>>>>>
>>>>>>         mlockall(MCL_CURRENT | MCL_FUTURE);
>>>>>
>>>>>         The line above is redundant with libcobalt inits. But you need:
>>>>>
>>>>>           err = rt_task_shadow(NULL, "main", <prio>, 0);
>>>>>           ...
>>>>
>>>> OK thank you so much for this. I will try it.
>>>> And, yes I am debugging this issue.
>>>> I found that rt_cond_wait_timed, did not return from here:
>>>
>>> Ok, thanks for looking at this. The issue is that the app is missing the
>>> call to rt_task_shadow() in the main thread. So that thread receives
>>> -EPERM from rt_mutex_acquire(), and the rest is a fallout of that
>>> original error.
>>>
>>
>> Ok, we tried adding rt_task_shadow(NULL, "main", 99, 0), just before
>> rt_task_create(),
>> but it did not help.
>> Still the tasks hangs under rt_cond_wait().
>> So, we need to debug further.
>>
> 
> OK. Good news :)
> I changed as below and it worked.
> rt_task_shadow(NULL, "main", 98, 0)
> 

> So, basically, I lowered the priority of main task and the cond_task
> exited normally this time.
> This indicates that cond_task should run first, then the main task.
>

No, the priority is irrelevant to this case. Please check your code: a
condition variable has to be paired with a condition, otherwise it does
not make any sense. I double-checked here already, I'm confident there
is no such issue with the rt_cond* API. Typical example attached.

> But, now the question is:
> when do we need rt_task_shadow() and why it is important only for this case?
> We havent added this in any of our previous tests.

You need that to convert the main() thread into an Alchemy task.
Excerpt from the documentation of rt_task_shadow:
 * Set the calling thread personality to the Alchemy API, enabling the
 * full set of Alchemy services.

rt_mutex* and friends are Alchemy services.

> Is there any side effects of adding this in actual code ?
> 

Yes, it may help in fixing it.

> 
>>
>> Thanks,
>> Pintu
>>
>>
>>> --
>>> Philippe.
> 


-- 
Philippe.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: condsig.c
Type: text/x-csrc
Size: 1154 bytes
Desc: not available
URL: <http://xenomai.org/pipermail/xenomai/attachments/20180330/3e443929/attachment.c>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-30  8:04           ` Philippe Gerum
@ 2018-03-30  8:08             ` Philippe Gerum
  2018-03-30 13:43             ` Pintu Kumar
  1 sibling, 0 replies; 15+ messages in thread
From: Philippe Gerum @ 2018-03-30  8:08 UTC (permalink / raw)
  To: Pintu Kumar; +Cc: Xenomai@xenomai.org

On 03/30/2018 10:04 AM, Philippe Gerum wrote:
> On 03/30/2018 08:53 AM, Pintu Kumar wrote:
>> On Fri, Mar 30, 2018 at 12:12 PM, Pintu Kumar <pintu.ping@gmail.com> wrote:
>>> On Wed, Mar 28, 2018 at 11:02 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>>>> On 03/28/2018 07:20 PM, Pintu Kumar wrote:
>>>>> On Wed, Mar 28, 2018 at 8:37 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>>>>>> On 03/28/2018 09:54 AM, Pintu Kumar wrote:
>>>>>>> Hi,
>>>>>>>
>>>>>>> We are facing one issue on Xenomai-3 on x86_64 system.
>>>>>>> Kernel: 4.9.51
>>>>>>> ipipe version: 4
>>>>>>> # /usr/xenomai/sbin/version
>>>>>>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>>>>>>
>>>>>>> Its a very simple API level test.
>>>>>>> We create a condition variable and wait for the condition inside a task.
>>>>>>> Then, we immediately signal the condition from the main thread, and simply
>>>>>>> wait for the test to finish.
>>>>>>>
>>>>>>> But we observed that condition task is never getting released, and the
>>>>>>> program hangs.
>>>>>>>
>>>>>>> This is the output:
>>>>>>> --------------------------
>>>>>>> # ./condsignal
>>>>>>> cond_task --> started
>>>>>>> main --> cond signal success: 0
>>>>>>> Waiting for task to finish....
>>>>>>> ^C
>>>>>>>
>>>>>>>
>>>>>>> This is the code snippet that was used.
>>>>>>> -----------------------------------------------------
>>>>>>> # cat condsignal.c
>>>>>>>
>>>>>>> /*
>>>>>>>  * 1. Create an condition variable and wait inside a task.
>>>>>>>  * 2. Delete the condition variable from main task.
>>>>>>>  * 3. Observe that the condition wait is released and task ends normally.
>>>>>>>  * 4. Finally main task should exit normally.
>>>>>>>  *
>>>>>>>  * */
>>>>>>> #include <stdio.h>
>>>>>>> #include <stdlib.h>
>>>>>>> #include <sys/mman.h>
>>>>>>> #include <alchemy/task.h>
>>>>>>> #include <alchemy/mutex.h>
>>>>>>> #include <alchemy/cond.h>
>>>>>>> #include <alchemy/task.h>
>>>>>>>
>>>>>>> static RT_COND cond;
>>>>>>> static RT_MUTEX mutex;
>>>>>>>
>>>>>>> static void cond_task(void *cookie)
>>>>>>> {
>>>>>>>         int err;
>>>>>>>
>>>>>>>         printf("%s --> started \n", __func__);
>>>>>>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>>>>>>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>>>>>>>         rt_mutex_release(&mutex);
>>>>>>>         if (err) {
>>>>>>>                 printf("rt_cond_wait failed, err = %d", err);
>>>>>>>                 return;
>>>>>>>         }
>>>>>>>         printf("%s --> cond wait done\n", __func__);
>>>>>>> }
>>>>>>>
>>>>>>> int main(int argc, char **argv)
>>>>>>> {
>>>>>>>         int err;
>>>>>>>         RT_TASK task1;
>>>>>>>
>>>>>>>         mlockall(MCL_CURRENT | MCL_FUTURE);
>>>>>>
>>>>>>         The line above is redundant with libcobalt inits. But you need:
>>>>>>
>>>>>>           err = rt_task_shadow(NULL, "main", <prio>, 0);
>>>>>>           ...
>>>>>
>>>>> OK thank you so much for this. I will try it.
>>>>> And, yes I am debugging this issue.
>>>>> I found that rt_cond_wait_timed, did not return from here:
>>>>
>>>> Ok, thanks for looking at this. The issue is that the app is missing the
>>>> call to rt_task_shadow() in the main thread. So that thread receives
>>>> -EPERM from rt_mutex_acquire(), and the rest is a fallout of that
>>>> original error.
>>>>
>>>
>>> Ok, we tried adding rt_task_shadow(NULL, "main", 99, 0), just before
>>> rt_task_create(),
>>> but it did not help.
>>> Still the tasks hangs under rt_cond_wait().
>>> So, we need to debug further.
>>>
>>
>> OK. Good news :)
>> I changed as below and it worked.
>> rt_task_shadow(NULL, "main", 98, 0)
>>
> 
>> So, basically, I lowered the priority of main task and the cond_task
>> exited normally this time.
>> This indicates that cond_task should run first, then the main task.
>>
> 
> No, the priority is irrelevant to this case. Please check your code: a
> condition variable has to be paired with a condition, otherwise it does
> not make any sense. I double-checked here already, I'm confident there
> is no such issue with the rt_cond* API. Typical example attached.
> 
>> But, now the question is:
>> when do we need rt_task_shadow() and why it is important only for this case?
>> We havent added this in any of our previous tests.
> 
> You need that to convert the main() thread into an Alchemy task.
> Excerpt from the documentation of rt_task_shadow:
>  * Set the calling thread personality to the Alchemy API, enabling the
>  * full set of Alchemy services.
> 
> rt_mutex* and friends are Alchemy services.
> 
>> Is there any side effects of adding this in actual code ?
>>
> 
> Yes, it may help in fixing it.
> 

Besides, it turns the calling main() thread into a real-time task if
prio > 0. So any latency benchmark that would run over a main() context
without such initial transition to a rt task would measure non-rt
timings. This may explain some of the weird results you got with Alchemy.

-- 
Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
  2018-03-30  8:04           ` Philippe Gerum
  2018-03-30  8:08             ` Philippe Gerum
@ 2018-03-30 13:43             ` Pintu Kumar
  1 sibling, 0 replies; 15+ messages in thread
From: Pintu Kumar @ 2018-03-30 13:43 UTC (permalink / raw)
  To: Philippe Gerum; +Cc: Xenomai@xenomai.org

On Fri, Mar 30, 2018 at 1:34 PM, Philippe Gerum <rpm@xenomai.org> wrote:
> On 03/30/2018 08:53 AM, Pintu Kumar wrote:
>> On Fri, Mar 30, 2018 at 12:12 PM, Pintu Kumar <pintu.ping@gmail.com> wrote:
>>> On Wed, Mar 28, 2018 at 11:02 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>>>> On 03/28/2018 07:20 PM, Pintu Kumar wrote:
>>>>> On Wed, Mar 28, 2018 at 8:37 PM, Philippe Gerum <rpm@xenomai.org> wrote:
>>>>>> On 03/28/2018 09:54 AM, Pintu Kumar wrote:
>>>>>>> Hi,
>>>>>>>
>>>>>>> We are facing one issue on Xenomai-3 on x86_64 system.
>>>>>>> Kernel: 4.9.51
>>>>>>> ipipe version: 4
>>>>>>> # /usr/xenomai/sbin/version
>>>>>>> Xenomai/cobalt v3.0.6 -- #5956064 (2018-03-20 12:13:33 +0100)
>>>>>>>
>>>>>>> Its a very simple API level test.
>>>>>>> We create a condition variable and wait for the condition inside a task.
>>>>>>> Then, we immediately signal the condition from the main thread, and simply
>>>>>>> wait for the test to finish.
>>>>>>>
>>>>>>> But we observed that condition task is never getting released, and the
>>>>>>> program hangs.
>>>>>>>
>>>>>>> This is the output:
>>>>>>> --------------------------
>>>>>>> # ./condsignal
>>>>>>> cond_task --> started
>>>>>>> main --> cond signal success: 0
>>>>>>> Waiting for task to finish....
>>>>>>> ^C
>>>>>>>
>>>>>>>
>>>>>>> This is the code snippet that was used.
>>>>>>> -----------------------------------------------------
>>>>>>> # cat condsignal.c
>>>>>>>
>>>>>>> /*
>>>>>>>  * 1. Create an condition variable and wait inside a task.
>>>>>>>  * 2. Delete the condition variable from main task.
>>>>>>>  * 3. Observe that the condition wait is released and task ends normally.
>>>>>>>  * 4. Finally main task should exit normally.
>>>>>>>  *
>>>>>>>  * */
>>>>>>> #include <stdio.h>
>>>>>>> #include <stdlib.h>
>>>>>>> #include <sys/mman.h>
>>>>>>> #include <alchemy/task.h>
>>>>>>> #include <alchemy/mutex.h>
>>>>>>> #include <alchemy/cond.h>
>>>>>>> #include <alchemy/task.h>
>>>>>>>
>>>>>>> static RT_COND cond;
>>>>>>> static RT_MUTEX mutex;
>>>>>>>
>>>>>>> static void cond_task(void *cookie)
>>>>>>> {
>>>>>>>         int err;
>>>>>>>
>>>>>>>         printf("%s --> started \n", __func__);
>>>>>>>         rt_mutex_acquire(&mutex, TM_INFINITE);
>>>>>>>         err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
>>>>>>>         rt_mutex_release(&mutex);
>>>>>>>         if (err) {
>>>>>>>                 printf("rt_cond_wait failed, err = %d", err);
>>>>>>>                 return;
>>>>>>>         }
>>>>>>>         printf("%s --> cond wait done\n", __func__);
>>>>>>> }
>>>>>>>
>>>>>>> int main(int argc, char **argv)
>>>>>>> {
>>>>>>>         int err;
>>>>>>>         RT_TASK task1;
>>>>>>>
>>>>>>>         mlockall(MCL_CURRENT | MCL_FUTURE);
>>>>>>
>>>>>>         The line above is redundant with libcobalt inits. But you need:
>>>>>>
>>>>>>           err = rt_task_shadow(NULL, "main", <prio>, 0);
>>>>>>           ...
>>>>>
>>>>> OK thank you so much for this. I will try it.
>>>>> And, yes I am debugging this issue.
>>>>> I found that rt_cond_wait_timed, did not return from here:
>>>>
>>>> Ok, thanks for looking at this. The issue is that the app is missing the
>>>> call to rt_task_shadow() in the main thread. So that thread receives
>>>> -EPERM from rt_mutex_acquire(), and the rest is a fallout of that
>>>> original error.
>>>>
>>>
>>> Ok, we tried adding rt_task_shadow(NULL, "main", 99, 0), just before
>>> rt_task_create(),
>>> but it did not help.
>>> Still the tasks hangs under rt_cond_wait().
>>> So, we need to debug further.
>>>
>>
>> OK. Good news :)
>> I changed as below and it worked.
>> rt_task_shadow(NULL, "main", 98, 0)
>>
>
>> So, basically, I lowered the priority of main task and the cond_task
>> exited normally this time.
>> This indicates that cond_task should run first, then the main task.
>>
>
> No, the priority is irrelevant to this case. Please check your code: a
> condition variable has to be paired with a condition, otherwise it does
> not make any sense. I double-checked here already, I'm confident there
> is no such issue with the rt_cond* API. Typical example attached.
>

OK. Thank you so much for your sample code.
Now, my code is also working fine with any priority, and without
infinite loop, and adding rt_task_join in the end.
Now, I found the root cause.
I found that the following while condition is important:
{{{
        while (count == oldcount)
                err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
}}}
Then, we make it true by incrementing the count before signal.
If I remove the while, it did not work.
So, the point is, we should wait for the condition to true, before
executing the cond_wait itself.
Please correct me if I am wrong.

Thank You,
Pintu


>> But, now the question is:
>> when do we need rt_task_shadow() and why it is important only for this case?
>> We havent added this in any of our previous tests.
>
> You need that to convert the main() thread into an Alchemy task.
> Excerpt from the documentation of rt_task_shadow:
>  * Set the calling thread personality to the Alchemy API, enabling the
>  * full set of Alchemy services.
>
> rt_mutex* and friends are Alchemy services.
>
>> Is there any side effects of adding this in actual code ?
>>
>
> Yes, it may help in fixing it.
>
>>
>>>
>>> Thanks,
>>> Pintu
>>>
>>>
>>>> --
>>>> Philippe.
>>
>
>
> --
> Philippe.


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2018-03-30 13:43 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-28  7:54 [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination Pintu Kumar
2018-03-28  8:12 ` Philippe Gerum
2018-03-28  9:32   ` Pintu Kumar
2018-03-28  9:46     ` Philippe Gerum
2018-03-28  8:14 ` Julien Blanc
2018-03-28 10:04   ` Pintu Kumar
2018-03-28 13:29     ` Philippe Gerum
2018-03-28 15:07 ` Philippe Gerum
2018-03-28 17:20   ` Pintu Kumar
2018-03-28 17:32     ` Philippe Gerum
2018-03-30  6:42       ` Pintu Kumar
2018-03-30  6:53         ` Pintu Kumar
2018-03-30  8:04           ` Philippe Gerum
2018-03-30  8:08             ` Philippe Gerum
2018-03-30 13:43             ` Pintu Kumar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.