From owner-freebsd-hackers@FreeBSD.ORG Thu Apr 26 21:38:56 2007 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 96B5416A404 for ; Thu, 26 Apr 2007 21:38:56 +0000 (UTC) (envelope-from asmrookie@gmail.com) Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.241]) by mx1.freebsd.org (Postfix) with ESMTP id 5436B13C4AD for ; Thu, 26 Apr 2007 21:38:56 +0000 (UTC) (envelope-from asmrookie@gmail.com) Received: by an-out-0708.google.com with SMTP id c24so410861ana for ; Thu, 26 Apr 2007 14:38:55 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=UuLOatCkBiIRwGTT5GIXBkB3F7mHsliq5xItlacCtLGobhLVK8sVZK3heeQi9+hgeujPVrutLAPZsl9u450Fuj3Yi46Qd08nRUn7zKizU83VmhpGKsec8ZdekGT9lwz4m8KOTtgorDj9Elr3vC5+Ursaj3dP/vMJjqacAsA3wzU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=GAXk0vJmfisfAt5lsfa7tkq4xgG464Akc9FDCH/nkgc1ox/dhYFTOh0ypZEkvQK6rUXwRWbq7Wp8coWla2oMdRC7AjSjJX+sr8F6qeGpnnyeOM03Tam4PsYm8j9mqwu/KaVWiN0CgrXlk5QCJHt+A08KsTnHbpni/Oqdnmh+5u8= Received: by 10.100.40.17 with SMTP id n17mr1447024ann.1177623535468; Thu, 26 Apr 2007 14:38:55 -0700 (PDT) Received: by 10.100.178.1 with HTTP; Thu, 26 Apr 2007 14:38:55 -0700 (PDT) Message-ID: <3bbf2fe10704261438k5e25e892w3f821ac9507f5457@mail.gmail.com> Date: Thu, 26 Apr 2007 23:38:55 +0200 From: "Attilio Rao" Sender: asmrookie@gmail.com To: "Hans Petter Selasky" In-Reply-To: <200704262136.33196.hselasky@c2i.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <200704262136.33196.hselasky@c2i.net> X-Google-Sender-Auth: 53a4ac8bf6a46057 Cc: freebsd-hackers@freebsd.org Subject: Re: msleep() on recursivly locked mutexes X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Apr 2007 21:38:56 -0000 2007/4/26, Hans Petter Selasky : > 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