summaryrefslogtreecommitdiffstats
path: root/util/transactions.c
blob: 2dbdedce95b44711e38a6828316292665ffb9eaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Simple transactions API
 *
 * Copyright (c) 2021 Virtuozzo International GmbH.
 *
 * Author:
 *  Sementsov-Ogievskiy Vladimir <vsementsov@virtuozzo.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"

#include "qemu/transactions.h"
#include "qemu/queue.h"

typedef struct TransactionAction {
    TransactionActionDrv *drv;
    void *opaque;
    QSLIST_ENTRY(TransactionAction) entry;
} TransactionAction;

struct Transaction {
    QSLIST_HEAD(, TransactionAction) actions;
};

Transaction *tran_new(void)
{
    Transaction *tran = g_new(Transaction, 1);

    QSLIST_INIT(&tran->actions);

    return tran;
}

void tran_add(Transaction *tran, TransactionActionDrv *drv, void *opaque)
{
    TransactionAction *act;

    act = g_new(TransactionAction, 1);
    *act = (TransactionAction) {
        .drv = drv,
        .opaque = opaque
    };

    QSLIST_INSERT_HEAD(&tran->actions, act, entry);
}

void tran_abort(Transaction *tran)
{
    TransactionAction *act, *next;

    QSLIST_FOREACH(act, &tran->actions, entry) {
        if (act->drv->abort) {
            act->drv->abort(act->opaque);
        }
    }

    QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
        if (act->drv->clean) {
            act->drv->clean(act->opaque);
        }

        g_free(act);
    }

    g_free(tran);
}

void tran_commit(Transaction *tran)
{
    TransactionAction *act, *next;

    QSLIST_FOREACH(act, &tran->actions, entry) {
        if (act->drv->commit) {
            act->drv->commit(act->opaque);
        }
    }

    QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
        if (act->drv->clean) {
            act->drv->clean(act->opaque);
        }

        g_free(act);
    }

    g_free(tran);
}