All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philippe Gerum <rpm@xenomai.org>
To: Pintu Kumar <pintu.ping@gmail.com>
Cc: "Xenomai@xenomai.org" <xenomai@xenomai.org>
Subject: Re: [Xenomai] Problem with rt_cond_wait and rt_cond_signal combination
Date: Wed, 28 Mar 2018 11:46:21 +0200	[thread overview]
Message-ID: <c06ad595-f04a-4e0b-5d79-56ae35671965@xenomai.org> (raw)
In-Reply-To: <CAOuPNLhZ7LQcR6e_dm-ExRNdFj-QHvR8Z=XuQkksLua-jzxw1w@mail.gmail.com>

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.


  reply	other threads:[~2018-03-28  9:46 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c06ad595-f04a-4e0b-5d79-56ae35671965@xenomai.org \
    --to=rpm@xenomai.org \
    --cc=pintu.ping@gmail.com \
    --cc=xenomai@xenomai.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.