| 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 | * The clean() functions registered by the drivers in a transaction are called |
| 36 | * last, after all abort() or commit() functions have been called. |
| 37 | */ |
| 38 | |
| 39 | #ifndef QEMU_TRANSACTIONS_H |
| 40 | #define QEMU_TRANSACTIONS_H |
| 41 | |
| 42 | #include <gmodule.h> |
| 43 | |
| 44 | typedef struct TransactionActionDrv { |
| 45 | void (*abort)(void *opaque); |
| 46 | void (*commit)(void *opaque); |
| 47 | void (*clean)(void *opaque); |
| 48 | } TransactionActionDrv; |
| 49 | |
| 50 | typedef struct Transaction Transaction; |
| 51 | |
| 52 | Transaction *tran_new(void); |
| 53 | void tran_add(Transaction *tran, TransactionActionDrv *drv, void *opaque); |
| 54 | void tran_abort(Transaction *tran); |
| 55 | void tran_commit(Transaction *tran); |
| 56 | |
| 57 | static inline void tran_finalize(Transaction *tran, int ret) |
| 58 | { |
| 59 | if (ret < 0) { |
| 60 | tran_abort(tran); |
| 61 | } else { |
| 62 | tran_commit(tran); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #endif /* QEMU_TRANSACTIONS_H */ |