From owner-freebsd-current@FreeBSD.ORG Wed Mar 14 09:17:04 2007 Return-Path: X-Original-To: current@freebsd.org Delivered-To: freebsd-current@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 149A916A401; Wed, 14 Mar 2007 09:17:04 +0000 (UTC) (envelope-from rwatson@FreeBSD.org) Received: from cyrus.watson.org (cyrus.watson.org [209.31.154.42]) by mx1.freebsd.org (Postfix) with ESMTP id E125813C459; Wed, 14 Mar 2007 09:17:03 +0000 (UTC) (envelope-from rwatson@FreeBSD.org) Received: from fledge.watson.org (fledge.watson.org [209.31.154.41]) by cyrus.watson.org (Postfix) with ESMTP id 3AE3946FA7; Wed, 14 Mar 2007 04:17:03 -0500 (EST) Date: Wed, 14 Mar 2007 10:17:03 +0100 (BST) From: Robert Watson X-X-Sender: robert@fledge.watson.org To: Pawel Jakub Dawidek In-Reply-To: <20070314042926.GA6013@garage.freebsd.pl> Message-ID: <20070314101424.A60010@fledge.watson.org> References: <200703092241.l29Mf2Ds062856@repoman.freebsd.org> <200703121535.22140.jhb@freebsd.org> <20070312200345.GB5688@garage.freebsd.pl> <200703121618.41084.jhb@freebsd.org> <45F5E1F9.5090806@elischer.org> <20070313010309.Q25395@fledge.watson.org> <45F73AE7.6010508@elischer.org> <45F771E2.9050709@elischer.org> <20070314042926.GA6013@garage.freebsd.pl> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: Julian Elischer , current@freebsd.org Subject: Re: [RFC] locking.9 X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Mar 2007 09:17:04 -0000 On Wed, 14 Mar 2007, Pawel Jakub Dawidek wrote: > I think it should be: > >> You have: You want: Spin_mtx Slp_mtx sx_lock rw_lock sleep >> SPIN mutex ok no no no no-3 >> Sleep mutex ok ok-1 no ok no-3 >> sx_lock ok no ?? no ok-4 > sx_lock ok ok ok-2 ok ok >> rw_lock ok no no ok-2 no-3 > rw_lock ok ok no ok-2 no > > And I'd sort the table a bit differently: spin, mtx, rw, sx[, sleep]. Hmm. I was thinking more along the lines of a table that compared properties and locks. I.e., horizontally feature, vertically lock type. Features would be things like "safe in interrupt handlers", "can perform unbounded sleep while holding", "supports multiple readers, not just exclusion", "priority propagation", etc. > BTW. I just wake up with a feeling that I did something wrong in my code. I > thought about it for a moment and it seems I'm right. When one always use > rw/sx locks this way: > > sx_xlock(); > /* do work */ > sx_downgrade(); > /* do work */ > sx_sunlock(); > > (the same for rw(9)) the lock will _never_ be shared, because one still > always acquire exclusive lock first, which serialize synchronization. Is my > thinking correct? If so, I think it's not very obvious, so we may want to > add a comment about such use to the manual page as well. FYI, one of the problems with "downgrade" as a primitive is that if you always acquire exclusively and then downgrade, you never get multiple shared readers, as all shared readers must first go through an exclusive stage. It only helps if you have threads entering as shared from inception. This is one reason why naively moving to reader/writer locks doesn't solve concurrency limits in the TCP input path. Robert N M Watson Computer Laboratory University of Cambridge