From owner-freebsd-hackers Wed Sep 20 14:59:24 1995 Return-Path: owner-hackers Received: (from root@localhost) by freefall.freebsd.org (8.6.12/8.6.6) id OAA01377 for hackers-outgoing; Wed, 20 Sep 1995 14:59:24 -0700 Received: from mpp.minn.net (mpp.Minn.Net [204.157.201.242]) by freefall.freebsd.org (8.6.12/8.6.6) with ESMTP id OAA01360 for ; Wed, 20 Sep 1995 14:59:17 -0700 Received: (from mpp@localhost) by mpp.minn.net (8.6.11/8.6.9) id QAA12896; Wed, 20 Sep 1995 16:58:45 -0500 From: Mike Pritchard Message-Id: <199509202158.QAA12896@mpp.minn.net> Subject: Re: Coding style ( was Re: why is this not a bug in namei?) To: terry@lambert.org (Terry Lambert) Date: Wed, 20 Sep 1995 16:58:44 -0500 (CDT) Cc: terry@lambert.org, nate@rocky.sri.MT.net, davidg@root.com, hackers@freefall.freebsd.org In-Reply-To: <199509201711.KAA01075@phaeton.artisoft.com> from "Terry Lambert" at Sep 20, 95 10:11:52 am X-Mailer: ELM [version 2.4 PL24 ME7a] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Length: 1243 Sender: owner-hackers@FreeBSD.org Precedence: bulk Terry Lambert wrote: > > Why not: > > XXX_LOCK(); > ... > if (error_condition) { > error = EWHATEVER; > goto err_with_lock_held; > } > ... > err_with_lock_held: > XXX_UNLOCK(): > err: > return (error); > > Or better yet: > > XXX_LOCK(); > ... > if (error_condition) { > error = EWHATEVER; > } else { > ... > } > XXX_UNLOCK(): > err: > return (error); > > I don't understand the need for the local lock state variable in your > second example. My previous example was very simplistic. Having to lock multiple objects during a function wasn't not uncommon. E.g. XXX_LOCK(); ... if (error_condition) { XXX_UNLOCK(); return (EWHATEVER); } ... XXX_UNLOCK(); ... if (error_condition2) return (EHWATEVER); ... YYY_LOCK(); ... if (error_condition2) { YYY_UNLOCK(); return (EWHATEVER); } ... YYY_UNLOCK(); return (0); In the above example, with gotos you would need 4 exit points: 1 for an exit without any error. And one each for each of the 3 error conditions, since they all have different requirements. 2 of them have different locks, and 1 has no locks at all. -- Mike Pritchard mpp@mpp.minn.net "Go that way. Really fast. If something gets in your way, turn"