[Vm-dev] Simple versus Recursive Mutexes
btc at openinworld.com
Tue Feb 2 17:54:47 UTC 2016
While we are in the process of revising the Mutex infrastructure 
to use dedicated primitives rather than rely on counting via a
semaphore, I want to raise the question of simple versus recursive
This isn't something I know much about, except that some people
strongly believe recursive mutexes are evil, including David Butenhof
who apparently was the guy who added recursive mutexes to POSIX "on a
dare [...] but nobody was supposed to use recursive mutexes." .
There are two interesting discussion on stockoverflow . But btw
I disagree with Tall Jeff  says "The difference between a recursive
and non-recursive mutex has to do with ownership. " I think he
confuses a simple-mutex and binary-semaphore, as do a some commenters.
"Prefer Simple Mutex Over Recursive Mutex"  demonstrates some
performance considerations and advises "a recursive mutex is dangerous
because you lose sense of locking scope. It costs more than a simple
"Recursive Locks Will Kill You!"  provides some thread safety guidelines.
Finally, there is a paper "Ad Hoc Synchronization Considered harmful"
, which I haven't read yet since I wanted to get this post out
before heading to bed, but I hope to tomorrow.
So our existing Mutex implementation happens to be recursive, and I'm
not suggesting we change that. However if we should consider *not*
baking the *assumption* of recursion into the primitives, so the same
primitives could *also* be used for a SimpleMutex class. The logic to
provide recursion for Mutex is only [ owner = activeProcess ] which is
easily done in-image.
So I propose introducing aquire/release primitives based off the
existing critical section primitives with the recursion removed.
More information about the Vm-dev