Recursive mutual exclusion
The regular mutex lock (shown earlier) will deadlock the calling
thread on attempts to re-lock a lock that it already holds.
A recursive mutually exclusive lock (recursive mutex or rmutex) allows
the holder of a lock
to re-lock without deadlock; other threads will block normally.
int rmutex_lock( rmutex_t *rmutex);
int rmutex_unlock(rmutex_t *rmutex);
Considerations for the use of recursive mutex locks:
-
The holder must unlock the lock for each time it was locked.
-
This facility is useful for
-
The implementation of recursive algorithms.
-
Situations where the code locking a resource cannot know which locks
have already been acquired.
This may arise in the implementation of library functions where generally
the activities of the callers are not known.
-
Recursive mutexes only prevent deadlock of a thread with itself
for a single resource.
It is still possible for a thread to become deadlocked even with
recursive mutexes.
Two (or more) threads can deadlock by each acquiring multiple locks
in an unfortunate order.
-
Recursive mutexes provide exclusivity but they sacrifice ``atomicity''.
A resource protected by an rmutex will be used by only one thread at a time;
however,
that use must be designed to be reentrant because that thread might
reacquire the resource in the midst of using it.
Next topic:
Reader-writer locks
Previous topic:
Spin locks
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004