From patchwork Thu Apr 27 15:45:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 677534 Delivered-To: patch@linaro.org Received: by 2002:a5d:4a41:0:0:0:0:0 with SMTP id v1csp418684wrs; Thu, 27 Apr 2023 08:55:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5GEvRCwJXKKNYQFsMSBC5XCAydckbBImCXtP1qe0I0Wducljxd3nT1wxc7ebQTMuYiglkT X-Received: by 2002:ac8:4e51:0:b0:3f1:f5c0:fe3e with SMTP id e17-20020ac84e51000000b003f1f5c0fe3emr3587256qtw.26.1682610931096; Thu, 27 Apr 2023 08:55:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682610931; cv=none; d=google.com; s=arc-20160816; b=LV82K5xP5cMeIMn7dYfjdfWtd9C6oF7s4DAw08CksebPchO3gnvYX+oAEmA+gUKgnl sEsNZT361nVqtOgPxicXE/iGUtGElmSQvQQBzy2yNS0TFhVGuFz/YReUmRREhuW+JEOL uVIUZbmYjQxT2Udx03W40hOXaMLkhDZSFtSHFUFkZmpi+JHPSqyL5j2l1W4QizFuknGc i/booB3IRuNM8/n5H5Ro9U+T3Y/+oFWDzZ9+gOR7IjNhrqLXXKQqy8HGsu+vrnZEeqkv 6xO9orPqmIqpnbgEPTNaHKTNvcJ8g8/uj14HaTF0KPLs2tKHMHhNGoG7z7ObO/gXM1qh 3JBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Ce0cdJ9i1NfBLh1+Cb1T4HPYjQLrv+NJRBLrnGH/7bo=; b=OsOT1Wdlhqgl6oNs4AxE1SfwU7Ho6Ot4uxkIcDzaPUeZkoIZ07Tv5BWnIEgndRurNW +oOpVaFogyBDhav9xYgfGPkF5tBbVx2UaqLV8+a+X+YFVb1LCm2E8Kqt00nsfbHGT4Zs ILCKrc9cxAC3X+fksyxBRZvyb5hK+wvC08fUf+43ZWCFzNO+GIrYe6DYHfI/YbjVRs8l s0H2fC/CFeaZglOXbbVeCjQ41ko6pykgyEYIgqilbsGGrdHORvGVSjHP1zcqO7Qd/ebJ nXOX9+iDAxYiInzkkD82lrMQlCLok2JWIJSjrsltJoj50aHCjFJYHdTud+0nFKn9xLip cvRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sqP16VpK; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id r4-20020ae9d604000000b007468f1dec63si11948570qkk.571.2023.04.27.08.55.30 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 27 Apr 2023 08:55:31 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sqP16VpK; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ps3wx-0007nc-I5; Thu, 27 Apr 2023 11:54:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ps3wv-0007mC-7X for qemu-devel@nongnu.org; Thu, 27 Apr 2023 11:54:25 -0400 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ps3wt-0006mE-58 for qemu-devel@nongnu.org; Thu, 27 Apr 2023 11:54:24 -0400 Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-2febac9cacdso5384333f8f.1 for ; Thu, 27 Apr 2023 08:54:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1682610861; x=1685202861; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ce0cdJ9i1NfBLh1+Cb1T4HPYjQLrv+NJRBLrnGH/7bo=; b=sqP16VpKZOgf/EXdW4ghzExsmDZrjiEQEUpIASL4UhPilg8U9IiNGhdOtzAiQ6D/BS mhPWxIDCcqQU46Ct/SCXUHlkuPuREOIY3TuJobbsByj89fhUQGqOQ9s1BmnDh9XnWB/a 7zo8fYv9N4cZbD1Td5jysdkXJDmNpJlZ4Bv0tij7DOFZyfWbd40xlcRtF6TrU+ruqlzS 89JxOKmh3SgvaeTWPVh5FAtZ9Slqs9dnt2UDl7Syw2D0G29n3xZO+jy1CLvlUd3Y/9sa dh2xLgD0WdIDXQBgNWZl09sBHRvzwnMPzbj0msQndDud/ki4c2sTWq/9r8CIb9PCnEdT MauQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682610861; x=1685202861; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ce0cdJ9i1NfBLh1+Cb1T4HPYjQLrv+NJRBLrnGH/7bo=; b=ekvfIL7D4mCFtK1fONgDbwLknsI7GsiB+mftg0wSyQtpn9eRhCy+YRzJjLwRBX4Rza 89lUD3B90mGZVB5zHWeCrl5/QVKaqEh9VtIccRb05lK0iiQlodah3UPgXG82HnOTI2bC Gg3bDTXPbEw1oBv+Rz+CoBBwkGlq1TNKd+pKSpca3ZSgMGgIeWfSZ+DUVV7E6/5mNVEf x03MWv5i7L9UmGZJ9USfcSx/dYvN9VtRU1lBffCCuIynQo4QKDvqPAI9Lc5mvgXPOBGd W8swxbZ7tW/OrY8I6PLRQbUP6REtLmcIrI2PgaXZXA5up8nllDSY4Bro+yYNrj1+1iBd JKPg== X-Gm-Message-State: AC+VfDyCc/5eyNOR3KTLNEitXezUcky5s9vEcyHskaqNzuBqnT66ibfA Nj7gcHBRg/RWoJr9P6VfF85IXQ== X-Received: by 2002:a5d:4d4a:0:b0:2f9:5841:a4d4 with SMTP id a10-20020a5d4d4a000000b002f95841a4d4mr1827847wru.27.1682610860790; Thu, 27 Apr 2023 08:54:20 -0700 (PDT) Received: from zen.linaroharston ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id j14-20020adfea4e000000b002fc3d8c134bsm18943318wrn.74.2023.04.27.08.54.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 08:54:20 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 8D2461FFC7; Thu, 27 Apr 2023 16:45:12 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: richard.henderson@linaro.org, =?utf-8?q?Alex_Benn=C3=A9e?= , =?utf-8?q?Philippe_M?= =?utf-8?q?athieu-Daud=C3=A9?= , Juan Quintela , Vladimir Sementsov-Ogievskiy , =?utf-8?q?Daniel_?= =?utf-8?q?P=2E_Berrang=C3=A9?= , Thomas Huth , Markus Armbruster Subject: [PULL 18/18] docs/style: call out the use of GUARD macros Date: Thu, 27 Apr 2023 16:45:10 +0100 Message-Id: <20230427154510.1791273-19-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230427154510.1791273-1-alex.bennee@linaro.org> References: <20230427154510.1791273-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42e; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x42e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org There use makes our code safer so we should mention them. Signed-off-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Juan Quintela Reviewed-by: Vladimir Sementsov-Ogievskiy Message-Id: <20230424092249.58552-19-alex.bennee@linaro.org> diff --git a/docs/devel/style.rst b/docs/devel/style.rst index ac2ce42a2f..aa5e083ff8 100644 --- a/docs/devel/style.rst +++ b/docs/devel/style.rst @@ -665,6 +665,60 @@ Note that there is no need to provide typedefs for QOM structures since these are generated automatically by the QOM declaration macros. See :ref:`qom` for more details. +QEMU GUARD macros +================= + +QEMU provides a number of ``_GUARD`` macros intended to make the +handling of multiple exit paths easier. For example using +``QEMU_LOCK_GUARD`` to take a lock will ensure the lock is released on +exit from the function. + +.. code-block:: c + + static int my_critical_function(SomeState *s, void *data) + { + QEMU_LOCK_GUARD(&s->lock); + do_thing1(data); + if (check_state2(data)) { + return -1; + } + do_thing3(data); + return 0; + } + +will ensure s->lock is released however the function is exited. The +equivalent code without _GUARD macro makes us to carefully put +qemu_mutex_unlock() on all exit points: + +.. code-block:: c + + static int my_critical_function(SomeState *s, void *data) + { + qemu_mutex_lock(&s->lock); + do_thing1(data); + if (check_state2(data)) { + qemu_mutex_unlock(&s->lock); + return -1; + } + do_thing3(data); + qemu_mutex_unlock(&s->lock); + return 0; + } + +There are often ``WITH_`` forms of macros which more easily wrap +around a block inside a function. + +.. code-block:: c + + WITH_RCU_READ_LOCK_GUARD() { + QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) { + err = do_the_thing(kid->child); + if (err < 0) { + return err; + } + } + } + Error handling and reporting ============================