[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 18/18] docs/style: call out the use of GUARD macros
From: |
Alex Bennée |
Subject: |
[PATCH 18/18] docs/style: call out the use of GUARD macros |
Date: |
Mon, 24 Apr 2023 10:22:49 +0100 |
There use makes our code safer so we should mention them.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20230420155723.1711048-10-alex.bennee@linaro.org>
---
vppr:
- show example of what QEMU_LOCK_GUARD makes easier (although I
didn't repeat for the WITH_ form)
---
docs/devel/style.rst | 54 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git a/docs/devel/style.rst b/docs/devel/style.rst
index e9fce0fc69..b97e5111fc 100644
--- a/docs/devel/style.rst
+++ b/docs/devel/style.rst
@@ -668,6 +668,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
============================
--
2.39.2
- [PATCH 10/18] MAINTAINERS: Cover tests/avocado/machine_aspeed.py, (continued)
- [PATCH 10/18] MAINTAINERS: Cover tests/avocado/machine_aspeed.py, Alex Bennée, 2023/04/24
- [PATCH 17/18] docs/devel: mention the spacing requirement for QOM, Alex Bennée, 2023/04/24
- [PATCH 15/18] docs/system: remove excessive punctuation from guest-loader docs, Alex Bennée, 2023/04/24
- [PATCH 07/18] .gitlab-ci.d/cirrus: Drop the CI job for compiling with FreeBSD 12, Alex Bennée, 2023/04/24
- [PATCH 12/18] tests/avocado/tuxrun_baselines.py: improve code coverage for ppc64, Alex Bennée, 2023/04/24
- [PATCH 09/18] tests/avocado/machine_aspeed: Fix the broken ast2[56]00_evb_sdk tests, Alex Bennée, 2023/04/24
- [PATCH 11/18] avocado_qemu/__init__.py: factor out the qemu-img finding, Alex Bennée, 2023/04/24
- [PATCH 13/18] tests/tcg: limit the scope of the plugin tests, Alex Bennée, 2023/04/24
- [PATCH 18/18] docs/style: call out the use of GUARD macros,
Alex Bennée <=
- [PATCH 16/18] docs/devel: make a statement about includes, Alex Bennée, 2023/04/24