msleep() on recursivly locked mutexes

Attilio Rao attilio at FreeBSD.org
Wed May 2 09:08:02 UTC 2007


Sergey Babkin wrote:
>> From: Julian Elischer <julian at elischer.org>
>> Basically you shouldn't have a recursed mutex FULL STOP. We have a couple 
>> of instances in the kernel where we allow a mutex to recurse, but they had to be 
>> hard fought, and the general rule is "Don't". If you are recursing on 
>> a mutex you need to switch to some other method of doing things.
>> e.g. reference counts, turnstiles, whatever.. use the mutex to create these 
> 
> One typical problem is when someone holds a mutex
> and needs to call a function that also tried to get the mutex.
> The typical solution for it is to provide two versions of
> this function, one expecting the mutex being already held
> by the caller, the other being a wrapper that grabs the mutex and
> then calls the actual worker function.

If that happens rather frequently, the better thing to do is assuming 
that the lock is alredy held in the function (adding an assertion for 
it) than acquiring/releasing before/after the function.

Attilio



More information about the freebsd-hackers mailing list