[Vm-dev] Simple versus Recursive Mutexes

Ben Coman btc at openinworld.com
Tue Feb 2 17:54:47 UTC 2016


While we are in the process of revising the Mutex infrastructure [1]
to use dedicated primitives rather than rely on counting via a
semaphore, I want to raise the question of simple versus recursive
mutexes.

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." [1].

There are two interesting discussion on stockoverflow [2][3]. But btw
I disagree with Tall Jeff [3] 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" [4] demonstrates some
performance considerations and advises "a recursive mutex is dangerous
because you lose sense of locking scope. It costs more than a simple
mutex."

"Recursive Locks Will Kill You!" [5] provides some thread safety guidelines.

Finally, there is a paper "Ad Hoc Synchronization Considered harmful"
[6], which I haven't read yet since I wanted to get this post out
before heading to bed, but I hope to tomorrow.


[1] http://www.zaval.org/resources/library/butenhof1.html
[2] http://stackoverflow.com/questions/2415082/when-to-use-recursive-mutex
[3] http://stackoverflow.com/questions/187761/recursive-lock-mutex-vs-non-recursive-lock-mutex
[4] http://askldjd.com/2009/10/26/prefer-simple-mutex-over-recursive-mutex/
[5] http://www.fieryrobot.com/blog/2008/10/14/recursive-locks-will-kill-you/
[6] https://www.usenix.org/legacy/event/osdi10/tech/full_papers/Xiong.pdf


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.

cheers -ben


More information about the Vm-dev mailing list