Reentrant mutex

In computer science, the reentrant mutex (recursive mutex, recursive lock) is a particular type of mutual exclusion (mutex) device that may be locked multiple times by the same process/thread, without causing a deadlock.
While any attempt to perform the "lock" operation on an ordinary mutex (lock) would either fail or block when the mutex is already locked, on a recursive mutex this operation will succeed if and only if the locking thread is the one that already holds the lock. Typically, a recursive mutex tracks the number of times it has been locked, and requires equally many unlock operations to be performed before other threads may lock it.
Motivation.
Recursive mutexes solve the problem of non-reentrancy with regular mutexes: if a function that takes a lock and executes a callback is itself called by the callback, deadlock ensues. In pseudocode, that is the following situation:
 var m : Mutex // A non-recursive mutex, initially unlocked.
 function lock_and_call(i : Integer)
 m.lock()
 callback(i)
 m.unlock()
 function callback(i : Integer)
 if i > 0
 lock_and_call(i - 1)
 lock_and_call(1) // Invoking the function
Given these definitions, the function call will cause the following sequence of events:
Replacing the mutex with a recursive one solves the problem, because the final will succeed without blocking.
Practical use.
W. Richard Stevens notes that recursive locks are "tricky" to use correctly, and recommends their use for adapting single-threaded code without changing APIs, but "only when no other solution is possible".
The Java language's native synchronization mechanism, monitor, uses recursive locks. Syntactically, a lock is a block of code with the 'synchronized' keyword preceding it and any Object reference in parentheses that will be used as the mutex. Inside the synchronized block, the given object can be used as a condition variable by doing a wait(), notify(), or notifyAll() on it. Thus all Objects are both recursive mutexes and condition variables.
Software emulation.
Software emulation can be accomplished using the following structure: