Skip to contentSkip to conversation

mutex locking

Xenomai-help Jan Kiszka 1 participant 1 message Jun 21, 2006 1 message not in the archive

lore
Share & tools
lore

Jan Kiszka

author
#1 lore raw
In reply to a message not in the archive
Quoted 14 lines · Tobias Bär
Quoted 14 lines

-----Ursprüngliche Nachricht-----
Von: Jan Kiszka <[email protected]>
Gesendet: 21.06.06 11:46:34
An: Tobias Bär <[email protected]>
CC: [email protected]
Betreff: Re: [Xenomai-help] mutex locking

Try this, it's safe from such races:

/* returns -EWOULDBLOCK if the mutex is locked */
if (rt_mutex_lock(&write_mutex,TM_NONBLOCK) == 0)

{
      // do sth.
Quoted 5 lines
Quoted 2 lines
      rt_mutex_unlock(&mutex);
}

Thank's for the quick reply! But another question: How could it be that a mutex is locked twice?

RTFM ;): "Xenomai mutexes are implicitely recursive [...]."

Quoted 10 lines

if(rt_mutex_lock(&write_mutex,TM_NONBLOCK) == 0)

  {
    if(rt_mutex_lock(&write_mutex,TM_NONBLOCK) == 0)
      printf("Mutex is locked twice!\n");
   rt_mutex_unlock(&mutex);
  }

Is it locked by the ID of the thread?

Yep, the owner is recorded, also to perform priority inheritance when required. Recursive means that the current owner can acquire the mutex multiple times (but (s)he also has to release it symmetrically then - your example is lacking one unlock).

If you use the POSIX skin with its standard pthread_mutex'es, then you can decide about the recursiveness explicitly.

Jan