From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.5 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0111C433E0 for ; Sat, 18 Jul 2020 14:35:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 71AF720684 for ; Sat, 18 Jul 2020 14:35:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727788AbgGROfE (ORCPT ); Sat, 18 Jul 2020 10:35:04 -0400 Received: from netrider.rowland.org ([192.131.102.5]:49105 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1726574AbgGROfD (ORCPT ); Sat, 18 Jul 2020 10:35:03 -0400 Received: (qmail 1180388 invoked by uid 1000); 18 Jul 2020 10:35:02 -0400 Date: Sat, 18 Jul 2020 10:35:02 -0400 From: Alan Stern To: Eric Biggers Cc: Matthew Wilcox , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, "Paul E . McKenney" , linux-fsdevel@vger.kernel.org, Akira Yokosawa , Andrea Parri , Boqun Feng , Daniel Lustig , "Darrick J . Wong" , Dave Chinner , David Howells , Jade Alglave , Luc Maranget , Nicholas Piggin , Peter Zijlstra , Will Deacon Subject: Re: [PATCH] tools/memory-model: document the "one-time init" pattern Message-ID: <20200718143502.GC1179836@rowland.harvard.edu> References: <20200717044427.68747-1-ebiggers@kernel.org> <20200717174750.GQ12769@casper.infradead.org> <20200718013839.GD2183@sol.localdomain> <20200718021304.GS12769@casper.infradead.org> <20200718052818.GF2183@sol.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200718052818.GF2183@sol.localdomain> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 17, 2020 at 10:28:18PM -0700, Eric Biggers wrote: > /** > * INIT_ONCE() - do one-time initialization > * @done: pointer to a 'bool' flag that tracks whether initialization has been > * done yet or not. Must be false by default. > * @mutex: pointer to a mutex to use to synchronize executions of @init_func > * @init_func: the one-time initialization function > * @...: additional arguments to pass to @init_func (optional) > * > * This is a more general version of DO_ONCE_BLOCKING() which supports > * non-static data by allowing the user to specify their own 'done' flag and > * mutex. > * > * Return: 0 on success (done or already done), or a negative errno value > * returned by @init_func. It might be worth pointing out explicitly that init_func can be called multiple times, if it returns an error. > */ > #define INIT_ONCE(done, mutex, init_func, ...) \ > ({ \ > int err = 0; \ > \ > if (!smp_load_acquire(done)) { \ > mutex_lock(mutex); \ > if (!*(done)) { \ > err = init_func(__VA_ARGS__); \ > if (!err) \ > smp_store_release((done), true); \ > } \ > mutex_unlock(mutex); \ > } \ > err; \ > }) If this macro is invoked in multiple places for the same object (which is not unlikely), there is a distinct risk that people will supply different mutexes or done variables for the invocations. IMO a better approach would be to have a macro which, given a variable name v, generates an actual init_once_v() function. Then code wanting to use v would call init_once_v() first, with no danger of inconsistent usage. You can fill in the details... Alan Stern