Date: Wed, 28 Nov 2007 17:19:42 -0500 (EST) From: Daniel Eischen <deischen@freebsd.org> To: "M. Warner Losh" <imp@bsdimp.com> Cc: arch@freebsd.org Subject: Re: Code review request: small optimization to localtime.c Message-ID: <Pine.GSO.4.64.0711281718530.24547@sea.ntplx.net> In-Reply-To: <20071128.151021.709401576.imp@bsdimp.com> References: <20071128.151021.709401576.imp@bsdimp.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.GSO.4.64.0711281718530.24547>