All of lore.kernel.org
 help / color / mirror / Atom feed
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>,
	John Snow <jsnow@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Vladimir Sementsov-Ogievskiy <v.sementsov-og@mail.ru>,
	qemu-block@nongnu.org,
	Emanuele Giuseppe Esposito <eesposit@redhat.com>
Subject: [RFC PATCH 2/8] transactions: add tran_add_back
Date: Tue, 12 Jul 2022 17:19:05 -0400	[thread overview]
Message-ID: <20220712211911.1302836-3-eesposit@redhat.com> (raw)
In-Reply-To: <20220712211911.1302836-1-eesposit@redhat.com>

First change the transactions from a QLIST to QSIMPLEQ, then
use it to implement tran_add_tail, which allows adding elements
to the end of list transactions.

This is useful if we have some "preparation" transiction callbacks
that we want to run before the others but still only when invoking
finalize/commit/abort.

For example (A and B are lists transaction callbacks):

for (i=0; i < 3; i++) {
	tran_add(A[i]);
	tran_add_tail(B[i]);
}

tran_commit();

Will process transactions in this order: A2 - A1 - A0 - B0 - B1 - B2

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
 include/qemu/transactions.h |  9 +++++++++
 util/transactions.c         | 29 +++++++++++++++++++++--------
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/include/qemu/transactions.h b/include/qemu/transactions.h
index 2f2060acd9..42783720b9 100644
--- a/include/qemu/transactions.h
+++ b/include/qemu/transactions.h
@@ -50,7 +50,16 @@ typedef struct TransactionActionDrv {
 typedef struct Transaction Transaction;
 
 Transaction *tran_new(void);
+/*
+ * Add transaction at the beginning of the transaction list.
+ * @tran will be the first transaction to be processed in finalize/commit/abort.
+ */
 void tran_add(Transaction *tran, TransactionActionDrv *drv, void *opaque);
+/*
+ * Add transaction at the end of the transaction list.
+ * @tran will be the last transaction to be processed in finalize/commit/abort.
+ */
+void tran_add_tail(Transaction *tran, TransactionActionDrv *drv, void *opaque);
 void tran_abort(Transaction *tran);
 void tran_commit(Transaction *tran);
 
diff --git a/util/transactions.c b/util/transactions.c
index 2dbdedce95..89e541c4a4 100644
--- a/util/transactions.c
+++ b/util/transactions.c
@@ -28,18 +28,18 @@
 typedef struct TransactionAction {
     TransactionActionDrv *drv;
     void *opaque;
-    QSLIST_ENTRY(TransactionAction) entry;
+    QSIMPLEQ_ENTRY(TransactionAction) entry;
 } TransactionAction;
 
 struct Transaction {
-    QSLIST_HEAD(, TransactionAction) actions;
+    QSIMPLEQ_HEAD(, TransactionAction) actions;
 };
 
 Transaction *tran_new(void)
 {
     Transaction *tran = g_new(Transaction, 1);
 
-    QSLIST_INIT(&tran->actions);
+    QSIMPLEQ_INIT(&tran->actions);
 
     return tran;
 }
@@ -54,20 +54,33 @@ void tran_add(Transaction *tran, TransactionActionDrv *drv, void *opaque)
         .opaque = opaque
     };
 
-    QSLIST_INSERT_HEAD(&tran->actions, act, entry);
+    QSIMPLEQ_INSERT_HEAD(&tran->actions, act, entry);
+}
+
+void tran_add_tail(Transaction *tran, TransactionActionDrv *drv, void *opaque)
+{
+    TransactionAction *act;
+
+    act = g_new(TransactionAction, 1);
+    *act = (TransactionAction) {
+        .drv = drv,
+        .opaque = opaque
+    };
+
+    QSIMPLEQ_INSERT_TAIL(&tran->actions, act, entry);
 }
 
 void tran_abort(Transaction *tran)
 {
     TransactionAction *act, *next;
 
-    QSLIST_FOREACH(act, &tran->actions, entry) {
+    QSIMPLEQ_FOREACH(act, &tran->actions, entry) {
         if (act->drv->abort) {
             act->drv->abort(act->opaque);
         }
     }
 
-    QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
+    QSIMPLEQ_FOREACH_SAFE(act, &tran->actions, entry, next) {
         if (act->drv->clean) {
             act->drv->clean(act->opaque);
         }
@@ -82,13 +95,13 @@ void tran_commit(Transaction *tran)
 {
     TransactionAction *act, *next;
 
-    QSLIST_FOREACH(act, &tran->actions, entry) {
+    QSIMPLEQ_FOREACH(act, &tran->actions, entry) {
         if (act->drv->commit) {
             act->drv->commit(act->opaque);
         }
     }
 
-    QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
+    QSIMPLEQ_FOREACH_SAFE(act, &tran->actions, entry, next) {
         if (act->drv->clean) {
             act->drv->clean(act->opaque);
         }
-- 
2.31.1



  parent reply	other threads:[~2022-07-12 21:20 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-12 21:19 [RFC PATCH 0/8] Refactor bdrv_try_set_aio_context using transactions Emanuele Giuseppe Esposito
2022-07-12 21:19 ` [RFC PATCH 1/8] block.c: assert bs->aio_context is written under BQL and drains Emanuele Giuseppe Esposito
2022-07-14 14:03   ` Hanna Reitz
2022-07-12 21:19 ` Emanuele Giuseppe Esposito [this message]
2022-07-14 15:13   ` [RFC PATCH 2/8] transactions: add tran_add_back Hanna Reitz
2022-07-18 16:20     ` Paolo Bonzini
2022-07-20 13:36       ` Vladimir Sementsov-Ogievskiy
2022-07-12 21:19 ` [RFC PATCH 3/8] RFC: block: use transactions as a replacement of ->{can_}set_aio_context() Emanuele Giuseppe Esposito
2022-07-14 16:45   ` Hanna Reitz
2022-07-18  8:45     ` Emanuele Giuseppe Esposito
2022-07-18  9:09     ` Emanuele Giuseppe Esposito
2022-07-20 14:09   ` Vladimir Sementsov-Ogievskiy
2022-07-25  8:35     ` Emanuele Giuseppe Esposito
2022-07-12 21:19 ` [RFC PATCH 4/8] blockjob: implement .change_aio_ctx in child_job Emanuele Giuseppe Esposito
2022-07-15 11:14   ` Hanna Reitz
2022-07-12 21:19 ` [RFC PATCH 5/8] block: implement .change_aio_ctx in child_of_bds Emanuele Giuseppe Esposito
2022-07-15 11:17   ` Hanna Reitz
2022-07-12 21:19 ` [RFC PATCH 6/8] block-backend: implement .change_aio_ctx in child_root Emanuele Giuseppe Esposito
2022-07-15 11:34   ` Hanna Reitz
2022-07-12 21:19 ` [RFC PATCH 7/8] block: use the new _change_ API instead of _can_set_ and _set_ Emanuele Giuseppe Esposito
2022-07-15 13:32   ` Hanna Reitz
2022-07-18 16:30   ` Paolo Bonzini
2022-07-18 16:39   ` Paolo Bonzini
2022-07-19  9:57     ` Emanuele Giuseppe Esposito
2022-07-19 18:07       ` Paolo Bonzini
2022-07-20 11:47         ` Emanuele Giuseppe Esposito
2022-07-12 21:19 ` [RFC PATCH 8/8] block: remove all unused ->can_set_aio_ctx and ->set_aio_ctx callbacks Emanuele Giuseppe Esposito
2022-07-15 14:34   ` Hanna Reitz
2022-07-18  8:45     ` Emanuele Giuseppe Esposito
2022-07-18 14:39       ` Emanuele Giuseppe Esposito

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=20220712211911.1302836-3-eesposit@redhat.com \
    --to=eesposit@redhat.com \
    --cc=hreitz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=v.sementsov-og@mail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.