From patchwork Fri Oct 2 14:31:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 9910 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id CA811C3B5C for ; Fri, 2 Oct 2020 14:32:09 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 864E663B6A; Fri, 2 Oct 2020 16:32:09 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="OklxCzZ6"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D954763B59 for ; Fri, 2 Oct 2020 16:32:07 +0200 (CEST) Received: from pyrite.rasen.tech (unknown [IPv6:2400:4051:61:600:2c71:1b79:d06d:5032]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id ABF36528; Fri, 2 Oct 2020 16:32:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1601649127; bh=u2gyxacPihn9ZLzANFT9dxHtFdESh1oXWYz2HI8cvTg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OklxCzZ6Z2k6+yDqawnnKD+UHf6iF1Si0gZXwUx2bxVOscY5yqQHlPKj81lVIiz7F 2Fa6qI19h9t9vN6he7qgfPnSFl5vaXNzMptlsGG6qwL7PmrEsZciGwbzSZi8r6ZG1v BBtXLQOSvObSjA1mj/FgI/KcvAPivcE/vKr6P5/I= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Fri, 2 Oct 2020 23:31:17 +0900 Message-Id: <20201002143154.468162-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201002143154.468162-1-paul.elder@ideasonboard.com> References: <20201002143154.468162-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 01/38] Documentation: coding-style: Document global variable guidelines X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Document the issue related to global variable dependencies and how to avoid them. Signed-off-by: Paul Elder Reviewed-by: Laurent Pinchart --- Changes in v3: - add more detail New in v2 --- Documentation/coding-style.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Documentation/coding-style.rst b/Documentation/coding-style.rst index 8af06d6a..71d5c0b2 100644 --- a/Documentation/coding-style.rst +++ b/Documentation/coding-style.rst @@ -187,6 +187,25 @@ These rules match the `object ownership rules from the Chromium C++ Style Guide` long term borrowing isn't marked through language constructs, it shall be documented explicitly in details in the API. +Global Variables +~~~~~~~~~~~~~~~~ + +The order of initializations and destructions of global variables cannot be +reasonably controlled. This can cause problems (including segfaults) when global +variables depend on each other, directly or indirectly. For example, if the +declaration of a global variable calls a constructor which uses another global +variable that hasn't been initialized yet, incorrect behavior is likely. +Similar issues may occur when the library is unloaded and global variables are +destroyed. + +Global variables that are statically initialized and have trivial destructors +(such as an integer constant) do not cause any issue. Other global variables +shall be avoided when possible, but are allowed when required (for instance to +implement factories with auto-registration). They shall not depend on any other +global variable, should run a minimal amount of code in the constructor and +destructor, and code that contains dependencies should be moved to a later +point in time. + C Compatibility Headers ~~~~~~~~~~~~~~~~~~~~~~~