All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anders Blomdell <anders.blomdell@domain.hid>
To: xenomai-help <xenomai@xenomai.org>
Subject: [Xenomai-help] What have I misunderstood about condition variables
Date: Tue, 11 Mar 2008 19:19:21 +0100	[thread overview]
Message-ID: <47D6CD29.4020700@domain.hid> (raw)

Why do I get the following output (Xenomai 2.4.2 / kernel 2.6.24.3):

ABCaAbBCABCcd 3
aAbBCABCcd 5
aAbBCABCcd 7
aAbBCABCcd 9
aAbBCABCcd 11
aAbBCABCcd 13

>From this small program:

#include <native/cond.h>
#include <native/mutex.h>
#include <native/task.h>
#include <native/timer.h>
#include <rtdk.h>
#include <sys/mman.h>

RT_TASK main_task, producer_task, consumer_task;
RT_MUTEX mutex;
RT_COND cond;
int data;

static void producer(void *arg)
{
  while(1) {
    rt_task_set_mode(0, T_PRIMARY, 0);
    rt_printf("A");
    rt_mutex_acquire(&mutex, TM_INFINITE);
    rt_printf("B");
    data++;
    rt_mutex_release(&mutex);
    rt_mutex_acquire(&mutex, TM_INFINITE);
    rt_cond_broadcast(&cond);
    rt_printf("C");
    rt_mutex_release(&mutex);
    rt_task_sleep(100000000L);
  }
}


static void consumer(void *arg)
{
  while(1) {
    int err;

    rt_task_set_mode(T_PRIMARY, 0, 0);
    rt_mutex_acquire(&mutex, TM_INFINITE);
    rt_printf("a");
    rt_task_sleep(1000000000L);
    rt_printf("b");
    err = rt_cond_wait(&cond, &mutex, TM_INFINITE);
    rt_printf("c");
    rt_mutex_release(&mutex);
    rt_printf("d %d\n", data);
  }
}


int main(int argc, char *argv[])
{
  mlockall(MCL_CURRENT|MCL_FUTURE);
  rt_print_auto_init(1);
  rt_task_shadow(&main_task, NULL, 1, T_FPU);
  rt_mutex_create(&mutex, NULL);
  rt_cond_create (&cond, NULL);
  rt_task_create(&producer_task, NULL, 0, 50, 0);
  rt_task_start(&producer_task, &producer, NULL);
  rt_task_create(&consumer_task, NULL, 0, 20, 0);
  rt_task_start(&consumer_task, &consumer, &consumer_task);
  while (1) {
    rt_task_sleep(1000000000L);
  }
  return 0;
}

To me the symptoms indicate that the high priority producer gets the mutex and
signals it before the consumer has properly started to wait on the condition.

Regards

Anders Blomdell


-- 
Anders Blomdell                  Email: anders.blomdell@domain.hid
Department of Automatic Control
Lund University                  Phone:    +46 46 222 4625
P.O. Box 118                     Fax:      +46 46 138118
SE-221 00 Lund, Sweden


             reply	other threads:[~2008-03-11 18:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-11 18:19 Anders Blomdell [this message]
2008-03-11 18:27 ` [Xenomai-help] What have I misunderstood about condition variables Gilles Chanteperdrix
2008-03-11 19:27   ` Anders Blomdell
2008-03-11 20:13     ` [Xenomai-core] " Anders Blomdell
2008-03-12 14:14       ` Philippe Gerum
2008-03-11 23:00     ` Gilles Chanteperdrix
2008-03-12 12:00       ` Anders Blomdell
2008-03-12 13:11         ` Gilles Chanteperdrix

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=47D6CD29.4020700@domain.hid \
    --to=anders.blomdell@domain.hid \
    --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.