From: "Alex Bennée" <alex.bennee@linaro.org>
To: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Cc: qemu-devel@nongnu.org,
"Elena Ufimtseva" <elena.ufimtseva@oracle.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
peter.maydell@linaro.org,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Alexandre Iooss" <erdnaxe@crans.org>,
"John G Johnson" <john.g.johnson@oracle.com>,
"Jagannathan Raman" <jag.raman@oracle.com>,
"Juan Quintela" <quintela@redhat.com>,
"Eduardo Habkost" <eduardo@habkost.net>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Mahmoud Mandour" <ma.mandourr@gmail.com>
Subject: Re: [PATCH 9/9] docs/style: call out the use of GUARD macros
Date: Mon, 24 Apr 2023 10:07:33 +0100 [thread overview]
Message-ID: <87sfcpabr4.fsf@linaro.org> (raw)
In-Reply-To: <c2e905e0-2e2a-9666-3ea5-c2453d58a54b@yandex-team.ru>
Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> writes:
> On 20.04.23 18:57, Alex Bennée wrote:
>> There use makes our code safer so we should mention them.
>> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
>
> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
>
>
>> ---
>> docs/devel/style.rst | 36 ++++++++++++++++++++++++++++++++++++
>> 1 file changed, 36 insertions(+)
>> diff --git a/docs/devel/style.rst b/docs/devel/style.rst
>> index 0bd01f3fca..b50a981a86 100644
>> --- a/docs/devel/style.rst
>> +++ b/docs/devel/style.rst
>> @@ -657,6 +657,42 @@ that declaration and the new code.
>> 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;
>> + }
>
> For more clearness, maybe add an equivalent code with qemu_mutex_lock() / qemu_mutex_unlock(), I mean:
>
> 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;
> }
>
>> +
>> +will ensure s->lock is released however the function is exited. 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;
>> + }
>> + }
>> + }
>> +
>
> and maybe similar here.
I added the example although I didn't repeat it for the WITH form
because readers should hopefully have understood the idea with the first
example.
>
>> Error handling and reporting
>> ============================
>>
--
Alex Bennée
Virtualisation Tech Lead @ Linaro
next prev parent reply other threads:[~2023-04-24 9:08 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-20 15:57 [PATCH 0/9] docs: various (style, punctuation and typo fixes) Alex Bennée
2023-04-20 15:57 ` [PATCH 1/9] docs/devel/kconfig.rst: Fix incorrect markup Alex Bennée
2023-04-20 20:53 ` Juan Quintela
2023-04-20 15:57 ` [PATCH 2/9] qemu-options.hx: Update descriptions of memory options for NUMA node Alex Bennée
2023-04-20 20:55 ` Juan Quintela
2023-04-21 6:18 ` Philippe Mathieu-Daudé
2023-04-20 15:57 ` [PATCH 3/9] docs: Fix typo (wphx => whpx) Alex Bennée
2023-04-20 15:57 ` [PATCH 4/9] docs/cxl: Fix sentence Alex Bennée
2023-04-21 12:30 ` Jonathan Cameron via
2023-04-20 15:57 ` [PATCH 5/9] MAINTAINERS: Add Juan Quintela to developer guides review Alex Bennée
2023-04-20 15:57 ` [PATCH 6/9] docs/system: remove excessive punctuation from guest-loader docs Alex Bennée
2023-04-20 16:35 ` Thomas Huth
2023-04-20 20:56 ` Juan Quintela
2023-04-20 15:57 ` [PATCH 7/9] docs/devel: make a statement about includes Alex Bennée
2023-04-20 20:57 ` Juan Quintela
2023-04-20 15:57 ` [PATCH 8/9] docs/devel: mention the spacing requirement for QOM Alex Bennée
2023-04-20 19:32 ` Mark Cave-Ayland
2023-04-21 6:15 ` Philippe Mathieu-Daudé
2023-04-26 8:47 ` Mark Cave-Ayland
2023-04-20 20:58 ` Juan Quintela
2023-04-20 15:57 ` [PATCH 9/9] docs/style: call out the use of GUARD macros Alex Bennée
2023-04-20 20:58 ` Juan Quintela
2023-04-21 6:17 ` Philippe Mathieu-Daudé
2023-04-21 9:07 ` Vladimir Sementsov-Ogievskiy
2023-04-24 9:07 ` Alex Bennée [this message]
2023-04-24 12:58 ` Vladimir Sementsov-Ogievskiy
2023-04-20 16:05 ` [PATCH 0/9] docs: various (style, punctuation and typo fixes) Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87sfcpabr4.fsf@linaro.org \
--to=alex.bennee@linaro.org \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=eduardo@habkost.net \
--cc=elena.ufimtseva@oracle.com \
--cc=erdnaxe@crans.org \
--cc=jag.raman@oracle.com \
--cc=john.g.johnson@oracle.com \
--cc=ma.mandourr@gmail.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=richard.henderson@linaro.org \
--cc=thuth@redhat.com \
--cc=vsementsov@yandex-team.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).