[En-Nut-Discussion] Feature Request 1572837: mutex API nesting level info

duane ellis ethernut at duaneellis.com
Tue Jul 15 03:04:41 CEST 2008


Przemek Rudy wrote:
> Hi,
> Sorry for the delay.
>
> The story with this mutex is pretty simple:
> There is a function that calls the mutex. This function can be called 
> recursively. 
>   

Posix PTHREADS describes this as a: PTHREAD_MUTEX_RECURSIVE.

POSIX says:
    A recursive mutex can be locked repeatedly by the owner. The mutex
doesn't become unlocked until the owner has called
*pthread_mutex_unlock*() for each successful lock request that it has
outstanding on the mutex.

An other approach is a "PTHREAD_MUTEX_ERRORCHECK" type:

    An errorcheck mutex checks for deadlock conditions that occur when a
thread re-locks an already held mutex. If a thread attempts to relock a
mutex that it already holds, the lock request fails with the *EDEADLK*
error

== My tidbit ==
Obviously both of the above require an "current owner" in the mutex
structure. (I do not remember the internals of Nut's mutexes)

But - To convert an "error check" into a recursion type manually - do this:

LOCK:
    LOCK (or attempt to lock) the mutex.
          Either this SUCCEEDS, or ERRORS with "you already own the lock"
    obviously you just got the lock or you already own the lock and had
the error.
    Bump a recursion count
    (It is protected by the lock you must made)

WORK:
    Do your work.

UNLOCK:
    You obviously own the lock, the "count" is protected by the lock.
    Decrement the recursion count.
    If the count is now zero - RELEASE the lock.
    Otherwise - do not release the lock.

-Duane.








More information about the En-Nut-Discussion mailing list