xref: /qemu/include/qemu/transactions.h (revision 727385c4)
1 /*
2  * Simple transactions API
3  *
4  * Copyright (c) 2021 Virtuozzo International GmbH.
5  *
6  * Author:
7  *  Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  *
23  * = Generic transaction API =
24  *
25  * The intended usage is the following: you create "prepare" functions, which
26  * represents the actions. They will usually have Transaction* argument, and
27  * call tran_add() to register finalization callbacks. For finalization
28  * callbacks, prepare corresponding TransactionActionDrv structures.
29  *
30  * Then, when you need to make a transaction, create an empty Transaction by
31  * tran_create(), call your "prepare" functions on it, and finally call
32  * tran_abort() or tran_commit() to finalize the transaction by corresponding
33  * finalization actions in reverse order.
34  */
35 
36 #ifndef QEMU_TRANSACTIONS_H
37 #define QEMU_TRANSACTIONS_H
38 
39 #include <gmodule.h>
40 
41 typedef struct TransactionActionDrv {
42     void (*abort)(void *opaque);
43     void (*commit)(void *opaque);
44     void (*clean)(void *opaque);
45 } TransactionActionDrv;
46 
47 typedef struct Transaction Transaction;
48 
49 Transaction *tran_new(void);
50 void tran_add(Transaction *tran, TransactionActionDrv *drv, void *opaque);
51 void tran_abort(Transaction *tran);
52 void tran_commit(Transaction *tran);
53 
54 static inline void tran_finalize(Transaction *tran, int ret)
55 {
56     if (ret < 0) {
57         tran_abort(tran);
58     } else {
59         tran_commit(tran);
60     }
61 }
62 
63 #endif /* QEMU_TRANSACTIONS_H */
64