1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include "qemu/osdep.h"
24
25#include "qemu/transactions.h"
26#include "qemu/queue.h"
27
28typedef struct TransactionAction {
29 TransactionActionDrv *drv;
30 void *opaque;
31 QSLIST_ENTRY(TransactionAction) entry;
32} TransactionAction;
33
34struct Transaction {
35 QSLIST_HEAD(, TransactionAction) actions;
36};
37
38Transaction *tran_new(void)
39{
40 Transaction *tran = g_new(Transaction, 1);
41
42 QSLIST_INIT(&tran->actions);
43
44 return tran;
45}
46
47void tran_add(Transaction *tran, TransactionActionDrv *drv, void *opaque)
48{
49 TransactionAction *act;
50
51 act = g_new(TransactionAction, 1);
52 *act = (TransactionAction) {
53 .drv = drv,
54 .opaque = opaque
55 };
56
57 QSLIST_INSERT_HEAD(&tran->actions, act, entry);
58}
59
60void tran_abort(Transaction *tran)
61{
62 TransactionAction *act, *next;
63
64 QSLIST_FOREACH(act, &tran->actions, entry) {
65 if (act->drv->abort) {
66 act->drv->abort(act->opaque);
67 }
68 }
69
70 QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
71 if (act->drv->clean) {
72 act->drv->clean(act->opaque);
73 }
74
75 g_free(act);
76 }
77
78 g_free(tran);
79}
80
81void tran_commit(Transaction *tran)
82{
83 TransactionAction *act, *next;
84
85 QSLIST_FOREACH(act, &tran->actions, entry) {
86 if (act->drv->commit) {
87 act->drv->commit(act->opaque);
88 }
89 }
90
91 QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
92 if (act->drv->clean) {
93 act->drv->clean(act->opaque);
94 }
95
96 g_free(act);
97 }
98
99 g_free(tran);
100}
101