All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philippe Gerum <rpm@xenomai.org>
To: "Tobias Bär" <Tobias.Baer@domain.hid>
Cc: xenomai@xenomai.org
Subject: Re: [Xenomai-help] mutex locking
Date: Wed, 21 Jun 2006 11:58:53 +0200	[thread overview]
Message-ID: <1150883933.7498.17.camel@domain.hid> (raw)
In-Reply-To: <216509776@domain.hid>

On Wed, 2006-06-21 at 11:37 +0200, Tobias Bär wrote:
> Hi all,
> 
> my question is about the right way to lock and inquire a mutex. At time, my code looks like this:
> 
> 
> rt_mutex_inquire(&mutex,TM_NONBLOCK);
> 
> if(info.lockcnt == 0) 
> {
>       rt_mutex_lock(&write_mutex,TM_NONBLOCK);
> 
>       // do sth.
> 
>       rt_mutex_unlock(&mutex);
> }
> 
> 
> What happens if anyone locks the mutex between my "rt_mutex_inquire(...)" and my "if(info.lockcnt == 0)" ?
> 
> Is this the standard way to lock and inquire a mutex?

No, this snippet is terminally broken, I'm afraid. The mutex object is
precisely aimed at avoiding this kind of race conditions; the code above
would introduce them back.

Native skin mutexes are recursive by nature, so there is no need to
inquire for their current status. Additionally, passing TM_NONBLOCK
might cause mutex_lock() to return an error, if another thread holds it
already, so you need to check for the return value in any case. What you
likely want is, either:

if (rt_mutex_lock(...TM_INFINITE) < 0)
	bail_out();
/* work in critical section */
rt_mutex_unlock()

or,

if (rt_mutex_lock(...TM_NONBLOCK) < 0)
	resource_not_immediately_available();
/* work in critical section */
rt_mutex_unlock()


> 
> Thanks,
>   Tobias
> 
> _______________________________________________
> Xenomai-help mailing list
> Xenomai-help@domain.hid
> https://mail.gna.org/listinfo/xenomai-help
-- 
Philippe.




  parent reply	other threads:[~2006-06-21  9:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-21  9:37 [Xenomai-help] mutex locking Tobias Bär
2006-06-21  9:46 ` Jan Kiszka
2006-06-21  9:58 ` Philippe Gerum [this message]
  -- strict thread matches above, loose matches on Subject: below --
2006-06-21  9:58 Tobias Bär
     [not found] <216532953@domain.hid>
2006-06-21 10:32 ` Jan Kiszka

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=1150883933.7498.17.camel@domain.hid \
    --to=rpm@xenomai.org \
    --cc=Tobias.Baer@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.