From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id 8B0EB3858D32 for ; Tue, 10 Oct 2023 14:00:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B0EB3858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from r6.localdomain (82-217-174-174.cable.dynamic.v4.ziggo.nl [82.217.174.174]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 755CF300B302; Tue, 10 Oct 2023 16:00:23 +0200 (CEST) Received: by r6.localdomain (Postfix, from userid 1000) id 3716634031B; Tue, 10 Oct 2023 16:00:23 +0200 (CEST) Message-ID: <99c0df19c4fe86f9142597b62fedac1bc1b85e75.camel@klomp.org> Subject: Re: [PATCH 02/16] libelf: Make elf_version thread-safe From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: hsm2@rice.edu Date: Tue, 10 Oct 2023 16:00:23 +0200 In-Reply-To: <20231010134300.53830-2-mark@klomp.org> References: <301fac87e83ebbbd677750579ae9a3429b461bdf.camel@klomp.org> <20231010134300.53830-1-mark@klomp.org> <20231010134300.53830-2-mark@klomp.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.48.4 (3.48.4-1.fc38) MIME-Version: 1.0 X-Spam-Status: No, score=-3033.6 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_BARRACUDACENTRAL,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hi Heather, On Tue, 2023-10-10 at 15:42 +0200, Mark Wielaard wrote: > From: Heather McIntyre >=20 > * elf_version.c (version_once): Define once. > (initialize_version): New static function. > (elf_version): Use initialize_version version_once. >=20 > Signed-off-by: Heather S. McIntyre > Signed-off-by: Mark Wielaard > --- > libelf/elf_version.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) >=20 > diff --git a/libelf/elf_version.c b/libelf/elf_version.c > index 6ec534ab..8296bb65 100644 > --- a/libelf/elf_version.c > +++ b/libelf/elf_version.c > @@ -32,12 +32,21 @@ > #endif > =20 > #include > +#include > =20 > +/* Multiple threads may initialize __libelf_version. > + pthread_once() ensures that __libelf_version is initialized only once= . */ > +once_define(static, version_once); > =20 > /* Currently selected version. Should be EV_CURRENT. > Will be EV_NONE if elf_version () has not been called yet. */ > unsigned int __libelf_version =3D EV_NONE; > =20 > +static void initialize_version(void) > +{ > + __libelf_version =3D EV_CURRENT; > +} > + > unsigned int > elf_version (unsigned int version) > { > @@ -49,7 +58,7 @@ elf_version (unsigned int version) > /* Phew, we know this version. */ > =20 > /* Signal that the version is now initialized. */ > - __libelf_version =3D EV_CURRENT; > + once(version_once, initialize_version); > =20 > /* And return the last (or initial) version. */ > return EV_CURRENT; This is an odd function. The intention clearly was to support more "ELF versions" at some point. But (luckily) that never happened and I doubt there will ever be a different (incompatible) ELF version that we'll have to support. So in the end this will always be EV_CURRENT =3D=3D 1. But the function has to be called to make the rest of the library work. I think this works and is fine. There will most likely never be real contention calling elf_version because normally a program just calls it once at the start. But have you thought about using some atomic operation here instead? Cheers, Mark