Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 26 Apr 2007 23:38:55 +0200
From:      "Attilio Rao" <attilio@freebsd.org>
To:        "Hans Petter Selasky" <hselasky@c2i.net>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: msleep() on recursivly locked mutexes
Message-ID:  <3bbf2fe10704261438k5e25e892w3f821ac9507f5457@mail.gmail.com>
In-Reply-To: <200704262136.33196.hselasky@c2i.net>
References:  <200704262136.33196.hselasky@c2i.net>

next in thread | previous in thread | raw e-mail | index | archive | help
2007/4/26, Hans Petter Selasky <hselasky@c2i.net>:
> Hi,
>
> In the new USB stack I have defined the following:
>
> u_int32_t
> mtx_drop_recurse(struct mtx *mtx)
> {
>        u_int32_t recurse_level = mtx->mtx_recurse;
>        u_int32_t recurse_curr = recurse_level;
>
>        mtx_assert(mtx, MA_OWNED);
>
>        while(recurse_curr--) {
>            mtx_unlock(mtx);
>        }
>
>        return recurse_level;
> }
>
> void
> mtx_pickup_recurse(struct mtx *mtx, u_int32_t recurse_level)
> {
>        mtx_assert(mtx, MA_OWNED);
>
>        while(recurse_level--) {
>            mtx_lock(mtx);
>        }
>        return;
> }
>
> When I do a msleep() I do it like this:
>
>        level = mtx_drop_recurse(ctd->p_mtx);
>
>        error = msleep(ctd, ctd->p_mtx, 0,
>                       "config td sleep", timeout);
>
>        mtx_pickup_recurse(ctd->p_mtx, level);
>
> Are there any comments on integrating this functionality into msleep(), and
> adding mtx_drop_recurse() and mtx_pickup_recurse() to the FreeBSD kernel?

Several times I thought to adding recursed mutex handling in msleep &
condvar, but in the end the better approach is mantaining the current
behaviour.

Recursed mutexes often are results of incorrect locking strategies and
catering that kind of errors is necessarily a bad idea.
It is a lot better handling potential recursing mutexes outside from
sleeping points (that is what your implementation does).

Just, please, don't deal with internal struct mtx fields for that (so,
please, don't refer to mtx->mtx_recurse) just use the mutex(9) API
provided.

Attilio


-- 
Peace can only be achieved by understanding - A. Einstein



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3bbf2fe10704261438k5e25e892w3f821ac9507f5457>