From owner-freebsd-arch@FreeBSD.ORG Wed Nov 28 22:42:14 2007 Return-Path: Delivered-To: arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9F4E416A419 for ; Wed, 28 Nov 2007 22:42:14 +0000 (UTC) (envelope-from deischen@freebsd.org) Received: from mail.netplex.net (mail.netplex.net [204.213.176.10]) by mx1.freebsd.org (Postfix) with ESMTP id 7392013C45B for ; Wed, 28 Nov 2007 22:42:14 +0000 (UTC) (envelope-from deischen@freebsd.org) Received: from sea.ntplx.net (sea.ntplx.net [204.213.176.11]) by mail.netplex.net (8.14.2/8.14.2/NETPLEX) with ESMTP id lASMJgnZ008341; Wed, 28 Nov 2007 17:19:42 -0500 (EST) X-Virus-Scanned: by AMaViS and Clam AntiVirus (mail.netplex.net) X-Greylist: Message whitelisted by DRAC access database, not delayed by milter-greylist-4.0 (mail.netplex.net [204.213.176.10]); Wed, 28 Nov 2007 17:19:42 -0500 (EST) Date: Wed, 28 Nov 2007 17:19:42 -0500 (EST) From: Daniel Eischen X-X-Sender: eischen@sea.ntplx.net To: "M. Warner Losh" In-Reply-To: <20071128.151021.709401576.imp@bsdimp.com> Message-ID: References: <20071128.151021.709401576.imp@bsdimp.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: arch@freebsd.org Subject: Re: Code review request: small optimization to localtime.c X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Daniel Eischen List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 28 Nov 2007 22:42:14 -0000 On Wed, 28 Nov 2007, M. Warner Losh wrote: > Please find enclosed some small optimizations. I know they make a > couple lines too long, I'll correct that before I commit. They make > the time functions do less redundant locking. > > However, in many places we do the following: > > pthread_mutex_lock(); > if (!is_set) { > is_set = true; > // do something > } > pthread_mutex_unlock(); > > This is wasteful. We get locks ALL the time for every time operation, > when in fact we need it more rarely. If we can tolerate losing a > race, we can eliminate the locking in all but the startup case and > those threads racing the startup: > > if (!is_set) { > pthread_mutex_lock(); > if (!is_set) { > is_set = true; > // do something > } > pthread_mutex_unlock(); > } > > here, we know that is_set only ever changes from false to true. If it > is already true, there's nothing to do. If it is false, we may need > to do something, so we lock, check to see if we really need to do it, > etc. > > Can anybody see a flaw in this logic? Is this not a good place to use pthread_once() instead? -- DE