From: Roman Pisl <rpisl@domain.hid>
To: xenomai@xenomai.org
Subject: [Xenomai-help] Problem with mutexes in daemon
Date: Wed, 22 Apr 2009 11:41:50 +0200 [thread overview]
Message-ID: <49EEE65E.6060308@domain.hid> (raw)
[-- Attachment #1: Type: text/plain, Size: 419 bytes --]
Hello,
I'm experiencing weird behavior with mutexes in a daemon application.
After daemon() is called, unnamed mutexes fail on call
rt_mutex_release() with -EPERM. Everything is ok for named mutexes.
Is there something special with daemon and unnamed mutexes - should all
have name? Or is it a bug? I'm using kernel 2.6.28 and Xenomai 2.4.6.1.
Example showing this problem is attached.
Thank you for help,
Roman
[-- Attachment #2: daemon.c --]
[-- Type: text/plain, Size: 3313 bytes --]
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <native/task.h>
#include <native/cond.h>
#include <native/mutex.h>
#include <native/timer.h>
RT_TASK demo_task;
RT_TASK main_task;
RT_MUTEX mutex;
RT_MUTEX shut_mutex;
RT_COND shut_cond;
#define TRUE 1
#define FALSE 0
bool terminated = FALSE;
bool terminated2 = FALSE;
void demo(void *arg)
{
int ret;
RTIME now, previous;
rt_task_set_periodic(NULL, TM_NOW, 1000000000);
previous = rt_timer_read();
while (!terminated2) {
rt_task_wait_period(NULL);
now = rt_timer_read();
printf("DT: Time since last turn: %ld.%06ld ms\n",
(long)(now - previous) / 1000000,
(long)(now - previous) % 1000000);
previous = now;
if (ret = rt_mutex_acquire(&mutex, TM_INFINITE))
printf("DT: rt_mutex_acquire() failed %i\n", ret);
rt_task_sleep(1000000);
if (ret = rt_mutex_release(&mutex))
printf("DT: rt_mutex_release() failed %i\n", ret);
}
if (ret = rt_mutex_acquire(&shut_mutex, TM_INFINITE))
printf("DT: rt_mutex_acquire() before cond failed %i\n", ret);
if (ret = rt_cond_signal(&shut_cond))
printf("DT: rt_cond_signal() failed %i\n", ret);
if (ret = rt_mutex_release(&shut_mutex))
printf("DT: rt_mutex_release() after cond failed %i\n", ret);
}
void catch_signal(int sig)
{
terminated = TRUE;
}
int main(int argc, char* argv[])
{
int ret;
//when daemon - problems with unnamed mutexes
//without daemon everything ok
daemon(1, 1);
if (ret = rt_mutex_create(&mutex, NULL))
{
printf("Error creating mutex %i\n", ret);
return 0;
}
if (ret = rt_mutex_create(&shut_mutex, NULL))
{
printf("Error creating shut_mutex %i\n", ret);
return 0;
}
if (ret = rt_cond_create(&shut_cond, NULL))
{
printf("Error creating cond%i\n", ret);
return 0;
}
signal(SIGTERM, catch_signal);
signal(SIGINT, catch_signal);
mlockall(MCL_CURRENT|MCL_FUTURE);
if (ret = rt_task_shadow(&main_task, "main", 90, 0))
printf("rt_task_shadow() failed %i\n", ret);
if (ret = rt_task_create(&demo_task, "trivial", 0, 99, 0))
printf("rt_task_create() failed %i\n", ret);
rt_task_start(&demo_task, &demo, NULL);
while (!terminated)
{
if (ret = rt_mutex_acquire(&mutex, TM_INFINITE))
printf("MT: rt_mutex_acquire() failed, %i\n", ret);
rt_task_sleep(1000000);
printf("sleep\n");
if (ret = rt_mutex_release(&mutex))
printf("MT: rt_mutex_release() failed %i\n", ret);
rt_task_sleep(1000000000);
}
printf("Finished...\n");
if (ret = rt_mutex_acquire(&shut_mutex, TM_INFINITE))
printf("MT: rt_mutex_acquire() before cond failed %i\n", ret);
terminated2 = TRUE;
if (ret = rt_cond_wait(&shut_cond, &shut_mutex, TM_INFINITE))
printf("MT: rt_cond_signal() failed %i\n", ret);
if (ret = rt_mutex_release(&shut_mutex))
printf("MT: rt_mutex_release() after cond failed %i\n", ret);
rt_task_delete(&demo_task);
rt_mutex_delete(&mutex);
rt_mutex_delete(&shut_mutex);
rt_cond_delete(&shut_cond);
}
next reply other threads:[~2009-04-22 9:41 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-22 9:41 Roman Pisl [this message]
2009-04-22 9:57 ` [Xenomai-help] Problem with mutexes in daemon Gilles Chanteperdrix
2009-04-22 10:34 ` Roman Pisl
2009-04-22 11:57 ` 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=49EEE65E.6060308@domain.hid \
--to=rpisl@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.